• Nenhum resultado encontrado

Programação Funcional BCC222. Aula 2. Avaliação

N/A
N/A
Protected

Academic year: 2021

Share "Programação Funcional BCC222. Aula 2. Avaliação"

Copied!
27
0
0

Texto

(1)

Programação Funcional — BCC222 Aula 2

Avaliação

Lucília Camarão de Figueiredo Departamento de Ciência da Computação

(2)

Parte I

(3)

O que é uma função?

I Uma receita para gerar uma saída a partir de dados de entrada: “Multiplicar um número por ele próprio”

I Um conjunto de pares (entrada, saída): (1,1) (2,4) (3,9) (4,16) (5,25) . . .

I Uma equação

f x= x2

(4)

Operações sobre números

C:\Users\lucilia>ghci

GHCi, version 6.10.4: www.haskell.org/ghc/ :? for help

Loading package ghc-prim ... linking ... done.

Loading package integer ... linking ... done.

Loading package base ... linking ... done.

Prelude>3 + 3

6

Prelude>3 * 3

(5)

Funções sobre números

lect02.hs

square :: Integer -> Integer square x = x * x

pyth :: Integer -> Integer -> Integer pyth a b = square a + square b

(6)

Testando suas funções

C:\Users\lucilia>ghci lect02b.hs

GHCi, version 6.10.4: www.haskell.org/ghc/ :? for help

Loading package ghc-prim ... linking ... done.

Loading package integer ... linking ... done.

Loading package base ... linking ... done.

[1 of 1] Compiling Main ( lect02b.hs, interpreted )

Ok, modules loaded: Main.

*Main> square 3

9

*Main> pyth 3 4

(7)

Mais alguns testes

*Main> square 0 0 *Main> square 1 1 *Main> square 2 4 *Main> square 3 9 *Main> square 4 16 *Main> square (-3) 9 *Main> square 10000000000 100000000000000000000

(8)

Declaração e avaliação

Declaração (file lect02a.hs)

square :: Integer -> Integer square x = x * x

pyth :: Integer -> Integer -> Integer pyth a b = square a + square b

Avaliação

% ghci lect02a.hs

GHCi, version 6.10.4: www.haskell.org/ghc/ :? for help

Loading package ghc-prim ... linking ... done.

Loading package integer ... linking ... done.

Loading package base ... linking ... done.

[1 of 1] Compiling Main ( lect02b.hs, interpreted )

Ok, modules loaded: Main.

*Main> pyth 3 4

25

(9)

Regra de Leibniz

square :: Integer -> Integer square x = x * x

pyth :: Integer -> Integer -> Integer pyth a b = square a + square b

“Equals may be substituted for equals.” – Gottfried Wilhelm Leibniz (1646–1716)

pyth 3 4 = square 3 + square 4 = 3*3 + 4*4 = 9 + 16 = 25

(10)

Operações numéricas são funções

(+) :: Integer -> Integer -> Integer (*) :: Integer -> Integer -> Integer

Main*> 3+4 7 Main*> 3*4 12 3 + 4 é o mesmo que (+) 3 4 3 * 4 é o mesmo que (*) 3 4 Main*> (+) 3 4 7 Main*> (*) 3 4 12

(11)

Precedencia e parenteses

Aplicação de função temprecedenciasobre operadores infixados. square 3 + square 4

=

(square 3) + (square 4)

Multiplicação temprecedenciasobre adição. 3*3 + 4*4

=

(12)

Associatividade

Adição éassociativa. 3 + (4 + 5) = 3 + 9 = 12 = 7 + 5 = (3 + 4) + 5

Adição éassociativa à esquerda. 3 + 4 + 5

significa (3 + 4) + 5

(13)

Parte II

(14)

Tipos de dados

I Integers: 42, -69 I Floats: 3.14 I Characters: ’h’ I Strings: "hello" I Pictures:

(15)

Aplicando uma função

invert :: Picture -> Picture

knight :: Picture

(16)

Compondo funções

beside :: Picture -> Picture -> Picture flipV :: Picture -> Picture

invert :: Picture -> Picture

knight :: Picture

(17)

Definindo uma nova função

double :: Picture -> Picture

