[Haskell-cafe] Hi I need help for very simple question!

Marc Weber marco-oweber at gmx.de
Fri Mar 2 12:56:44 EST 2007


On Fri, Mar 02, 2007 at 08:22:46AM -0800, iliali16 wrote:
> suggestions. Also I don't know how to produce a base case for my recursion.
> 
> import Char
> 
> intoWords ::String -> [String]
> 
> intoWords  (x:xs) 
> 	|isSpace x = intoWords xs
> 	|otherwise = xs

He. You got the type signature right... but... can you tell me which
type is xs ? xs has the same type as (x:xs) thats your input. 
Thus String
But which type has intoWords xs ? That is the return type of your function.
Thus [String]
Do they match?

I don't know which haskell implementation you are using (ghc, hugs,
helium?)

ghc show the error 
test.hs|8| 14:
     Couldn't match expected type `String' against inferred type `Char'
       Expected type: [String]
       Inferred type: [Char]
     In the expression: xs
     In the definition of `intoWords':
 	intoWords (x : xs)
 		    | isSpace x = intoWords xs
 		    | otherwise = xs

What happens?

The compiler finds
[String]   and String

which is equal to
[String]  and [Char]

Now ghc tries to match the type contained in the list
String and Char. You should be able to understand that error message now.

Lets have look hat the output of hugs:

ERROR "test.hs":8 - Type error in guarded expression
*** Term           : xs
*** Type           : [Char]
*** Does not match : [String]

I think you already did know why this function doesn't compile. We still have to
discuss how to solve this problem...

Goto hoogle and try to find the function by name or type (name is much better here)
http://haskell.org/hoogle/?q=words
Now you know that the function is called words.
Have look at the documentation and get another function (eg fromInteger)
Use google code search (we need that second name to get the module we want faster)
http://www.google.com/codesearch?hl=en&lr=&q=fromInteger+words+unwords+lang%3Ahaskell&btnG=Search
The first match is from hugs. Thats fine.
You can find a working implementation of your problem there.

Using google code search may not always show up recent work.
That's why I'h suggest get the sources of the Prelude (distributed with the source of hugs or ghc).

You've tried writing this one week?
You don't have to. Get an irc client and join irc.freenode.net channel #haskell.
There are many kind people telling you at least where to find more information
about your problem immediately.

Marc


More information about the Haskell-Cafe mailing list