# Day 3: Haskell

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.*

## Things I’m learning

- 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 -> Integer

fact 0 = 1

fact n = n * fact(n-1)

fact1 :: Integer -> Integer

fact1 n = if n <= 1 then 1 else n*fact1(n-1)

fact2 :: Integer -> Integer

fact2 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.

- Basic List Addition function

sum1 [] = 0

sum1 (x:xs) = x + sum xssum2 xs = foldr (*) 1 xs

The first one is basic, however the second one is what is suggested by Haskell (or some extension I installed. Can’t tell, no difference).

*Note: Learning Haskell is kind of a pain in the ass, but I must admit that my mind is already racing to stuff I can do cleanly. My college assignments come to mind, and so does my number theory class. I wanted to write a program to calculate that stuff, but I must admit that I felt defeated initially. Looks like I’ll have another go at it.*