double p = beside (invert p) (flipV p)

(18)

Definindo uma nova função

double :: Picture -> Picture

double p = beside (invert p) (flipV p)

(19)

Terminologia

Declaração de tipo (assinatura)

makePicture :: Picture -> Picture

Declaração de função

makePicture p = sideBySide (invert p) (flipV p)

(20)

Terminologia

parâmetro formal argumento (parâmetro real)

(21)

Parte III

(22)

Um programa (file lect02a.hs)

square :: Integer -> Integer square x = x * x

pyth :: Integer -> Integer -> Integer pyth a b = square a + square b

(23)

Executando o programa (file lect02a.hs)

% ghci lect02a.hs

GHCi, version 6.10.4: www.haskell.org/ghc/ :? for help

Loading package ghc-prim ... linking ... done.

Loading package integer ... linking ... done.

Loading package base ... linking ... done.

[1 of 1] Compiling Main ( lect02b.hs, interpreted )

Ok, modules loaded: Main.

*Main> square 3

9

*Main> pyth 3 4

(24)

Outro programa (file lect02b.hs)

import Test.QuickCheck

square :: Integer -> Integer square x = x * x

pyth :: Integer -> Integer -> Integer pyth a b = square a + square b

prop_square :: Integer -> Bool prop_square x =

square x >= 0

prop_squares :: Integer -> Integer -> Bool prop_squares x y =

square (x+y) == square x + 2*x*y + square y prop_pyth :: Integer -> Integer -> Bool prop_pyth x y =

(25)

Executando o outro programa (arquivo lect02b.hs)

% ghci lect02a.hs

GHCi, version 6.10.4: www.haskell.org/ghc/ :? for help

Loading package ghc-prim ... linking ... done.

Loading package integer ... linking ... done.

Loading package base ... linking ... done.

[1 of 1] Compiling Main ( lect02b.hs, interpreted )

Ok, modules loaded: Main.

*Main> quickCheck prop_square

Loading package old-locale-1.0.0.0 ... linking ... done.

Loading package old-time-1.0.0.0 ... linking ... done.

Loading package random-1.0.0.0 ... linking ... done.

Loading package mtl-1.1.0.1 ... linking ... done.

Loading package QuickCheck-2.1 ... linking ... done.

+++ OK, passed 100 tests. *Main> quickCheck prop_squares +++ OK, passed 100 tests. *Main> quickCheck prop_pyth

(26)

Parte IV

(27)

Gottfried Wilhelm Leibniz (1646–1716)

Antecipou a lógica simbólica e descobriu o cálculo diferencial e integral (independentemente de Newton).

“The only way to rectify our reasonings is to make them as tangible as those of the Mathematicians, so that we can find our error at a glance, and when there are disputes among persons, we can simply say: Let us calculate, without further ado, to see who is right. ”

“In symbols one observes an advantage in discovery which is greatest when they express the exact nature of a thing briefly and, as it were, picture it; then indeed the labor of thought is wonderfully diminished.”

Referências

Documentos relacionados

Given the impacts of the species in other regions of the world and the results of potential distribution modelling (Potter et al. 2009), efforts should also be made to detect

We owe our our researchers much involvement and work and that this journal brings together the knowledge of those who know and of those who want to learn, continuing to improve

We need to have enough works available to increase the number of articles per edition so that our journal is comparable to the best publications of the world.. Thus, we are grateful

In conclusion, we can mention our results to strengthen the concept that training is the right way to go because our ECMO weaning rate has increased from 60% to 88.9% and our

legais, implantação essa forçada, em parte, pela atuação do Gempo, os trabalhadores portuários brasileiros puderam constatar que o processo de reforma das relações de trabalho se

in this work we analysed tissues effects (muscle, head, eye, gut and the whole larvae) on rna:Dna ratios from different species (Sardina pilchardus, Engraulis encrasicolus,

(2) This reality must be inquired into, if we are to find the best standard of truth (we have said that we ought to determine our thoughts by the given standard of a true idea, and

Ao longo das Práticas Pedagógicas realizadas em contexto de 1.º e 2.º Ciclo do Ensino Básico (CEB) recorri de forma constante à reflexão para a melhoria das minhas práticas