1
0
Fork 0
main
Sebastian Steger 2025-08-20 15:03:58 +02:00
parent 2c634800d2
commit 3b8b1c369e
1 changed files with 46 additions and 1 deletions

View File

@ -78,4 +78,49 @@ Expression | Type (:t) | Value | Comment
`case 1 of { 1 -> "small"; 2 -> "medium"; _ -> "large"}` | `String` | `"small"` | patterns are overlapping
`(let x = 5*2 in (x * 2,x /2))` | `(Fractional b, Num a) => (a, b)` | `(20,5.0)` |
## Lists
## Lists
Expression | Type (:t) | Value | Comment
--- | --- | --- | --------
`[1,7,4]` | `Num a => [a]` | `[1,7,4]` |
`[]` | `[a]` | `[]` |
`8 : [1,7,4]` | `Num a => [a]` | `[8,1,7,4]` | **cons** operator, short for construct
`1:7:4:[]` | `Num a => [a]` | `[1,7,4]` | actual construction of a list
`[1,7,4] ++ [8]` | `Num a => [a]` | `[1,7,4,8]` |
`(++)` | `[a] -> [a] -> [a]` | |
`[1,7,4]` | `[Num a] => [a]` | `[1,7,4]` |
`head [1,7,4]` | `Num a => a` | `1` | Similar `last`
`tail [1,7,4]` | `[Num a] => [a]` | `[7,4]` | Similar `init`
`length [1,7,4]` | `Int` | `3` |
`null []` | `Bool` | `True` |
`reverse [1,7,4]` | `[Num a] => [a]` | `[4,7,1]` | Similar `Data.List.sort`
`take 2 [1,7,4]` | `[Num a]=>[a]` | `[1,7]` | Similar `drop`
`take` | `Int -> [a] -> [a]` | |
`sum [1,7,4]` | `Num a=>a` | `12` | Similar `product,maximum,minimum,length`
`foldl (-) 0 [1,7,4]` | `Num a => a` | `-12` | `(((0-1)-7)-4)`
`foldl` | `Foldable t => (b -> a -> b) -> b -> t a -> b` | |
`foldr (-) 0 [1,7,4]` | `Num a => a` | `-2` | `(1-(7-(4-0)))`
`elem 7 [1,7,4]` | `Bool` | `True` |
`all even [1,7,4]` | `Bool` | `False` | Similar `any`
`filter even [1,7,4]` | `[Integral a] => [a]` | `[1,7,4]` | Similar `odd,(>1),(/=3)`
`(length . filter (>1) ) [1,7,4]` | `Int` | `2` |
`Data.List.partition (>1) [1,7,4]` | `(Ord a, Num a) => ([a], [a])` | `([7,4],[1])`
`zip [1,7,4] ["one","seven","four","eight"]` | `Num a => [(a, String)]` | `[(1,"one"),(7,"seven"),(4,"four")]` | truncating to the shorter list
`Data.List.sortOn fst $ zip [1,7,4] ["one", "seven", "four"]` | `(Ord b, Num b) => [(b, String)]` | `[(1,"one"),(4,"four"),(7,"seven")]`
`Data.List.sort ["one","seven","four","eight"]` | `[String]` | `["eight","four","one","seven"]`
`reverse $ Data.List.sort ["one","seven","four","eight"]` | `[String]` | `["seven","one","four","eight"]`
`Data.List.sortBy compare ["one","seven","four","eight"]` | `[String]` | `["eight","four","one","seven"]`
`Data.List.sortBy (flip compare) ["one","seven","four","eight"]` | `[String]` | `["seven","one","four","eight"]`
`Data.List.sortBy (curry( (uncurry compare) . (uncurry(Control.Arrow.***) (length,length)))) ["one","seven","four","eight","ten"]` | `[String]` | `["one","ten","four","seven","eight"]` |
`Data.List.sortBy (Data.Ord.comparing length) ["one","seven","four","eight","ten"]` | `[String]` | `["one","ten","four","seven","eight"]`
`map (+1) [1,7,4]` | `Num a => [a]` | `[2,8,5]` |
`map length ["one", "seven", "four"]` | `[Int]` | `[3,5,4]` |
`map (negate . abs) [1,-7,4]` | `Num a => [a]` | `[-1,-7,-4]` |
`map even [1,7,4]` | `[Bool]` | `[False,False,True]` |
`[1..5]` | `(Num a, Enum a) => [a]` | `[1,2,3,4,5]` |
`[1,3..9]` | `(Num a, Enum a) => [a]` | `[1,3,5,7,9]` |
`[1..]` | `(Num a, Enum a) => [a]` | `[1,2,3,4,5,...]` | infinite sequence
`take 4 [10..]` | `(Num a, Enum a) => [a]` | `[1,2,3,4]` | laziness in action
`[x*2 \| x <- [1..5]]` | `[Num a] => [a]` | `[2, 4, 6, 8, 10]` | List comprehension
``[x*2 \| x <- [1..5], x `mod` 3 == 0]`` | `[Num a] => [a]` | `[6]` |
`[ (a,b,c) \| c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2]` | `(Num c, Eq c, Enum c) => [(c, c, c)]` | `[(3,4,5),(6,8,10)]`