Mindmap
Syntax in Functions
Inhalt der Mindmap
Syntax in Functions
functions
- Head Function
- head' :: [a] -> a head' [] = error "¡Hey, no puedes utilizar head con una lista vacía!" head' (x:_) = x
- length
- length' :: (Num b) => [a] -> b length' [] = 0 length' (_:xs) = 1 + length' xs
- sum
- Head Function
data type
- numbers, characters, lists, tuples, etc.
- lucky :: (Integral a) => a -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"
- patterns
- will be checked from top to bottom and when it conforms to a pattern, the corresponding function body will be used.
- Normally we use as patterns to avoid repeating ourselves when matching against a bigger pattern when we have to use the whole thing again in the function body.
- patterns
- lucky :: (Integral a) => a -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"
- numbers, characters, lists, tuples, etc.
factorial function recursively
- Recursion is important in Haskell and we'll take a closer look at it later.
- factorial :: (Integral a) => a -> a factorial 0 = 1 factorial n = n * factorial (n - 1)
- Recursion is important in Haskell and we'll take a closer look at it later.
Pattenrs
- Pattern matching can also fail. If we define a function like this:
- charName :: Char -> String charName 'a' = "Albert" charName 'b' = "Broseph" charName 'c' = "Cecil"
- Non-exhaustive patterns in function charName
- When making patterns, we should always include a catch-all pattern so that our program doesn't crash if we get some unexpected input.
- charName :: Char -> String charName 'a' = "Albert" charName 'b' = "Broseph" charName 'c' = "Cecil"
- Pattern matching can also be used on tuples.
- The x:xs pattern
- is used a lot, especially with recursive functions. But patterns that have : in them only match against lists of length 1 or more.
- Pattern matching can also fail. If we define a function like this:
Pairs
- fst and snd extract the components
List
- can be used in pattern matching.
- You can match with the empty list [] or any pattern that involves : and the empty list
- can be used in pattern matching.
imperative languages
- if else
- where
- let
- The names that you define in the let part are accessible to the expression after the in part
- Case expressions
- expression is matched against the patterns.
Informationen
- Beschreibung
- Mindmap am Syntax in Functions, erstellt von carlos marin am 27/02/2018.
- Erstellt von
- carlos marin
- Aufrufe
- 0
- Bewertung