Alex Rivera | Logout

"functions are first class values" what does this exactly mean?

Asked 2012-05-27T21:12:40.177
24

Could somebody clearly explain it with some good example. I came across this statement in Scala, when explaining about the functional programming.

Edit
Report

1 Answer

2

It's a term taken from purely functional languages such as Haskell or Erlang where functions are first class citizens. It means that functions can be passed as arguments to other functions and functions can return other functions.

Because functions are first class citizens – we have a function type – signified by an arrow. In haskel: (->) In skala: (=>)

Consider map function. It's a function which takes a function and a list as its arguments and applies given function to all the elements of the list:

in haskell:

map(\x->x+1)[1,2,3] 
= [2,3,4]

in scala:

List(1,2,3) map (x => x + 1) 
= [2,3,4]

(\x->x+1) and (x => x + 1) are functions (denoted by lambda expressions) passed to map function as arguments.

answered 2012-05-28T16:27:31.123

Your Answer