Understanding Haskell's plusOne Function

  • Thread starter whitehorsey
  • Start date
In summary, the function plusOne takes in a list of integers and uses the map function to apply the (+ 1) function to every element in the list. The expression (+ 1) is a partial application, which means it creates a function that takes in a value and adds 1 to it. This function is then applied to each element in the list, resulting in a new list with each element being 1 more than the original.
  • #1
whitehorsey
192
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
  • #2
I know nothing about Haskell, but I have a feeling "+1" and "+ 1" are not the same.
 
  • #3
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.
 

1. What is the plusOne function in Haskell?

The plusOne function in Haskell is a simple function that takes in an integer as an argument and adds one to it, returning the result. It can be defined as follows: plusOne :: Int -> Int and plusOne x = x + 1.

2. How does the plusOne function work?

The plusOne function in Haskell uses a concept called "currying" to take in an integer as an argument and return a function that adds one to that integer. This is possible because in Haskell, all functions are curried by default.

3. Can the plusOne function be used with different types of numbers?

Yes, the plusOne function in Haskell can be used with any numerical type, as long as the type implements the Num typeclass. This includes integers, floating point numbers, and even custom numerical types.

4. What happens if a non-numeric type is passed into the plusOne function?

If a non-numeric type is passed into the plusOne function, Haskell will throw a type error because the function is expecting a numerical type. This is due to the type signature plusOne :: Int -> Int, which specifies that both the input and output must be integers.

5. How can I use the plusOne function in my own Haskell programs?

To use the plusOne function in your own Haskell programs, you can either define it in your code or import it from a module that already contains the function. Once imported, you can use it as you would any other function in your program.

Similar threads

  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
1
Views
279
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
1
Views
826
  • Programming and Computer Science
Replies
11
Views
812
Back
Top