Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell Function Application

A bit of a neophyte haskell question, but I came across this example in Haskell's tutorial examples. For "find the last element of a list" there are some obvious versions, like

last' [x] = x
last' (_:xs) = last' xs

But I can't make sense of an alternate version presented:

myLast' = foldr1 (const id)

So, in trying to make sense of what the application of the id function is doing, I tried in ghci:

const id 1 2 -> gives 2

This binds like this:

(const id) 1 2 -> gives 2

And not like this:

 const (id 1) 2 -> gives 1 

But I'm not making sense of this. (const id) should translate to something like

`(\x y->x) (\x->x)` 

Shouldn't this return a function that simply returns the id of its first element? Or, how is the function order making (const id) behave differently than const?

like image 706
Steve B. Avatar asked Dec 05 '08 05:12

Steve B.


People also ask

What is function application Haskell?

To "apply something" means to use it. To "apply A to B" means to do something to B using A. So "apply a function" means to use/call the function on something. In Haskell when I write the expression f x I am applying f to x . Thus "function application" is just a term for the general concept of applying functions.

What is a curried function Haskell?

From HaskellWiki. Currying is the process of transforming a function that takes multiple arguments in a tuple as its argument, into a function that takes just a single argument and returns another function which accepts further arguments, one by one, that the original function would receive in the rest of that tuple.

What is partial application Haskell?

Partial function application refers to calling a multiple parameter function with less than its total number of parameters. This results in a new function taking the remaining number of parameters.


2 Answers

The definition of const is

const x = \_ -> x 

Hence, (const id) is a function which takes one argument and always returns id and

const id 1 2 = (\_ -> id) 1 2              = id 2              = 2 

The definition of foldr1 is

foldr1 f [x] = x foldr1 f (x:xs) = f x (foldr1 f xs) 

If we have

myLast' = foldr1 (const id) 

then

myLast' [x] = foldr1 (const id) [x]               {- definition of foldr1 -}             = x 

and

myLast' (x:xs) = foldr1 (const id) (x:xs)                  {- definition of foldr1 -}                = (const id) x (foldr1 (const id) xs)                  {- definition of const -}                  = (\_ -> id) x (foldr1 (const id) xs)                  {- function application -}                  = id (foldr1 (const id) xs)                  {- definition of id -}                  = foldr1 (const id) xs                  {- definition of myLast' -}                  = myLast' xs 

which agrees with the definition of last'.

like image 89
Chris Conway Avatar answered Sep 23 '22 15:09

Chris Conway


I rely heavily on :t when trying to understand Haskell. In this case:

Prelude> :t const id const id :: b -> a -> a

might have helped you see what was going on.

like image 25
Magnus Avatar answered Sep 20 '22 15:09

Magnus