zzz6519003 / blog

My blog about coding
4 stars 1 forks source link

haskell day2(todo) #85

Open zzz6519003 opened 7 years ago

zzz6519003 commented 7 years ago

You can also use map with part of a function, called a section, like this: ``` Prelude> map (+ 1) [1, 2, 3] [2,3,4]

(+ 1) is actually a partially applied function. The + function takes two parameters, and we’ve supplied only one. The result is that we get a function like (x + 1), with a single parameter x. 

Every function in Haskell has one parameter 

(**todo**)
```haskell
Prelude> :t prod
prod :: (Num a) => a -> a -> a 

The portion Num a => means “In the following type definition, a is a type of Num.” You’ve seen the rest before, and I lied to you about the meaning to simplify things. Now, it’s time to set the record straight. Haskell uses

Haskell uses a concept to split one function on multiple arguments into multiple functions, each with one argument. Haskell does this job with partial application.

That process is called currying, and just about every multiple-argument function in Haskell gets curried. That leads to greater flexibility and simpler syntax. Most of the time, you don’t really have to think about it, because the value of curried and uncurried functions is equivalent.

The nice thing about functional languages is that you can compose them in unexpected ways. For example, we can use function composi- tion in conjunction with partially applied functions and lazy sequences effortlessly:

*Main> take 5 (map ((* 2) . (* 5)) fib)
[10,10,20,30,50]

You can easily see how you’d compose the solutions to problems. You just pass one function to the next. In Haskell, f . g x is shorthand for f(g x). When you’re building functions in this way, you might want to apply them from first to last. You’d do so with the . operator. For exam- ple, to invert an image, flip it vertically and then flip it horizontally, an image processor might do something like (flipHorizontally . flipVertically . invert) image.