Escolar Documentos
Profissional Documentos
Cultura Documentos
1 Aula Apresentação
a
Pedro Vasconcelos
DCC/FCUP
2010
Na web. . .
1 Learn you a Haskell for great good!,
http://learnyouahaskell.com/
2 Real World Haskell,
http://book.realworldhaskell.org/
É um paradigma de programação
Preconiza a computação como a aplicação de funções
matemáticas a valores imutáveis (dados)
Por contraposição: no paradigma imperativo a computação é
vista como a execução de instruções que mudam variáveis
(estado)
Podemos programar num estilo (quase) funcional em muitas
linguagens. . .
Linguagens funcionais suportam melhor o paradigma funcional
Exemplos: Scheme, ML, Erlang, Haskell, F#
total = 0;
for (i=1; i<=10; ++i)
total = total + i;
21 total=total+i 10 55
22 ++i 11 55
sum [1..10]
sum [1..10] =
= sum [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] =
= 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 =
= 55
$ ghci
GHCi, version 6.8.3: http://www.haskell.org/ghc/
Loading package base ... linking ... done.
Prelude>
> 2+3*5
17
> (2+3)*5
25
== igualdade
/= diferença
< > <= >= relações de ordem
Haskell Matemática
f x f (x )
f (g x) f (g (x ))
f (g x) (h x) f (g (x ), h(x ))
f x y + 1 f (x , y ) + 1
f x (y+1) f (x , y + 1)
√
sqrt x + 1 x +1
√
sqrt (x + 1) x +1
(+) x y = x+y
(*) y 2 = y*2
x`mod`2 = mod x 2
f x `div` n = div (f x) n
$ ghci
...
> :load teste.hs
[1 of 1] Compiling Main ( teste.hs, interpreted )
Ok, modules loaded: Main.
> dobro 2
4
> quadruplo 2
8
> :reload
...
> factorial 10
3628800
> media 2 3
2.5
a = b+c
where b = 1
c = 2
d = a*2
a = b+c
where {b = 1;
c = 2}
d = a*2
b = 2 b = 2 b = 2
c = 3 c = 3 c = 3
ERRADO ERRADO OK
A ordem das denições não é relevante.