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

Taillefer, Troy (EXP) troy.taillefer at lmco.com
Fri Mar 2 12:35:12 EST 2007

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


-----Original Message-----
From: haskell-cafe-bounces at haskell.org
[mailto:haskell-cafe-bounces at haskell.org] On Behalf Of iliali16
Sent: March 2, 2007 11:23 AM
To: haskell-cafe at 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:
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Haskell-Cafe mailing list
Haskell-Cafe at haskell.org

More information about the Haskell-Cafe mailing list