Understanding Haskell's plusOne Function

  • Thread starter Thread starter whitehorsey
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
whitehorsey
Messages
188
Reaction score
0
Adds one to a list of integers.
plusOne :: [Integer] -> [Integer]
plusOne = map (+ 1)

I'm having a hard time understanding how this function works. More specifically how recursion happens. Map takes a function and a list and applies that function to every element in the list. In this function, map doesn't seem to be taking a function it just has (+1) but it adds one to the whole list. How does this work?
 
Physics news on Phys.org
I know nothing about Haskell, but I have a feeling "+1" and "+ 1" are not the same.
 
Haskell supports partial application of functions.
http://www.haskell.org/haskellwiki/Partial_application
The expression (+ 1) gives you a function of type Int -> Int.
You can then take that function and apply it to a value
e.g. (+ 1) 5
That passes the value 5 into the function (+ 1) so the result is 6.
map (+ 1)
is also a partial application. You only pass one argument to map i.e. the function (+ 1). So the result of that partial application is a function that takes in a list and applies (+ 1) to every element.