I'm learning haskell and having a bit of trouble understanding it. Basically, I have a program which will read in a letter, and change it if it is a consonant to a '!', however I can't seem to set the value of the character..
here's my code so far ..
isConsonant :: Char -> Bool
consonant_alphabet = ['b'..'d']++['f'..'h']++['j'..'n']++['p'..'t']++['v'..'z'] ++ ['B'..'D']++['F'..'H']++['J'..'N']++['P'..'T']++['V'..'Z']
isConsonant character = character `elem` consonant_alphabet
repConsonant :: Char -> String
repConsonant c =
if isConsonant c
then "CONVERT IT TO !"
else do "NO CONVERSION REQUIRED"
I have it as a string output for debugging purposes but I can't figure this one out.. Any ideas? I've tried to do
let c = "!"
Think about the transform you want to make in isolation to how many times you want to make it. repConsonant should be Char -> Char
repConsonant :: Char -> Char
repConsonant c =
if isConsonant c
then '!'
else c
Now repConsonant should do what you want to a character, and all you need to do is apply it to each character in a string.
I can't seem to set the value of the character.
Yes you can only assign names a value once. This is by design. It is supposed to guide your program design to smaller functions that transform input to output style of programming. If you need to build values inside a function you need either "let in" or "where" syntax.
let addone a = let b = 1 in a + b
or
let addtwo a = a + b
where b = 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With