Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't seem to set the value of a char in haskell?

Tags:

char

haskell

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 = "!"
like image 579
matan Avatar asked Mar 23 '23 02:03

matan


1 Answers

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
like image 72
stonemetal Avatar answered Apr 02 '23 12:04

stonemetal