TLDR : Youtube for the win

So I started watching this youtube series on Haskell, and I must admit it’s pretty good. Looks like I found another source to learn from.

• The below code apparently shows us the type of the function. I haven’t been able to replicate it in VSCode, so I’ll check on the terminal later.
`:t (+)`
• Different ways of defining a recursive factorial function.
`fact :: Integer -> Integerfact 0 = 1fact n = n * fact(n-1)fact1 :: Integer -> Integerfact1 n = if n <= 1 then 1 else n*fact1(n-1)fact2 :: Integer -> Integerfact2 n | n <= 1 = 1        | otherwise = n*fact2(n-1)`

I skipped over some usage of a combination of the where clause and guards. I’ll try to implement it later if possible.

• Lists can only have one type (something I figured out through trial and error before).
• Basic List creation function
`produceList n m | m<n = []                | m == n = [m]                | m > n = n : produceList (n+1) m`

Basically, recursively making lists. The semicolon in

`n : []`

basically means that we’re appending the value n to the beginning of the list.

Something like

`1:2:3:4:[]`

is equal to

`[1,2,3,4]`

You’ll also likely get a “Pattern Matches are Inexhaustive” error. Not sure if it’s wrong, but the funtion works anyway for now.

• List functions

Any function that returns a list will return a copy of the list, since data types in haskell are immutable

Head : Returns the first element of the list.

Tail : Returns all the elements of the list other than the first one.

length : Come on.

init : Returns the list without the last element

null : check if list is empty

`head (produceList 1 10)tail (produceList 1 10)length (produceList 1 10)init (produceList 1 10) null (produceList 1 0))`
• List comprehensions!

If you’ve used python, you already know. Nice to see all my knowledge not go to waste.

`sum1 [] = 0sum1 (x:xs) = x + sum xssum2 xs = foldr (*) 1 xs`