Created
June 30, 2017 21:37
-
-
Save v3c70r/f3a83c9b02241a97cd403ba95971c46c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- list operators | |
-- head | |
-- tail | |
-- last | |
-- init | |
-- length | |
-- null | |
-- reverse | |
-- take | |
-- drop | |
-- maximum | |
-- minimum | |
-- sum | |
-- product | |
-- elem | |
-- | |
-- Range | |
-- [1..10], ['a'..'z'] | |
-- Range with step | |
-- [2, 1..10] | |
-- | |
-- cycle | |
-- repeat | |
-- | |
-- Tuples operators | |
-- fst (pairs) | |
-- snd (pairs) | |
-- zip | |
-- | |
-- Type Classes | |
-- Int (ranged) | |
-- Integer (unlimited) | |
-- Flot | |
-- Double | |
-- Bool | |
-- Char | |
-- | |
-- Pattern matching | |
isHotdog :: (Integral a) => String->String | |
isHotdog "Hotdog" = "Hotdog" | |
isHotdog x = "Not hotdog" | |
dotVec2 :: (Num a)=>(a,a)->(a,a)->a | |
dotVec2 (x1,y1) (x2,y2) = x1*x2 + y1*y2 -- It matches pairs | |
-- Matching on lists | |
tell :: (Show a) => [a] -> String | |
tell [] = "Empty" | |
tell [x] = "One elem " ++ show x | |
tell [x, y] = "two elems " ++ show x ++ show y | |
tell (x:y:_) = "more than two" | |
-- Guards, like switch | |
max' :: (Ord a)=>a->a->a | |
max' a b | |
| a > b = a | |
| otherwise = b | |
compare' :: (Ord a) => a->a->Ordering | |
compare' a b | |
| a > b = GT | |
| a == b = EQ | |
| otherwise = LT | |
-- Where | |
bmiTell :: (RealFloat a) => a -> a -> String | |
bmiTell weight height | |
| bmi <= skinny = "You're underweight, you emo, you!" | |
| bmi <= normal = "You're supposedly normal. Pffft, I bet you're ugly!" | |
| bmi <= fat = "You're fat! Lose some weight, fatty!" | |
| otherwise = "You're a whale, congratulations!" | |
where bmi = weight / height ^ 2 | |
skinny = 18.5 | |
normal = 25.0 | |
fat = 30.0 | |
-- let ... in .. | |
-- | |
-- case expression of pattern -> result | |
-- pattern -> result | |
-- pattern -> result | |
maximum' :: (Ord a)=>[a]->a | |
maximum' [] = error "Empty list" | |
maximum' [x] = x | |
maximum' (x:xs) = | |
if x > maximum' xs | |
then x else maximum' xs | |
replicate' :: (Num i, Ord i)=>i -> a -> [a] | |
replicate' n x | |
| n == 0 = [] | |
| otherwise = x:replicate' (n-1) x | |
take' :: (Num i, Ord i)=>i->[a]->[a] | |
take' n _ | |
| n <= 0 = [] | |
take' _ [] = [] | |
take' n (x:xs) = | |
x:take' (n-1) xs | |
reverse' :: [a]->[a] | |
reverse' [] = [] | |
reverse' [x] = [x] | |
reverse' (x:xs) = reverse' xs ++ [x] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment