Skip to content

Instantly share code, notes, and snippets.

@HenriTEL
Last active July 27, 2018 11:24
Show Gist options
  • Save HenriTEL/209d80ce7da755c54eb6e5ed292c5113 to your computer and use it in GitHub Desktop.
Save HenriTEL/209d80ce7da755c54eb6e5ed292c5113 to your computer and use it in GitHub Desktop.
Haskell reminder

Haskell reminder

A bunch of True (when relevant) haskell statements.

General
Flow control
Data structures
Conversions
Modules and types definition
Ghci
Usefull links

General

import Prelude hiding (foldl)
import qualified Data.List as L (nub, sort)
-- comment
print "hello world"
interact -- read stdin
(sqrt $ 3 + 4) == sqrt (3 + 4)
(negate . sum) [1,2] == negate (sum [1,2])

isAlphaNum = (`elem` ['A'..'z'])
compare100 = compare 100
reverse' = foldl (\acc x -> x : acc) [ ]

Flow control

if True then True else False
not (1 /= 1)
even :: Int -> Bool
even n
    | n `rem` 2 == 0 = True
    | otherwise         = False

cylinderArea r h =
    let sideArea = 2 * pi * r * h
        topArea = pi * r ^2
    in  sideArea + 2 * topArea

cylinderArea r h = sideArea + 2 * topArea
    where sideArea = 2 * pi * r * h
               topArea = pi * r ^2

Data structures

True, False
minbound :: int == -2147483648

tuple = (1,2)
fst (1,2) == 1
snd (1,2) == 2

list = [1, 2]
[1..3] == [1,2,3]
[x*2 | x <- [0..3], x > 0] == [2,4,6]
1:[2,3] == [1,2,3]
[1,2] ++ [3,4] == [1,2,3,4]
take 3 [1,2,3,4] == [1,2,3]
drop 2 [1,2,3] == [3]
length [1,2] == 2
head [1,2] == 1
last [1,2] == 2
tail [1,2,3] == [2,3]
init [1,2,3] == [1,2]
null [ ]
[4,5,6] !! 1 == 5
reverse [1,2] == [2,1]
maximum [1,2] == 2
product [1,2] == 2
sum [1,2] == 3
1 `elem` [1,2]
[0,5..13] == [0,5,10]
take 2 $ cycle [1] == [1,1]
replicate 2 1 == [1,1]
take 2 $ repeat 1 == [1,1]
zipWith (+) [1,2] [3,4] == [4,6]
takeWhile (/=3) [1,2,3,4] == [1,2]
all even [2,4]
any even [1,2]

Conversions

show 10 == "10"
(read "5" :: Float) == 5.0
(fromIntegral 1 :: Float) == 1.0
zip [1,2] ['a', 'b'] == [(1,'a'),(2,'b')]
words "a b" == ["a","b"]
unlines ["a","b"] == "a\nb\n"

Modules and types definition

In Gemometry/Shapes.hs :

module Shapes
( Point(..)
, Shape(..)
, surface) where
data Shape = Circle Point Float | Rectangle Point Point
data Point = Point { x :: Float
                   , y :: Float
                   } deriving (Show)
-- recurcive definition (with :-: operator defintion)
infixr 5 :-:
data List' a = Empty | a :-: (List' a) deriving (Show, Read, Eq, Ord)

surface :: Shape -> Float
surface (Circle _ r) = pi * r ^ 2
surface (Rectangle (Point x1 y1) (Point x2 y2)) = (abs $ x2 - x1) * (abs $ y2 - y1)

-- type alias for [Char]
type String = [Char]

instance Eq Point where
    Red == Red = True
    Green == Green = True
    Yellow == Yellow = True
    _ == _ = False

class Empty a where
    empty :: a -> Bool
    filled x = not $ empty x

Ghci

:m + Data.List  
:l file.hs  
:t even

Usefull links

HTTP server library
Web services
Data structures and algorithms
Monadic parsing
Concurrent communication
Process monitoring
JSON

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment