Christopher Howard | 16 Jul 01:39 2013

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"
(Continue reading)

Karl Voelker | 16 Jul 04:59 2013
Picon

Re: partially indeterminate?

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:


-Karl
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane