Understanding Haskell's plusOne Function

  • Thread starter Thread starter whitehorsey
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on Haskell's plusOne function, defined as plusOne :: [Integer] -> [Integer], which utilizes the map function to increment each element of a list of integers by one. The expression (+ 1) is a partially applied function that transforms the input list by applying the addition operation to each element. This mechanism of partial application allows map (+ 1) to create a new function that processes the entire list, demonstrating Haskell's functional programming capabilities.

PREREQUISITES
  • Understanding of Haskell syntax and functions
  • Familiarity with functional programming concepts
  • Knowledge of list processing in Haskell
  • Basic understanding of partial application in Haskell
NEXT STEPS
  • Explore Haskell's map function in detail
  • Learn about Haskell's function types and type inference
  • Study the concept of partial application in Haskell
  • Investigate recursion in Haskell and its applications
USEFUL FOR

Haskell beginners, functional programming enthusiasts, and developers looking to deepen their understanding of list manipulation and function application in Haskell.

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.
 

Similar threads

  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
Replies
9
Views
3K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 11 ·
Replies
11
Views
1K