Lisp

Published on February 2017 | Categories: Documents | Downloads: 51 | Comments: 0 | Views: 322
of 3
Download PDF   Embed   Report

Comments

Content

;; Comentários
Receita para Programas Plenos (PLAIN FUNCTION):
;; Contract: nome_do_programa : argumentos -> retorno
;; Purpose: descrição do objetivo do programa
;; Exemplo: exemplos práticos e resultado esperado.
;; Definição: define o header
header ...
body
;; Testes.
Ex:
;; Contract: area-of-ring : number number -> number
;; Purpose: to compute the area of a ring whose radius is
;; outer and whose hole has a radius of inner
;; Example: (area-of-ring 5 3) should produce 50.24
;; Definition: [refines the header]
(define (area-of-ring outer inner)
(- (area-of-disk outer)
(area-of-disk inner)))
;; Tests:
(area-of-ring 5 3)
;; expected value
50.24
Receita para Funções Condicionais:
Análise dos dados e definições: definir quais são as situações (condições) possíveis
Exemplos: deve-se utilizar exemplos nos limites de uma condição para outra e inserid
os completamente na condição.
Determinar e enumerar as condições (body conditions)
Verificar através dos exemplos se todas as condições, enumeradas antes da verdadeira,
são falsas.
Definir a resposta para cada condição (body answers).
;; Contract: nome_do_programa : argumentos -> retorno
;; Purpose: descrição do objetivo do programa
;; Exemplos: exemplos práticos e resultado esperado.
;; Definição: define o header
(define (function x)
(cond
[(...) (...)]
[(...) (...)]
[(...) (...)]))
Ex:
;; retorno_aplicação_1m : investido --> retirado
;; Definir quanto será retirado após 1m de aplicação, dado o valor investido.
;; Obs: juros de 1% entre R$ 0,00 a R$999,99; 2% se R$ 1.000,00 ou mais.
;; Exemplos:
;; -500: Zero
;; 0: 0 (1%)

;; 500: 505 (1%)
;; 999,99: 1009,99 (1%)
;; 1000: 1020 (2%)
;; 2000: 2040 (2%)
## 1) (< x 0)
## 2) (and (>= x 0) (< x 1000))
## 3) (>= x 1000)
(define (retorno_aplicação_1m investido)
(cond
[(< investido 0) 0]
[(and (>= investido 0) (< investido 1000)) (* investido 1.01) ]
[(>= investido 1000) (* investido 1.02)]))
## Testar!
(define-struct estrutura (prop1 prop2))
(make-estrutura 'teste 'reteste)
estrutura-prop1 (make-estrutura 'teste 'reteste): 'teste
estrutura-prop2 (make-estrutura 'teste 'reteste): 'reteste

;; Data Analysis & Definitions:
(define-struct student (last first teacher))
;; A student is a structure: (make-student l f t) where f, l, and t are
symbols.
;;
;; Contract: subst-teacher : student symbol -> student
;; Purpose: to create a student structure with a new
;; teacher name if the teacher's name matches 'Fritz
;;
;; Examples:
;; (subst-teacher (make-student 'Find 'Matthew 'Fritz) 'Elise)
;; =
;; (make-student 'Find 'Matthew 'Elise)
;; (subst-teacher (make-student 'Find 'Matthew 'Amanda) 'Elise)
;; =
;; (make-student 'Find 'Matthew 'Amanda)
;;
;; Template:
;; (define (process-student a-student a-teacher)
;; ... (student-last a-student) ...
;; ... (student-first a-student) ...
;; ... (student-teacher a-student) ...)
## Template nos informa detalhes sobre a estrutura de uma struct. Lista os proce
ssos para seleção de seus componentes individuais. Não se preocupa com outputs. No exe
mplo, como a-student é uma strcuct, lista as funções para seleção de suas propriedades.
;;
;; Definition:
(define (subst-teacher a-student a-teacher)
(cond
[(symbol=? (student-teacher a-student) 'Fritz)
(make-student (student-last a-student)
(student-first a-student)
a-teacher)]
[else a-student]))
;;
;; Tests:
(subst-teacher (make-student 'Find 'Matthew 'Fritz) 'Elise)
;; expected value:
;;(make-student 'Find 'Matthew 'Elise)

(subst-teacher (make-student 'Find 'Matthew 'Amanda) 'Elise)
;; expected value:
;;(make-student 'Find 'Matthew 'Amanda)
FUNCTIONS FOR MIXED DATA
Data Analysis and Design: When we analyze a problem statement, our first task
is to determine whether it mentions distinct classes of data -- which we call MI
XED DATA and which is also known as the UNION of data classes. In other words, t
he data analysis must take into account several aspects now. First, we must dete
rmine how many distinct classes of objects are mentioned in the problem and what
their important attributes are. If there are several different classes of objec
ts, we are mixing data. Second, we must understand whether the various objects h
ave several properties. If an object has several attributes, we use compound dat
a for its representation. As a result, the resulting data definition may have se
veral clauses that enumerate several possibilities. Indeed, we will see that the
data analysis may yield a hierarchy of data definitions.
;; Data Definition:
(define-struct circle (center radius))
(define-struct square (nw length))
;; A shape is either
;; 1. a structure: (make-circle p s)
;;
where p is a posn, s a number;
;; 2. a structure: (make-square p s)
;;
where p is a posn, s a number.
;; Contract, Purpose, Header:
;; perimeter : shape -> number
;; to compute the perimeter of a-shape
;; Examples: see tests
;; Template:
;; (define (f a-shape)
;; (cond
;;
[(square? a-shape)
;;
... (square-nw a-shape) ... (square-length a-shape) ...]
;;
[(circle? a-shape)
;;
... (circle-center a-shape) ... (circle-radius a-shape) ...]))
;; Definition:
(define (perimeter a-shape)
(cond
[(circle? a-shape)
(* (* 2 (circle-radius a-shape)) pi)]
[(square? a-shape)
(* (square-length a-shape) 4)]))
;; Tests: (same as examples)
(= (perimeter (make-square ... 3)) 12)
(= (perimeter (make-circle ... 1)) (* 2 pi))
Even a cursory comparative reading of the design recipes in sections 2.5, 4.
4, 6.5, and the current one suggests that the data analysis and the template des
ign steps are becoming more and more important. If we do not understand what kin
d of data a function consumes, we cannot design it and organize it properly. If,
however, we do understand the structure of the data definition and organize our
template properly, it is easy to modify or to extend a function. For example, i
f we add new information to the representation of a circle, then only those cond
-clauses related to circles may require changes. Similarly, if we add a new kind
of shape to our data definition, say, rectangles, we must add new cond-clauses
to our functions.

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close