Jens Petersen (juhp)
FUDCon APAC at Phnom Penh 2016 Nov 5
Please install hugs98
or ghc
in your Linux distro.
Or download from:
https://www.haskell.org/hugs/ (small, old)
https://www.haskell.org/ghc/ (big, new)
Alternatively: http://tryhaskell.org/ (online limited cli)
Today we will use hugs98 or ghc for some parts.
Hugs is old but small!
sudo apt install hugs
or
sudo dnf install hugs98
Installers for Windows and Mac
if you can’t install hugs or ghc:
https://petersen.fedorapeople.org/talks/fudcon-apac-2016/
generated with pandoc and reveal.js
from https://github.com/juhp/presentations/tree/master/fudcon2016-apac/
First functional programming language was probably Algol
Mathematical maps:
f: A -> B
Lambda calculus
programming language.
Typed lambda calculus
⇓
⇓
designed by a committee of CS academics
in the late 80’s
to create a standard lazy
functional programming language
cabal-install
stack
::
= “has type”
True :: Bool
1 :: Int -- machine integers
2^2^2^2^2 -1 :: Integer -- arbitrary precision
'a' :: Char
"Hello!" :: String
1.0 :: Double
() :: ()
add :: Integer -> Integer -> Integer
add m n = m + n
add 3 2 ⟹ 5 :: Integer
-- partial application
add 5 :: Integer -> Integer
prefix vs infix
2 + 4 ⟹ 6
-- prefix
(+) 2 4 ⟹ 6
div 4 2 ⟹ 2
-- infix
4 `div` 2 ⟹ 2
[a]
is a list of elements of type a
The type variable a
is an example of polymorphism.
-- empty list
[] :: [a]
[True, False, True] :: [Bool]
[1..5] :: [Int]
["Haskell", "is", "good"] :: [String]
Note ['a', 1, True]
is not a valid Haskell list!
(++) :: [a] -> [a] -> [a]
[1, 2] ++ [3, 4] ⟹ [1, 2, 3, 4]
head :: [a] -> a
tail :: [a] -> [a]
length :: [a] -> Integer
head [1, 2, 3] ⟹ 1
tail [1, 2, 3] ⟹ [2, 3]
length [1, 2, 3] ⟹ 3
length $ show $ 2^2^2^2^2 -1 ⟹ 19729
head
and tail
are not safe functions
head []
tail []
undefined
!
Avoid them!!
a : as
1 : [2, 3] ⟹ [1, 2, 3]
map :: (a -> b) -> [a] -> [b]
map (add 3) [1, 2, 3] ⟹ [4, 5, 6]
What is the type signature of the quicksort
function?
quicksort :: [a] -> [a]
quicksort :: Ord a => [a] -> [a]
quicksort [] = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
where
lesser = filter (< p) xs
greater = filter (>= p) xs
(not for production use)
:t quicksort
Lazy evaluation allows infinite lists:
naturals = [0..]
fib :: Int -> Int
-- naive definition
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
fibs = 0 : 1 : next fibs
where
next (a : t@(b:_)) = (a+b) : next t
let x = 2 in
x*x + 3*x + 1
sumsquare x y = square x + square y
where
square u = u * u
squares = [ x * x | x <- [0..] ]
-- "nested 'for' loop"
pairs = [(x,y) | x <- [1..10], y <- [1..10]]
Python’s comprehensions come from Haskell
primes = filterPrime [2..]
where filterPrime (p:xs) =
p : filterPrime [x | x <- xs, x `mod` p /= 0]
(from http://haskell.org)
(a, b)
is a pair(a, b ,c)
is a triple("Life", 42) :: (String, Int)
(False, 'a', 1.1) :: (Bool, Char, Float)
take :: Int -> [a] -> [a]
take 0 _ = []
take _ [] = []
take n (x:xs) = x : take (n-1) xs
case n of
0 -> "no"
1 -> "one"
otherwise -> "some"
vs
type annotations are generally optional
data Maybe a = Just a | Nothing
data Bool = False | True
-- record types
data Point = Pt Float Float
pt = Pt 1.0 2.0
-- recursive datatype
data Tree a = Leaf a | Branch (Tree a) (Tree a)
putStrLn :: String -> IO ()
getLine :: IO String
do
let greet = "Hello"
name <- getLine
putStrLn $ greet ++ " Your name is " ++ name
import modules with:
import My.Module.Name
Implement simple ls
command in Haskell using:
import System.Directory
Html combinators
Parsers
``` import Text.XHtml
hello :: String hello = showHtml $ p (stringHtml “hi!”)
Scotty
Warp webserver can
“provide performance on a par with nginx”
and “three times faster than Node”.
http://underscorejs.org/ (js)
Contact: petersen@fedoraproject.org (@juhp)