0

I'm working on a conversion problem for homework, and am a complete Haskell newbie, so bear with me. On one of them, it asks us to attempt to get the type of a function to be:

fc :: (Bool, [Char]) -> Int -> Integer -> [Bool]

Without worrying about what the actual function does or anything. These functions will not be run, it is just a test to see if we can convert types correctly. So far the furthest I can get is this:

fc :: (Bool, [Char]) -> Int
fc (x, y) = ord (head y)

Where I am turning it into an Int. When I try to turn it into an Integer using the toInteger function, it gives me:

Couldn't match expected type `Int -> Integer'
            with actual type `Integer'
In the return type of a call of `toInteger'
Probable cause: `toInteger' is applied to too many arguments
In the expression: toInteger (ord (head y))

Any tips for the new guy?

Edit: What I have been trying, for reference, is:

fc :: (Bool, [Char]) -> Int -> Integer
fc (x, y) = toInteger (ord (head y))

And I am getting the error above.

2
  • You should not be converting types at all. The point of this exercise is totally different, namely (I think) getting you understand currying (look it up). Commented Feb 9, 2014 at 20:17
  • 1
    Although this is almost certainly not what your teacher expects, it would be sufficient to define fc = const . const . return . fst Commented Feb 9, 2014 at 20:38

2 Answers 2

2

Your type signature is wrong. If you convert something you can't write it into the type signature. Only the last one is the return type. The others are parameter types. Follow these:

fc::(Bool,[Char])->Integer
fc (x,y) = toInteger . ord . head $ y

fc::(Bool,[Char])->Int->Integer--
fc (x,y) n = if n == w then n else w
 where w = toInteger . ord . head $ y

Edit: The others mentioned currying what is absolutely correct if your teacher expect it. But the conversions doesn't take place in the type sign.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh I understand now! I did not get how type signatures worked that way until now. Thank you so much!
1

As n.m. says, the idea this is getting at is called currying. Basically, in Haskell, any function takes a single value and returns a single value: a -> b.

So, given this restriction, how can we implement functions like addition, which need two parameters? The answer is that we implement a function which takes a number, and returns another function which takes a number and returns a number. Laying it out this way might clarify things:

add :: Int -> Int -> Int
add x = f where f y = x + y

(which is equivalent to add x y = x + y, as well as add = (+)).

In your case, you should read the error carefully: Couldn't match expected type Int -> Integer with actual type Integer In the return type of a call of toInteger means that Haskell is expecting fc to return a value of type Int -> Integer, because that's what your type signature says, but the definition you've provided will always produce a value of type Integer.

1 Comment

Yep, I have: fc :: (Bool, [Char]) -> Int -> Integer fc (x, y) = toInteger (ord (head y)) and I am getting the same problem: Couldn't match expected type Int -> Integer' with actual type Integer' In the return type of a call of toInteger' Probable cause: toInteger' is applied to too many arguments In the expression: toInteger (ord (head y)) In an equation for `fc': fc (x, y) = toInteger (ord (head y))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.