Shylock Hg

My own blog powered by Hugo and Ivy.

Haskell Types

2018-08-25


Atomic Types

Combination Types

List

List comprehension :

It’s same as mathematic set comprehension(but list is sequence), such as $$S={2*x | x \in \mathcal{N}, x \leqslant 10}$$, it could also be expressed as [x*2 | x <- [1..10]] in Haskell(But it’s sequence but set).

With condition :

[x*2 | x <- [1..10], x*2 >= 12]

With multi-conditions :

[x*2 | x <- [1..10], x*2 >= 12, x*2 <= 16]

Conditional Generator :

boomBangs xs = [if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]

With multi-factor :

let nouns = ["hobo","frog","pope"]
let adjectives = ["lazy","grouchy","scheming"]
[adjective ++ " " ++ noun | adjective <- adjectives , noun <- nouns]

With multi-dimensions :

let xxs = [[1,3,5,2,3,1,2,4,5],[1,2,3,4,5,6,7,8,9],[1,2,4,2,1,6,3,1,3,2,3,6]]
[ [x | x <- xs, even x] | xs <- xxs ]

Tuple