# Multiple parameters vs anonymous syntax Classic List Threaded 3 messages Reply | Threaded
Open this post in threaded view
|

## Multiple parameters vs anonymous syntax

 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 * zmTh2 x y = \z -> x * y * zmTh3 x = \y -> \z -> x * y * zmTh4 = \x -> \y -> \z -> x * y * zSo I created a file, anon.hs (attached):module Anon wheremTh1 x y z = x * y * zmTh2 x y = \z -> x * y * zmTh3 x = \y -> \z -> x * y * zmTh4 = \x -> \y -> \z -> x * y * zI 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 mTh1mTh1 :: Num a => a -> a -> a -> a*Anon> :t mTh2mTh2 :: Num a => a -> a -> a -> a*Anon> :t mTh3mTh3 :: Num a => a -> a -> a -> a*Anon> :t mTh4mTh4 :: Integer -> Integer -> Integer -> IntegerWhy 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:\$ ghciGHCi, version 8.2.1: http://www.haskell.org/ghc/  :? for helpPrelude> mTh4 = \x -> \y -> \z -> x * y * zPrelude> :t mTh4mTh4 :: Num a => a -> a -> a -> a-- Wink _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners anon.hs (184 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

## Re: Multiple parameters vs anonymous syntax

 It is because of NoMomomorphismRestriction>let mTh4 = \x -> \y -> \z -> x * y * z>:t mTh4mTh4 :: Integer -> Integer -> Integer -> Integer>:set -XNoMonomorphismRestriction >let mTh4 = \x -> \y -> \z -> x * y * z>:t mTh4mTh4 :: Num a => a -> a -> a -> aI'm not going into it too deeply, as it is somewhat involved and you can read about it but I believe when a function "takes no arguments", it is allowed to specialize polymorphic variables to defaults, and due to the Num constraint it chooses Integer.On Tue, Oct 17, 2017 at 1:28 PM, Wink Saville wrote: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 * zmTh2 x y = \z -> x * y * zmTh3 x = \y -> \z -> x * y * zmTh4 = \x -> \y -> \z -> x * y * zSo I created a file, anon.hs (attached):module Anon wheremTh1 x y z = x * y * zmTh2 x y = \z -> x * y * zmTh3 x = \y -> \z -> x * y * zmTh4 = \x -> \y -> \z -> x * y * zI 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 mTh1mTh1 :: Num a => a -> a -> a -> a*Anon> :t mTh2mTh2 :: Num a => a -> a -> a -> a*Anon> :t mTh3mTh3 :: Num a => a -> a -> a -> a*Anon> :t mTh4mTh4 :: Integer -> Integer -> Integer -> IntegerWhy 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:\$ ghciGHCi, version 8.2.1: http://www.haskell.org/ghc/  :? for helpPrelude> mTh4 = \x -> \y -> \z -> x * y * zPrelude> :t mTh4mTh4 :: Num a => a -> a -> a -> a-- Wink _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

## Re: Multiple parameters vs anonymous syntax

 Thank you, my head's spinning just starting to read  :)BTW, you led me to find the answer to why ghci was different in theinteractive mode vs loading. I discovered there are two sets of optionsused for ghci which are controlled by ":set" and ":seti", . And in theinteractive mode we see -XNoMonomorphismRestriction is in effect:Prelude> :setibase language is: Haskell2010with the following modifiers:  -XExtendedDefaultRules  -XNoMonomorphismRestriction  -XNondecreasingIndentationGHCi-specific dynamic flag settings:other dynamic, non-language, flag settings:  -fimplicit-import-qualifiedwarning settings:But it's not in the "non-interactive" mode:Prelude> :setoptions currently set: none.base language is: Haskell2010with the following modifiers:  -XNondecreasingIndentationGHCi-specific dynamic flag settings:other dynamic, non-language, flag settings:  -fimplicit-import-qualifiedwarning settings:On Tue, Oct 17, 2017 at 10:53 AM David McBride <[hidden email]> wrote:It is because of NoMomomorphismRestriction>let mTh4 = \x -> \y -> \z -> x * y * z>:t mTh4mTh4 :: Integer -> Integer -> Integer -> Integer>:set -XNoMonomorphismRestriction >let mTh4 = \x -> \y -> \z -> x * y * z>:t mTh4mTh4 :: Num a => a -> a -> a -> aI'm not going into it too deeply, as it is somewhat involved and you can read about it but I believe when a function "takes no arguments", it is allowed to specialize polymorphic variables to defaults, and due to the Num constraint it chooses Integer.On Tue, Oct 17, 2017 at 1:28 PM, Wink Saville wrote: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 * zmTh2 x y = \z -> x * y * zmTh3 x = \y -> \z -> x * y * zmTh4 = \x -> \y -> \z -> x * y * zSo I created a file, anon.hs (attached):module Anon wheremTh1 x y z = x * y * zmTh2 x y = \z -> x * y * zmTh3 x = \y -> \z -> x * y * zmTh4 = \x -> \y -> \z -> x * y * zI 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 mTh1mTh1 :: Num a => a -> a -> a -> a*Anon> :t mTh2mTh2 :: Num a => a -> a -> a -> a*Anon> :t mTh3mTh3 :: Num a => a -> a -> a -> a*Anon> :t mTh4mTh4 :: Integer -> Integer -> Integer -> IntegerWhy 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:\$ ghciGHCi, version 8.2.1: http://www.haskell.org/ghc/  :? for helpPrelude> mTh4 = \x -> \y -> \z -> x * y * zPrelude> :t mTh4mTh4 :: Num a => a -> a -> a -> a-- Wink _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners