Understanding Haskell's plusOne Function

  • Thread starter Thread starter whitehorsey
  • Start date Start date
AI Thread Summary
The plusOne function in Haskell adds one to each integer in a list using the map function, which applies a given function to every element. The expression (+ 1) is a partially applied function that takes an integer and returns that integer plus one. When map is called with (+ 1), it creates a new function that takes a list and applies (+ 1) to each element of that list. Understanding this concept of partial application is crucial for grasping how Haskell functions operate. Overall, the plusOne function demonstrates Haskell's functional programming capabilities effectively.
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?
 
Technology 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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Back
Top