Rumen Hristov
Rumen Hristov

Reputation: 887

what is wrong in the call of this simple haskell test function

Been a loooong time since I wrote code in Haskell, so while I do know quite a bit about it (from previous experience) it is slowly comming back. I am not sure why the following snippet does not want to run. Could you point out please.

convChar::Char->Int
convChar chr
 |chr == 'A'    =0
 |otherwise     =28

main = do
convChar 'A'

Upvotes: 0

Views: 47

Answers (1)

awesoon
awesoon

Reputation: 33671

main function is an entry point of a program and it must be of type IO (), but you are defining it in terms of function with Int type. So, you should transform Int to IO (), that is why you need a function with type Int -> IO(). It may print the given number to stdout, or just sleep the program for a given duration. You could find out such functions using Hoogle.

Note, that print function has Show a => a -> IO () type, so, it is better to search this function by name, not by type Int -> IO ().

So, if you want to print the result of your function, you should rewrite main function in the following way:

main = print $ convChar 'A'

Just a note: if you are not familiar with $ function, you could use brackets, to define the order of execution

main = print (convChar 'A')

But using $ makes your code cleaner

Upvotes: 3

Related Questions