Hi I need help for very simple question!

Hi Haskell People, I have problem implementing one function. I think my idea is write but I have minor mistakes which I cannot get right since I progrem Haskell from very recently. No the function is called intoWords and has to take a string of any size and kind of characters.No I have to produce as output a list of strings.To have a rule which defines an element from the list is the space character which can also be included in the string.So what i think should be smth like. intoWord Hello my name is Smart should produse a list looking like [Hello,my,name,is,Smart] Now I tried to do it recucively to take the string and checks if an element is white space in the list using the isSPace build in function. This is my code please help me if you have any 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 Thanks in advance for any suggestions. I really want to understand this bloody function couse I've been tring for a week now and I have tried so many ways to produce it that now I am fed up with it and I want to know how should it work since I am curious now. -- View this message in context: http://www.nabble.com/Hi-I-need-help-for-very-simple-question%21-tf3334486.h... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Hi there 1. First of all never forget your base case for exiting your recursion 2. you need to break up the problem like so import Char -- get the first word word :: String -> String word [] = [] word ( x : x1 : xs ) | isSpace x = [] | isSpace x1 = x : [] | otherwise = x : x1 : word(xs) -- get everything but the first word rest :: String -> String rest [] = [] rest ( x : x1 : xs ) | isSpace x = x1 : xs | isSpace x1 = xs | otherwise = rest(xs) intoWords :: String -> [ String ] intoWords [] = [] intoWords ws = word(ws) : words( rest(ws) ) -- glue the first word and call recursively on the rest 3. Assuming this wasn't for a homework assignment or curiosity the you can use the standard function Prelude.words to do this Troy -----Original Message----- From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of iliali16 Sent: March 2, 2007 11:23 AM To: haskell-cafe@haskell.org Subject: [Haskell-cafe] Hi I need help for very simple question! Hi Haskell People, I have problem implementing one function. I think my idea is write but I have minor mistakes which I cannot get right since I progrem Haskell from very recently. No the function is called intoWords and has to take a string of any size and kind of characters.No I have to produce as output a list of strings.To have a rule which defines an element from the list is the space character which can also be included in the string.So what i think should be smth like. intoWord Hello my name is Smart should produse a list looking like [Hello,my,name,is,Smart] Now I tried to do it recucively to take the string and checks if an element is white space in the list using the isSPace build in function. This is my code please help me if you have any 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 Thanks in advance for any suggestions. I really want to understand this bloody function couse I've been tring for a week now and I have tried so many ways to produce it that now I am fed up with it and I want to know how should it work since I am curious now. -- View this message in context: http://www.nabble.com/Hi-I-need-help-for-very-simple-question%21-tf33344 86.html#a9272506 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Taillefer, Troy (EXP) wrote:
Hi there
1. First of all never forget your base case for exiting your recursion 2. you need to break up the problem like so
import Char
-- get the first word word :: String -> String word [] = [] word ( x : x1 : xs ) | isSpace x = [] | isSpace x1 = x : [] | otherwise = x : x1 : word(xs)
-- get everything but the first word rest :: String -> String rest [] = [] rest ( x : x1 : xs ) | isSpace x = x1 : xs | isSpace x1 = xs | otherwise = rest(xs)
intoWords :: String -> [ String ] intoWords [] = [] intoWords ws = word(ws) : words( rest(ws) ) -- glue the first word and call recursively on the rest
Personally, I think it's a bit clearer to use dropWhile and span: import Data.List import Data.Char mywords :: String -> [String] mywords string | word == "" = [] | otherwise = word:mywords remain where (word, remain) = span (not . isSpace) . dropWhile isSpace $ string

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

Well, your function only remove space chars at string beginning. You may
use Prelude.words :: String -> [String] or some as:
http://haskell.org/hoogle/hoodoc.cgi?module=Prelude&name=words&mode=func
intoWords s = aux s []
2007/3/2, iliali16
Hi Haskell People, I have problem implementing one function. I think my idea is write but I have minor mistakes which I cannot get right since I progrem Haskell from very recently. No the function is called intoWords and has to take a string of any size and kind of characters.No I have to produce as output a list of strings.To have a rule which defines an element from the list is the space character which can also be included in the string.So what i think should be smth like. intoWord Hello my name is Smart should produse a list looking like [Hello,my,name,is,Smart] Now I tried to do it recucively to take the string and checks if an element is white space in the list using the isSPace build in function. This is my code please help me if you have any 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
Thanks in advance for any suggestions. I really want to understand this bloody function couse I've been tring for a week now and I have tried so many ways to produce it that now I am fed up with it and I want to know how should it work since I am curious now.
-- View this message in context: http://www.nabble.com/Hi-I-need-help-for-very-simple-question%21-tf3334486.h... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (5)
-
Bryan Donlan
-
iliali16
-
ivan gomez
-
Marc Weber
-
Taillefer, Troy (EXP)