Coding just likes Art
This is the blog about the coding, the whole coding and nothing but the coding , so God would help me become a better developer.

Learning Haskell (WIP)

1 Environment Install

1.1 Dependencies

Glasgow Haskell Compiler (GHC) - The compiler for Haskell. Cabal - Package description format. Stack - Package management tool.

1.2 Get Start

brew update # upgrade HomeBrew
brew install haskell-stack # install package management tool Stack
stack new # download Haskell runtime environment

1.3 Emacs Haskell-mode

M-x package-refresh-contents
M-x package-install RET haskell-mode
(setq haskell-program-name "GHCI_PATH")

1.4 Haskell Test

putStrLn "Hello World"
92 `div` 13

2 Haskell Features

  • Referential Transparency
  • Type Inference
  • Lazy
  • Statically Typed

3 Basic Rules

3.1 Define and Evaluate Function

let f x = 1 + x -- Same as define ~f~ in a file and load it using :l
f 2 -- 3

3.2 If Expression

let double'Small'Num x = (if x > 100 then x else 2 * x) + 1
double'Small'Num 101 -- 102

3.3 List

1 : [2,3] ++ [4,5] ++ [] -- [1,2,3,4,5]
('H' : "ello" ++ " " ++ "World") !! 1 -- 'e'
[3,2,1] <= [3,2,2] -- True
let l = [1,2,3,4]
(tail l) ++ [head l] -- [4,1,2,3]
(last l) : (init l) -- [4,1,2,3]
length l -- 4
null [] -- True
reverse l -- [4,3,2,1]
take 2 l -- [1,2]
drop 2 l -- [3,4]
maximum l -- 4
sum l -- 10
product l -- 24
5 `elem` l -- False

3.4 Range

[1..4] -- [1,2,3,4]
['a'..'d'] -- "abcd"
[1,3..7] -- [1,3,5,7]
[13,26..4*13] -- [13,26,39,52]

3.5 List Comprehension

[ x*2 | x<-[1..3], x/=2 ] -- [2,6]
let xxs = [[1..3], [2..4], [3..5]]
[[x | x <- xs, even x] | xs <- xxs] -- [[2], [2,4], [4]]
[x+y | x <- [1..3], y <- [2..5]] -- [3,4,5,6,4,5,6,7,5,6,7,8]

3.6 Tuple