Mapa mental
Higher order functions
Contenido del mapa
Higher order functions
caracteristics
- :: (a -> a) -> a -> a
- Before, we didn't need parentheses because -> is naturally right-associative.
- buy here they're mandatory
- ndicate that the first parameter is a function that takes something and returns that same thing
- The second parameter is something of that type also and the return value is also of the same type
- Before, we didn't need parentheses because -> is naturally right-associative.
- In the body
- applyTwice f x = f (f x) rocktopus
- use the parameter f as a function, applying x to it by separating them with a space and then applying the result to f again
- applyTwice f x = f (f x) rocktopus
- :: (a -> a) -> a -> a
Haskell functions can take functions as parameters and return functions as return values
- If the function requires us to pass it a function that takes only one paramete
- we can just partially apply a function to the point where it takes only one parameter and then pass it.
- If the function requires us to pass it a function that takes only one paramete
a single higher order function can be used in very versatile ways.
- Imperative programming usually uses stuff like for loops, while loops, setting something to a variable, checking its state
Maps and filters
- map takes a function and a list and applies that function to every element in the list
- producing a new list
- map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs
- producing a new list
- is similar with a list comprehension
- but using map is much more readable
- filter is a function that takes a predicate
- that is a function that tells whether something is true or not
- and return a list and then returns the list of elements that satisfy the predicate
- ilter :: (a -> Bool) -> [a] -> [a] filter _ [] = [] filter p (x:xs) | p x = x : filter p xs | otherwise = filter p xs
- and return a list and then returns the list of elements that satisfy the predicate
- that is a function that tells whether something is true or not
- map takes a function and a list and applies that function to every element in the list
Lambdas
- are anonymous functions that are used because we need some functions only once
- the purpose is passing it to a higher-order function
- To make a lambda, we write a \
- then we write the parameters, separated by spaces
- After put a -> and then the function body.
- then we write the parameters, separated by spaces
- are anonymous functions that are used because we need some functions only once
Información
- Descripción
- Mapa Mental sobre Higher order functions, creado por MENESES CHAVARRIA XUASY SANTIAGO el 04/03/2018.
- Creado por
- MENESES CHAVARRIA XUASY SANTIAGO
- Visitas
- 0
- Valoración