fa-ml at ariis.it
Wed Aug 15 21:27:29 UTC 2018
On Wed, Aug 15, 2018 at 09:06:40PM +0000, Stefan Chacko wrote:
> 1. What is the use of making definitions compared to having no
> definitions? Is it just like a comment for documation or does it really
> make a difference in compiling the code?
Writing signatures is always useful:
- to prototype (and let the compiler complain about wrong types)
- to restrict a function to some types only, e.g.
intHead :: [Int] -> Int
intHead is = head is
- to have an immediate, clear, verifiable documentation near the
> 2. What does the arrow(->) mean in such a definition? [...]
> For example there is sumWithL :: [Int] -> Int -> Int
`[Int] -> Int -> Int` can be (with great approximation) be read
"this function takes two arguments, `[Int]` and `Int`, to return
More precisely, if you provide `[Int]` you'll get back a function
that takes an Int to return one Int (Int -> Int), and if you
(finally) pass an Int to that, you receive the final result.
> 1. Why do we use clinches in such definitions. I concluded
> you need clinches if a function is not associative
(->) (called `function application`) associates to the right, so
sumWithL :: [Int] -> Int -> Int
is equal to
sumWithL :: [Int] -> (Int -> Int)
If you want a function that takes a function `[Int] -> Int` to return
an Int you got to put parentheses:
sumWithL :: ([Int] -> Int) -> Int
Hope it was clear!
p.s. haskell at haskell.org is for announcements only, so I am removing it
More information about the Haskell-Cafe