[Haskell-beginners] Multiple parameters vs anonymous syntax

Wink Saville wink at saville.com
Tue Oct 17 17:28:20 UTC 2017


I'm going through "Haskell Programming from first principles" and in
section 7.3 Anonymous Functions there is an exercise on converting multiple
parameters to anonymous functions, and it asks:

1. Which (two or more) of the following are equivalent?

mTh1 x y z = x * y * z
mTh2 x y = \z -> x * y * z
mTh3 x = \y -> \z -> x * y * z
mTh4 = \x -> \y -> \z -> x * y * z

So I created a file, anon.hs (attached):

module Anon where

mTh1 x y z = x * y * z
mTh2 x y = \z -> x * y * z
mTh3 x = \y -> \z -> x * y * z
mTh4 = \x -> \y -> \z -> x * y * z

I load that into ghci and check the function types:

$ ghci anon.hs
GHCi, version 8.2.1: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Anon             ( anon.hs, interpreted )
Ok, 1 module loaded.
*Anon> :t mTh1
mTh1 :: Num a => a -> a -> a -> a
*Anon> :t mTh2
mTh2 :: Num a => a -> a -> a -> a
*Anon> :t mTh3
mTh3 :: Num a => a -> a -> a -> a
*Anon> :t mTh4
mTh4 :: Integer -> Integer -> Integer -> Integer

Why is mTh4 different from the rest?


On the flip side If I enter "mTh4 = \x -> \y -> \z -> x * y * z" directly
in ghci command line then it has same type as the others:

$ ghci
GHCi, version 8.2.1: http://www.haskell.org/ghc/  :? for help
Prelude> mTh4 = \x -> \y -> \z -> x * y * z
Prelude> :t mTh4
mTh4 :: Num a => a -> a -> a -> a


-- Wink
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20171017/e3f68941/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: anon.hs
Type: text/x-haskell
Size: 135 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20171017/e3f68941/attachment.hs>


More information about the Beginners mailing list