PDA

Click to See Complete Forum and Search --> : haskell function question


Pouncer
Nov 17th, 2005, 12:30 PM
type Word = String

italplural :: Word -> Word
italplural (x:xs)
| (length(xs) == 0) = if (x == 'e') || (x == 'i') then x == 'o'


can anyone aee why it doesnt work? im trying to make the function replace ever letter on the end of the word with o (if its a 'e' or a 'i'), i've tried to start it off by checking if the tail is empty, and if the head of the word is e oe i, but it doesnt work, any ideas?

Craig100
Nov 17th, 2005, 05:19 PM
Hello pouncer, im not sure I understand you fully, do you want to replace every word, that ends in 'e' or 'i' with an 'o', or do you want to add an 'o' to the end of a word beginning with 'e' or 'i'?


and if the head of the word is e oe i


Thats what you would be doing by this.

I wrote a function that replaces all the e's and i's with o's. I hope it helps a little, I can maybe help more if you let me know about the above question.


type Word = String

italplural :: Word -> Word
italplural [] = []
italplural (x:xs)
| (x == 'e') || (x == 'i') = 'o' : italplural xs
| otherwise = x : italplural xs


Craig :)

Pouncer
Nov 18th, 2005, 08:02 AM
thank you very much for the reply,

I want to replace every word, that ends in 'e' or 'i' with an 'o'

and

replace every word, that ends in 'a' with an 'e'

e..g

pizza = pizze
helle = hello
a = e
i = o

and any word which doesnt end in a,e,i .. i just have to leave them as is, e.g.
hey = hey
f = f
erm = erm

etc..

Thanks, any help would be brilliant!!

Craig100
Nov 19th, 2005, 06:26 PM
Hey Pouncer, maybe some sort of input argument of the form;

italplural :: Word -> Word
italplural [] = []
italplural (x:y:xs)


may work, as you see it takes the first two elements or two 'heads' you could say, and adds them onto the tail of the list. We can treat this as a list because a String is the same as a [Char] (a list of characters).

I have tried it out tonight but got only so far, I will give it another bash and post something up tommorow for you.

Edit... I forgot to mention..the whole point of this would be to test if 'y' is a blank space ( ' ' ), then you would know that your at the end of the word.

Craig :)

Craig100
Nov 26th, 2005, 11:02 AM
Hey again, sorry for the delay.

I have come up with a new idea for you;

The standard prelude contains a function called "last". This function will return the last element(letter) of a word.

If you incorporate last into a simple recursive statement, then you have solved your problem.


Craig :)