partially indeterminate?

Hi. For learning, I was doing the "phone words" problem, where a function translates the digits of a phone number into all possible words. I am trying to connect this idea to the idea of list comprehensions / list monads (sort of the same thing, yes?) I know it is easy to do this: w = do two <- "ABC" three <- "DEF" four <- "GHI" -- and the other digits [[two, three, two, four]] -- for example But what if you don't know in advance what the digits will be? I'm not sure how to insert that deterministic component into this idea. So far, I have a framework like so: p dx = do undefined where m = map (\d -> case d of 2 -> "ABC" 3 -> "DEF" 4 -> "GHI" 5 -> "JKL" 6 -> "MNO" 7 -> "PRS" 8 -> "TUV" 9 -> "WXY" otherwise -> show d) dx I would appreciate any guidance.

I will suggest an analogy to a similar (but simpler) situation. Suppose you are adding up some numbers. foo = let { x = 3; y = 4; z = 5 } in x + y + z That pattern only works if you know in advance how many numbers you are going to add. Otherwise, you need a function that works on a whole list of numbers: sum = foldr (+) 0 So...the short answer to your question is: use recursion. The better answer is: use library functions that do the recursion for you. You might want to look at foldM: http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Mon... -Karl
participants (2)
-
Christopher Howard
-
Karl Voelker