2

I'm playing with the haskell repo around a little. why f2 behaves that way ? (I was expecting f2 takes only 1 argument), also what does t mean in the type?

BTW, why I have to prefix repo with let in order to define function?

λ> :t add  -- f1
add :: Num a => a -> a
λ> let add n = (+) 1  -- f2 what does this mean?
λ> add 1 1
2
λ> :t add
add :: Num a => t -> a -> a
λ> add 1 3
4

2 Answers 2

2
add n = (+) 1

You never use n on the right hand side. Keep in mind that (+) 1 is short for \x -> (+) 1, and you'll see that

add n = (+) 1 = \x -> (+) 1 x

Or, equivalently

add n x = (+) 1 x

Therefore, n can be any type (here called t), since it is not used at all.

You meant

add n = (+) 1 n

BTW, why I have to prefix repo with let in order to define function?

Because you're using GHCi, which doesn't act the same as using GHC on a source file.

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

Comments

0

You are not using n, so it can be of any type you want. t is not some specific type, it means an argument of some type t.

A good indication that t is not used anywhere (and something is wrong) is the fact that only a has a Num constraint. So for add to work properly if it actually used t in the addition, it would have to somehow be able to add a to a variable of any type, including types that are not an instance of Num.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.