Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Understanding Haskell

  1. May 6, 2013 #1
    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?
  2. jcsd
  3. May 6, 2013 #2


    User Avatar

    Staff: Mentor

    I know nothing about Haskell, but I have a feeling "+1" and "+ 1" are not the same.
  4. May 7, 2013 #3
    Haskell supports partial application of functions.
    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook