Академический Документы
Профессиональный Документы
Культура Документы
PARADIGMAS DE P ROGRAMAÇÃO 2
P ROGRAMAÇÃO F UNCIONAL
Imperativo × Funcional
total = 0;
for (int i=1; i<=10; i++) sum [1..10]
total = total + i;
P ROGRAMAÇÃO F UNCIONAL 3
A LINGUAGEM H ASKELL
➜ Breve histórico:
• Lisp (John McCarthy) - fim dos anos 1950
• Scheme (Rees e Clinger) - ***
• ML (Robin Milner) - ***
• Miranda (David Turner - fim dos anos 1970
• Haskell (Paul Hudak, Philip Wadler, Simon Peyton-Jones) - 1980s
• Haskell98 (padrão) - 1999
➜ Informações: www.haskell.org
A LINGUAGEM H ASKELL 4
TÓPICOS DO CURSO :
➜ Conceitos básicos de programação funcional
➜ O interpretador Hugs
➜ Tipos básicos
➜ Definições de funções
➜ Tuplas e listas
➜ Polimorfismo
➜ Prova de propriedades de programas
➜ Funções de ordem superior e padrões de computação
➜ Mais sobre funções: aplicação parcial, composição etc
➜ Sobrecarga
➜ Inferência de Tipos
➜ Tipos de dados algébricos
➜ Tipos abstratos
➜ Programação lazy
➜ Mônadas e Entrada e Saída
TÓPICOS DO CURSO : 5
B IBLIOGRAFIA
B IBLIOGRAFIA 6
H UGS
➜ Um interpretador para a linguagem Haskell.
➜ Um sistema interativo, adequado para o ensino e para prototipagem
➜ Disponível em www.haskell.org/hugs
➜ Uma sessão no Hugs:
• Quando o Hugs é iniciado, ele primeiramente carrega o arquivo
Prelude.hs, que constitui uma biblioteca de funções e tipos
comumente usados em programas Haskell.
• Em seguida, ele repetidamente apresenta um prompt para
entrada de uma expressão a ser avaliada:
> 2+3*4
14
> (2+3)*4
20
H UGS 7
E XPRESSÕES E AVALIAÇÃO
-bb-error = =
E XPRESSÕES E AVALIAÇÃO 8
P RIMEIRO P ROGRAMA
{-
Exemplo01.hs
Funções simples sobre valores inteiros
-}
module Exemplo01 where
square :: Int → Int
square x = x * x
quad :: Int → Int
quad x = square (square x)
fact :: Int → Int
fact 0 = 1
fact n = n * fact (n-1)
P RIMEIRO P ROGRAMA 9
U SANDO O H UGS
U SANDO O H UGS 10
C OMANDOS DO H UGS
:l nomearq Carrega o arquivo nomearq.hs
não precisa incluir a extensão do nome do arquivo (.hs ou .lhs)
C OMANDOS DO H UGS 11
AVALIAÇÃO DE E XPRESSÕES
➜ Avaliação lazy :
square (2+3) B (2+3) * (2+3) B 5 * 5 B 25
➜ Avaliação estrita:
square (2+3) B square 5 B 5 * 5 B 25
loop = 1 + loop
select1 x y = x
➜ Avaliação lazy :
square (select1 (2+3) loop)
B (select1 (2+3) loop ) * (select1 (2+3) loop)
B (2+3) * (2+3) B 5 * 5 B 25
➜ Avaliação estrita:
square (select1 loop (2+3)) B ???
AVALIAÇÃO DE E XPRESSÕES 12
AVALIAÇÃO DE EXPRESSÕES
fact 5
B 5 * fact (5-1)
B 5 * fact 4
B 5 * 4 * fact (4-1)
B 5 * 4 * fact 3
B 5 * 4 * 3 * fact (3-1)
B 5 * 4 * 3 * fact 2
B 5 * 4 * 3 * 2 * fact (2-1)
B 5 * 4 * 3 * 2 * fact 1
B 5 * 4 * 3 * 2 * 1 * fact (1-1)
B 5 * 4 * 3 * 2 * 1 * fact 0
B 5 * 4 * 3 * 2 * 1 * 1
B 120
AVALIAÇÃO DE EXPRESSÕES 13