From 7b6c597f32c7845648c7ed407d0ee08f9f08308c Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Wed, 20 Aug 2025 15:01:58 +0200 Subject: [PATCH 1/9] Types, Literals, and Constants along with Arithmetic and Boolean Operations --- haskell/01-expressions/README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/haskell/01-expressions/README.md b/haskell/01-expressions/README.md index 2bac07c..c6f7464 100644 --- a/haskell/01-expressions/README.md +++ b/haskell/01-expressions/README.md @@ -4,6 +4,20 @@ The following illustrates some haskell basics. It can be reproduced using `ghci` ## Types, Literals, and Constants along with Arithmetic and Boolean Operations +Expression | Type (:t) | Value | Comment +--- | --- | --- | -------- +`2` | `Num a => a` | `2` | +`2.03` | `Fractional a => a` | `2.3` | +`'c'` | `Char` | `'c'` +`"hallo"` | `String` | `"hallo"` | +`True` | `Bool` | `True` | +`x=5` | `Num a => a` | `5` | Constant named `x` +`2 + 3` | `Num a => a` | `5` | Similar `-,*,^` +`2 / 3` | `Fractional a => a` | `0.6666666666666666` | +`1 > 2` | `Bool` | `False` | Similar `<, <=, >=, ==, /=` +`True && False` | `Bool` | `False` | Similar `\|\|,not` +`((2+3)*5 > 1) \|\| not (1 > 2)` | `Bool` | `True` | Right part is not evaluated due to lazy evaluation + ## Unary Functions ## Binary Functions From eed6fc8fae740e472318caefb42c7e2cb4af7fb5 Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Wed, 20 Aug 2025 15:02:22 +0200 Subject: [PATCH 2/9] Unary Functions --- haskell/01-expressions/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/haskell/01-expressions/README.md b/haskell/01-expressions/README.md index c6f7464..fab33b6 100644 --- a/haskell/01-expressions/README.md +++ b/haskell/01-expressions/README.md @@ -20,6 +20,19 @@ Expression | Type (:t) | Value | Comment ## Unary Functions +Expression | Type (:t) | Value | Comment +--- | --- | --- | -------- +`sqrt(4)` | `Floating a => a` | `2.0` | +`sqrt 4` | `Floating a => a` | `2.0` | Similar `abs,negate,signum,recip` +`sqrt` | `Floating a => a -> a` | | +`sqrt 3^2 + 4^2` | `Floating a => a` | `19.0` | +`sqrt (3^2 + 4^2)` | `Floating a => a` | `5.0` | +`negate(sqrt (3^2 + 4^2))` | `Floating a => a` | `-5.0` | +`negate $ sqrt (3^2 + 4^2)` | `Floating a => a`| `-5.0` | Function **application** operator `$` +`(negate . sqrt) (3^2 + 4^2)`| `Floating a => a`| `-5.0` | Function **composition** operator `.` Reads `negate` **after** `negate . sqrt` | +`(negate . length) "hallo"` | `Int` | `-5` | +`negatedRoot = negate . sqrt` | `Floating c => c -> c` | | Constant named `negatedRoot` + ## Binary Functions ## Tuples From 6e1083ad568bc14fa196dd55a4949aff0c0e113e Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Wed, 20 Aug 2025 15:02:40 +0200 Subject: [PATCH 3/9] Binary Functions --- haskell/01-expressions/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/haskell/01-expressions/README.md b/haskell/01-expressions/README.md index fab33b6..dec9b5d 100644 --- a/haskell/01-expressions/README.md +++ b/haskell/01-expressions/README.md @@ -35,6 +35,23 @@ Expression | Type (:t) | Value | Comment ## Binary Functions +Expression | Type (:t) | Value | Comment +--- | --- | --- | -------- +`div 8 4` | `Integral a => a` | `2` | Similar `mod,gcd,lcm,not` +``8 `div` 4`` | `Integral a => a` | `2` | **Infix** notation using backticks +`8 + 4` | `Num a => a` | `12` | `-,*,^,<, <=, >=, ==, /=` +`(+) 8 4` | `Num a => a` | `12` | **Prefix** notation using parenthesis +`div` | `Integral a => a -> a -> a` | | **Currying** represents multi-argument functions as a chain of single-argument functions | +`div 8` | `Integral a => a -> a` | | binds the **first** argument; Similar `(8/),(8*),(8+),(8-)` | +`div8bySomething = div 8` | `Integral a => a -> a` | | +`div8bySomething 4` | `Integral a => a` | `2` | +`uncurry div` | `Integral c => (c, c) -> c` | | accepts a single tuple argument +`uncurry div (8,4)` | `Integral c => c` | `2` | +`uncurry` | `(a -> b -> c) -> (a, b) -> c`| | +`` (`div` 4) `` | `Integral a => a -> a` | | **Sectioning** binds the **second** argument of infix functions; Similar `(/8),(*8),(+8),(-8)` | +``divSomethingBy4 = (`div` 4)`` | `Integral a => a -> a` | | +`divSomethingBy4 8` | `Integral a => a` | `2` | + ## Tuples ## Strings From 9fda96489161933ca435939d22cb91a642c82d8b Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Wed, 20 Aug 2025 15:03:01 +0200 Subject: [PATCH 4/9] Tuples --- haskell/01-expressions/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/haskell/01-expressions/README.md b/haskell/01-expressions/README.md index dec9b5d..c390fab 100644 --- a/haskell/01-expressions/README.md +++ b/haskell/01-expressions/README.md @@ -54,6 +54,14 @@ Expression | Type (:t) | Value | Comment ## Tuples +Expression | Type (:t) | Value | Comment +--- | --- | --- | -------- +`(1, 2)` | `(Num a, Num b) => (a, b)` | `(1, 2)` | +`(1, "hello", True)` | `(Num a => (a, String, Bool)` | `(1, "hello", True)` | +`fst (1, 2)` | `Num a => a` | `1` | +`snd (1, 2)` | `Num b => b` | `2` | +`(,) 1 2` | `Num a => a -> a -> (a, a)` | `(1, 2)` | + ## Strings ## Control Structures From 08065437a747cf6311d9a99f06a6fcd4cd961758 Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Wed, 20 Aug 2025 15:03:14 +0200 Subject: [PATCH 5/9] Strings --- haskell/01-expressions/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/haskell/01-expressions/README.md b/haskell/01-expressions/README.md index c390fab..be76606 100644 --- a/haskell/01-expressions/README.md +++ b/haskell/01-expressions/README.md @@ -64,6 +64,12 @@ Expression | Type (:t) | Value | Comment ## Strings +Expression | Type (:t) | Value | Comment +--- | --- | --- | -------- +`"hello" ++ " world"` | `String` | `"hello world"` | String concatenation +`length "hello"` | `Int` | `5` | Length of the string +`null ""` | `Bool` | `True` | Checks if the string is empty + ## Control Structures ## Lists \ No newline at end of file From 2c634800d29786467d3d30d5ac340b6af27e1601 Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Wed, 20 Aug 2025 15:03:30 +0200 Subject: [PATCH 6/9] Control Structures --- haskell/01-expressions/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/haskell/01-expressions/README.md b/haskell/01-expressions/README.md index be76606..1d6bc21 100644 --- a/haskell/01-expressions/README.md +++ b/haskell/01-expressions/README.md @@ -72,4 +72,10 @@ Expression | Type (:t) | Value | Comment ## Control Structures +Expression | Type (:t) | Value | Comment +--- | --- | --- | -------- +`if 10/2==5 then "five" else "something else"` | `String` | `"five"` | +`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 \ No newline at end of file From 3b8b1c369e70f9c509e00646f16fb3f2ebc1c9e8 Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Wed, 20 Aug 2025 15:03:58 +0200 Subject: [PATCH 7/9] Lists --- haskell/01-expressions/README.md | 47 +++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/haskell/01-expressions/README.md b/haskell/01-expressions/README.md index 1d6bc21..2608428 100644 --- a/haskell/01-expressions/README.md +++ b/haskell/01-expressions/README.md @@ -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 \ No newline at end of file +## 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)]` From 918af522d638c8bd18fdcc3a5fc41bb5122c9e68 Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Fri, 7 Nov 2025 16:27:41 +0100 Subject: [PATCH 8/9] fix value --- haskell/01-expressions/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/haskell/01-expressions/README.md b/haskell/01-expressions/README.md index 2608428..d995c24 100644 --- a/haskell/01-expressions/README.md +++ b/haskell/01-expressions/README.md @@ -7,7 +7,7 @@ The following illustrates some haskell basics. It can be reproduced using `ghci` Expression | Type (:t) | Value | Comment --- | --- | --- | -------- `2` | `Num a => a` | `2` | -`2.03` | `Fractional a => a` | `2.3` | +`2.03` | `Fractional a => a` | `2.03` | `'c'` | `Char` | `'c'` `"hallo"` | `String` | `"hallo"` | `True` | `Bool` | `True` | From 8da037815374cf67ba420674f450bafac666d276 Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Fri, 7 Nov 2025 18:17:56 +0100 Subject: [PATCH 9/9] const binary function --- haskell/01-expressions/README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/haskell/01-expressions/README.md b/haskell/01-expressions/README.md index d995c24..ce20458 100644 --- a/haskell/01-expressions/README.md +++ b/haskell/01-expressions/README.md @@ -50,7 +50,10 @@ Expression | Type (:t) | Value | Comment `uncurry` | `(a -> b -> c) -> (a, b) -> c`| | `` (`div` 4) `` | `Integral a => a -> a` | | **Sectioning** binds the **second** argument of infix functions; Similar `(/8),(*8),(+8),(-8)` | ``divSomethingBy4 = (`div` 4)`` | `Integral a => a -> a` | | -`divSomethingBy4 8` | `Integral a => a` | `2` | +`divSomethingBy4 8` | `Integral a => a` | `2` | +`const` | `a -> b -> a` | | +`const 8` | `Num a => b -> a` | | function that ignores the input and always returns 8 +`const 8 4` | `Num a => a` | `8` | ## Tuples