2 Replies - 1651 Views - Last Post: 28 July 2009 - 02:19 PM

#1 hellhound   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 07-March 09

another haskell question

Posted 27 July 2009 - 07:04 PM

hey again heres another question for you

i have this peice of code that i have written but it spits out that there is a formation error. I have tried looking for the error but i cannot find it. i was hoping that someone here would be able to find it or maybe even surgest a better way to do this.

i am trying to write a program that will take a char a->z and a number 0->26 and use the number to "encript" the letter.

encode :: Char -> Int -> char
encode c i
	|i<0 || i>26 || c<"a" || c>"z" = "?"
	|i==0 = c
	|otherwise conic((conci c)+i)

conci :: Char -> Int
conci c = fromEnum c

conic :: Int -> Char 
conic i
	|i>=122 =toEnum i-26
	|otherwise = toEnum i




Is This A Good Question/Topic? 0
  • +

Replies To: another haskell question

#2 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: another haskell question

Posted 28 July 2009 - 05:29 AM

there you go
encode :: Char -> Int -> Char
encode c i | i < 0 || i > 26 || c < 'a' || c > 'z' = '?'
		   | i == 0 = c
		   | otherwise = conic((conci c) + i)

conci :: Char -> Int
conci c = fromEnum c

conic :: Int -> Char
conic i |i >= 122 = toEnum (i-26)
		|otherwise = toEnum i



you had a small typo in the first function definition which is the capital 'C' in Char and you also forgot the '=' in the otherwise case..also you defined the encode function to return a Char but you were actually returning [Char] which is a list of characters or a string. now haskell gave you an error coz you were writing your characters between double quotations which is defined to be a list of characters in haskell, characters should be written between single quotations... now in the conic function you are using the predefined toEnum function which takes only 1 argument but in the first case you were calling it like this
toEnum i-26


since no parenthesis were used, haskell will think that you were trying to pass more than one argument to the function which will spit out an error so to solve this problem you must parenthesis around the argument
toEnum (i-26)
to denote that you are actually passing one argument to it..

hope this helps :)

This post has been edited by mostyfriedman: 28 July 2009 - 05:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 hellhound   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 07-March 09

Re: another haskell question

Posted 28 July 2009 - 02:19 PM

wow thank you so much i understand so much more now. thank you for taking the time to attully explain it to me rather than just telling me the solutions.

thanks again :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1