Skip to content

Instantly share code, notes, and snippets.

@Bubblesphere
Last active April 14, 2018 18:37
Show Gist options
  • Save Bubblesphere/03de068cbb4e09693bd181d2a83f40d5 to your computer and use it in GitHub Desktop.
Save Bubblesphere/03de068cbb4e09693bd181d2a83f40d5 to your computer and use it in GitHub Desktop.
-- Nouveau nom pour type existant
type String = [Char] type Pos3D = (Float,Float,Float)
-- Type entièrement nouveau
data Bool = False | True
data Shape = Circle Float Float Float |
Rectangle Float Float Float Float
-- Pattern Matching -> fnc polymorphique
area :: Shape -> Float
area (Circle _ _ r) = pi * r ^ 2
area (Rectangle x1 y1 x2 y2) = (abs $ x2 - x1) * (abs $ y2 - y1)
-- Afficher sous forme de string
data Shape = Circle Float Float Float |
Rectangle Float Float Float Float
deriving(Show)
data Maybe a = Nothing | Just a
safehead :: [a] -> Maybe a
safehead [] = Nothing
safehead x = Just (head x)
module Shapes
( Point(..)
, Shape(..)
) where
data Person = Person { firstName :: String
, lastName :: String
, age :: Int
, height :: Float
, phoneNumber :: String
, flavor :: String } deriving (Show)
let frank = Person {firstName="Frank", lastName="Smith",
phoneNumber="543-9876", flavor="coffee", age=99, height=1.72}
data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show)
x // 0
| x > 0 = Left INF_P
| x < 0 = Left INF_N
| otherwise = Left NAN
x // y = Right (x/y)
> 5//6 —> Right 0.8333333333333334
> 5//0 —> Left INF_P
> 0//0 —> Left NAN
-- type recursif
data Nat = Zero | Succ Nat deriving(Show)
data List a = Empty | Cons a (List a)
deriving (Show, Read, Eq,Ord) -- list
-- création/insertion
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
singleton :: a -> Tree a
singleton x = Node x EmptyTree EmptyTree
treeInsert :: (Ord a) => a -> Tree a -> Tree a
treeInsert x EmptyTree = singleton x
treeInsert x t@(Node a left right)
| x == a = t
| x < a = Node a (treeInsert x left) right
| x > a = Node a left (treeInsert x right)
-- arbre binaire
ghci> let nums = [8,6,4,1,7,3,5]
ghci> let numsTree = foldr treeInsert EmptyTree nums
ghci> numsTree
Node 5 (Node 3 (Node 1 EmptyTree EmptyTree) (Node 4 EmptyTree
EmptyTree)) (Node 7 (Node 6 EmptyTree EmptyTree) (Node 8 EmptyTree
EmptyTree))
-- classe de type
class Eq a where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool
x == y = not (x /= y) -- il n’est pas obligatoire de donner une implémentation
x /= y = not (x == y)
-- donné partie d'une classe
data TrafficLight = Red | Yellow | Green
instance Eq TrafficLight where
Red == Red = True
Green == Green = True
Yellow == Yellow = True
_ == _ = False
instance Show TrafficLight where
show Red = "Red light"
show Yellow = "Yellow light"
show Green = "Green light”
-- struct type f a pour produire f b
class Functor f where
fmap :: (a -> b) -> f a -> f b
instance Functor [] where
fmap = map
ghci> :t map
map :: (a -> b) -> [a] -> [b] —- ici [] a est équivalent à [a]
instance Functor Maybe where
fmap _ Nothing = Nothing
fmap g (Just x) = Just (g x)
instance Functor Tree where
fmap _ EmptyTree = EmptyTree
fmap g (Node x left right) = Node (g x) (fmap g left) (fmap g right)
> fmap (*2) EmptyTree
EmptyTree
> fmap (*4) (foldr treeInsert EmptyTree [5,7,3])
Node 20 (Node 12 EmptyTree EmptyTree) (Node 28 EmptyTree EmptyTree)
data Either a b = Left a | Right b
instance Functor (Either a) where
fmap _ (Left x) = Left x
fmap g (Right x) = Right (g x)
-- concatenation
++
head [1,2,3]
> 1
tail [1,2,3]
> 3
5:[1,2]
> [5,1,2]
length, reverse, take # [], drop # [], minimum
maximum, sum [], product [], repeat, replicate
[x*2|x<-[1..10], x*2>10]
-- tuples
fst () -- first
snd () -- second
zip [1,2,3] [5,5,5]
> [(1,5),(2,5),(3,5)]
Integer, Float, Double, Bool, Char, Tuples, Eq,
Ord, Num, Integral, Int
--hof
max 4 5 -- (Ord a) => a -> a -> a
(max 4) 5 -- (Ord a) => a -> a
[] -- empty
(x:[]) -- 1 elem
(x:y:[]) -- 2 elem
(x:y:_) -- + elem
let a = 1
in a + 1
(x:xs) -- x: first xs: remaining
bmiTell bmi
| bmi <= 18.5 = "a"
| otherwise = "b"
where bmi = w/h^2
-- lambda
filter(\xs -> length xs > 15)
case expression of pattern -> result
pattern -> result
flip::(a->b->c)->(b->a->c)
flip f = g
where gxy = fyx
map (+3) [1..5]
filter (>3) [1..5]
takeWhile cond []
$ -- parenthese
zipWith::(a->b->c)->[a]->[b]->[c]
zipWith _ [] _ = []
zipWith _ _ [] = []
zipWith f (x:xs) (y:ys) = f x y : zipWIth f xs ys
zipWith (+) [4,2,5] [2,6,2]
> [6,8,7]
-- z: acc f: fnc
foldl (+) 0 [3,4,5,6]
>(+)3((+)4((+)5((+)6 0)))
-- foldr will only work on infinite list when the binary
-- function that we're passing to it doesn't alway need
-- to evaluate its second param to give us some sort of answer
-- fnc composition
xSquaredPlusOne = inc . sqr
xSquaredPlusOne x = (inc . sqr) x
xSquaredPlusOne x = inc(sqr x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment