introdução à programação: uma abordagem funcional pd i – engenharia elétrica prof.ª claudia...
TRANSCRIPT
![Page 1: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/1.jpg)
Introdução à Programação: uma Abordagem Funcional
PD I – Engenharia ElétricaProf.ª Claudia Boeres
2008/2
![Page 2: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/2.jpg)
RESOLVENDO PROBLEMAS – Os movimentos do cavalo no xadrez
Considere o jogo de xadrez, onde peças são movimentadas em um tabuleiro dividido em 8 linhas e oito colunas. Considere ainda os movimentos do cavalo, a partir de uma dada posição, conforme diagrama a seguir, onde cada possível movimento é designado por mi. No esquema, o cavalo localizado na posição (5, 4) pode fazer oito movimentos, onde o primeiro deles, m1, levaria o cavalo para a posição (7,5).
![Page 3: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/3.jpg)
Os movimentos do cavalo
8
7
6 m3 m2
5 m4 m1
4 C
3 m5 m8
2 m6 m7
1
1 2 3 4 5 6 7 8
![Page 4: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/4.jpg)
PROBLEMA 1
Escreva uma função que determina se, a partir de uma dada posição, o cavalo pode ou não realizar o primeiro movimento. Vamos chamá-la de pmov, e denominar seus parâmetros (a posição corrente), de x e y.
![Page 5: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/5.jpg)
Instâncias de pmov
INSTÂNCIA RESULTADO
pmov 5 4 True
pmov 8 1 False
pmov 1 1 True
pmov 1 8 False
![Page 6: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/6.jpg)
Solução
pmov x y=
(x + 2 <= 8 ) && (y + 1 <= 8)
![Page 7: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/7.jpg)
Testando a solução
8
7
6
5
4
3
2
1
1 2 3 4 5 6 7 8
![Page 8: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/8.jpg)
Estendendo o problema
pmov x y = (x + 2 <= 8 ) && (y + 1 <= 8)
smov x y = (x + 1 <= 8 ) && (y + 2 <= 8)
tmov x y = (x - 1 >= 1 ) && (y + 2 <= 8)
qmov x y = (x - 2 >= 1 ) && (y + 1 <= 8)
qtmov x y = (x - 2 >=1 ) && (y - 1 >= 1)
sxmov x y = (x - 1 >= 1 ) && (y - 2 >= 1)
stmov x y = (x + 1 <= 8 ) && (y - 2 >= 1)
omov x y = (x + 2 <= 8 ) && (y - 1 >= 1)
![Page 9: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/9.jpg)
Identificando abstrações
pmov x y = f (x + 2 ) && f( y + 1)
smov x y = f (x + 1) && f (y + 2)
tmov x y = g (x - 1) && f (y + 2)
qmov x y = g (x - 2) && f (y + 1)
qtmov x y = g (x - 2) && g (y – 1)
sxmov x y = g (x - 1) && g (y – 2)
stmov x y = f (x + 1 ) && g (y – 2)
omov x y = f (x + 2) && g (y – 1)
f w = w <= 8
g w = w >= 1
![Page 10: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/10.jpg)
Análise da solução
Clareza - Na medida em que agora está explicitado, que todas as oito funções para verificar os movimentos possuem estrutura semelhante e que todas estão usando funções para verificar a ultrapassagem das bordas;
Manutenção - Se nosso tabuleiro mudasse, ou seja, passasse a ter 9 linhas por nove colunas, bastaria alterar a função f e tudo estaria modificado, ao invés de termos que alterar as oito definições.
Reuso - As duas funções que testam as bordas poderiam ser usadas para construir funções para avaliar o movimento de outras peças do jogo de xadrez.
![Page 11: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/11.jpg)
PROBLEMA 2
Sabemos que para cada posição alguns movimentos podem ser realizados e outros não. Como ordenamos os movimentos no sentido anti-horário, gostaríamos de obter, para uma dada posição, dos movimentos que podem ser realizados, aquele que possui o menor índice.
![Page 12: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/12.jpg)
Qual o menor índice de movimento possível?
8 m4 m1 C1
7 C3 m5
6 m5 m8 m6
5 m6 m7
4
3 m2 m3
2 m1 m4
1 C4 C2
1 2 3 4 5 6 7 8
![Page 13: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/13.jpg)
Instâncias de qualmov
Instância resultado
qualmov 8 1 3
qualmov 8 8 5
qualmov 3 7 1
qualmov 1 1 1
![Page 14: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/14.jpg)
Solução
![Page 15: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/15.jpg)
Codificando a solução
qualmov x y = if pmov x y then 1 else if smov x y then 2 else if tmov x y then 3 else if qmov x y then 4 else if qtmov x y then 5 else if sxmov x y then 6 else if stmov x y then 7 else if omov x y then 8 else 0
![Page 16: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/16.jpg)
Análise da solução
qualmov x y = if not (pert x 1 8) || not (pert y 1 8) then 0else if pmov x y then 1
else if smov x y then 2
else if tmov x y then 3
else if qmov x y then 4
else if qtmov x y then 5
else if sxmov x y then 6
else 7wherepert x a b = a = b && x >= a && x <= b
![Page 17: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/17.jpg)
REVISITANDO O PROBLEMA 1
Observando a solução encontrada para o problema 1, constatamos que embora a noção de movimento do cavalo seja única, quem precisar saber se um dado movimento é válido, precisará conhecer o nome das oito funções. Embora seja cedo para falarmos de interface homem-máquina, já dá para dizer que estamos sobrecarregando nosso usuário ao darmos oito nomes para coisas tão parecidas. Será que temos como construir uma só função para tratar o problema?
![Page 18: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/18.jpg)
Melhorando a interface
pmov x y
smov x y
tmov x y
qmov x y
qtmov x y
sxmov x y
stmov x y
omov x y
mov m x y
![Page 19: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/19.jpg)
Exemplo de avaliação da função mov
? mov 7 3 4True?
![Page 20: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/20.jpg)
Codificação da Solução
mov m x y = if not (pert m 1 8) || not (pert x 1 8) || not (pert y 1 8) then False else if m == 1 then pmov else if m == 2 then smov else if m == 3 then tmov else if m == 4 then qmov else if m == 5 then qtmov else if m == 6 then sxmov else if m == 7 then stmov else omov where pmov = ... smov = ... tmov = ... ...
![Page 21: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/21.jpg)
Análise da solução nova solução
<1 1 2 3 4 5 6 7 8 >8
<1 1 2 3 4 5 6 7 8 >8
<1 1 2 3 4 5 6 7 8 >8
![Page 22: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/22.jpg)
Codificação da solução
mov m x y = if not (pert m 1 8)|| not(pert x 1 8) || not(pert y 1 8) then False else if m <= 4 then if m<= 2 then if m== 1 then pmov else smov else if m==3 then tmov else qmov else if m<= 6 then if m==5 then qtmov else sxmov else if m == 7 then stmov else omov where pmov = ... smov = ... tmov = ...
...
![Page 23: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/23.jpg)
Número de comparações
número de valores
esquema linear (número médio)
esquema binário(número máximo)
8 4 4
100 50 7
1000 500 10
1000000 500000 20
![Page 24: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/24.jpg)
Exemplo da aula anterior
y1
y2
B 1
5
3 E
x1 x2
F
A
DC
O ponto P(x,y) pertence a qual região?
![Page 25: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/25.jpg)
Solução
qualRegiao x y x1 y1 x2 y2 = if pertenceLosango x y a1 b1 a2 b2 then 'E' else if pertenceRetangulo x y x1 y1 x2 y2 then if x < xc then if y > yc then 'B' else 'C' else if y > yc then 'A' else 'D' else 'F' where
a1 = x1 b1 = (y1 + y2)/2 a2 = (x1 + x2)/2 b2 = y2 xc = a2 yc = b1
![Page 26: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/26.jpg)
Continuação do script
pert x a b = a <= b && x >=a && x <= b
pertenceRetangulo x y a b c d = pert x a c && pert y b d
pertenceLosango x y a b c d = acimaReta x y a b e f &&
acimaReta x y e f g h &&
abaixoReta x y a b c d &&
abaixoReta x y c d g h
where
e = c
f = 2*b-d
g = 2*c-a
h = b
![Page 27: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/27.jpg)
Final do script
acimaReta x y a b c d = y >= m * (x-a) + b
where
m = (d-b)/(c-a)
abaixoReta x y a b c d = y <= m * (x-a) + b
where
m = (d-b)/(c-a)
![Page 28: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/28.jpg)
Quais instâncias escolher?
1
4
1 5
P1
P2
F
A
DC
E
B
![Page 29: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/29.jpg)
Quais valores devemos escolher?
x y resultado esperado
resultado obtido
diagnóstico
3 3 ‘E’ ‘E’ ok
1 2.5 ‘B’ ‘B’ ok
2 4 ‘B’ ‘B’ Ok
1 5 ‘F’ ‘F’ ok
Pontos representantes de classes de equivalência
![Page 30: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/30.jpg)
Processo de teste
![Page 31: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/31.jpg)
Exercício : faça um script para determinar se um ponto pertence à area cinza. Faça um plano de teste.
r r r r
C
![Page 32: Introdução à Programação: uma Abordagem Funcional PD I – Engenharia Elétrica Prof.ª Claudia Boeres 2008/2](https://reader035.vdocuments.net/reader035/viewer/2022062512/552fc0f8497959413d8b4dc6/html5/thumbnails/32.jpg)
O ponto P (x, y) pertence a qual das áreas 1, 2, 3 e 4?
P (x, y) ?
a
a/2
C (x1, y1)
1
3
2
4