[Haskell-cafe] words function

Dougal Stanton ithika at gmail.com
Thu Nov 8 07:04:07 EST 2007

On 08/11/2007, Ryan Bloor <ryanbloor at hotmail.com> wrote:
> hi
>  I am trying to create a function that uses the words function... I am doing
> the same thing to each element in a list so I am using mapping techniques.
>  Code...
>   --Define the main first function
>  rStrings2Results :: ([String] -> String) -> [[String]] -> [String]
>  rStrings2Results f(head:tail) = (f head : rStrings2Results f tail)

Your description of the problem doesn't relate to the type signature
or the solution you have here. I assume the function f which you pass
in is meant to be 'words'? In that case, the type signature should be
(String -> [String]), not the other way round (which would be unwords,
or concat, for example).

The example solution as it stands also doesn't deal with the empty
list. The pattern match against [] will fail.

>  I just want take a list and on the first member ("hello my name is ryan")
> to say [("hello", "my", "name", "is", "ryan"),..............] using the
> words function.

I'm still not completely clear on what you want, so maybe you can
clarify. I guess it's something like:

["my name is ryan", "i am learning haskell", "it is fun"] ->
[["my","name", "is", "ryan"],["i am learning haskell", "it is fun"]]

Is this about right? If so, then your type signature should be:

f :: (String -> [String]) -> [String] -> [[String]]

Although as another poster mentioned, you might think about separating
out the head and the tail of the list. It seems to just be a
two-element list which could be easily represented as a tuple.

f' :: (String -> [String]) -> [String] -> ([String], [String])

Of course I could be very wrong with my interpretation of your problem!



Dougal Stanton
dougal at dougalstanton.net // http://www.dougalstanton.net

More information about the Haskell-Cafe mailing list