This repository has been archived by the owner on Aug 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
TORRE DE HANÓI | ||
|
||
#lang racket | ||
(define (move here there) | ||
(display (string-append here " -> " there "\n")) | ||
) | ||
|
||
(define towers n out> >in <extra>) | ||
(unless (zero? n) | ||
(begin | ||
(towers (- n 1) out> <extra> >in) | ||
(move out> >in) | ||
(towers (- n 1) <extra> out> >in) | ||
))) | ||
|
||
PROBLEMA DAS N-RAINHAS | ||
Exercício | ||
|
||
SIERPINSK | ||
(require 2htpd/image) | ||
|
||
@ let: define variáveis localmente, criando associações. Ex: | ||
@ (let ([a 42] [b 155]) (+ a b)) | ||
@ | ||
@ Mas esta forma não permitiria: | ||
@ (let ([a 42] [b a]) (+ a b)) | ||
@ Porque eles só valeriam depois das definições. | ||
@ | ||
@ Para o caso acima, existe o let*: | ||
@ (let* ([a 42] [b a]) (+ a b)) | ||
@ 'a' já estaria definida assim que fosse dada sua definição. | ||
@ | ||
@ Também podemos criar um let com nome. | ||
@ Este nome é um 'alias' disso. | ||
(let sierpinsk ([n 8]) | ||
(if (zero? n) | ||
(triangle 2 'solid 'red) | ||
(let ([t (sierpinski (- n 1))]) | ||
(freeze (above t (beside t t))) | ||
))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
Veremos agora uma linguagem TIPADA | ||
#lang play-typed | ||
|
||
Não temos mais 'car' ou 'cdr'. No lugar dela, usaremos a função | ||
'list' para definir o tipo listas e 'first' para conseguir o | ||
'car' de um par e/ou uma lista. Ex: | ||
|
||
> (first (list 1 2 3)) | ||
- number | ||
1 | ||
|
||
No play-typed, o apóstrofe tem tratamento diferente: | ||
> (+ 1 2) @ É executado e recebemos o retorno | ||
- number | ||
3 | ||
|
||
> '(+ 1 2) @ Cria-se uma s-expression. Um tipo novo que | ||
@ poderá ser interpretado. | ||
- s-expression | ||
'(+ 1 2) | ||
|
||
> (list 1 2) @ Uma sequência de números, de certo tipo. | ||
- (listof number) | ||
'(1 2) | ||
|
||
Para fazer o 'cast' entre s-expression e listas, precisamos usar | ||
o operador 's-exp->list'. | ||
|
||
Podemos fazer listas de listas: | ||
> (list (list 1 2) (list 4 5 6 7)) @ Listas de listas, como em Python | ||
- (listof (listof number)) | ||
'((1 2) (4 5 6 7)) | ||
|
||
> (list-ref (list 1 2 3 4 5 6) 3) @ Recupera um elemento de uma lista | ||
- number | ||
3 | ||
|
||
> (vector 1 2 3) @ Vector: Tem tamanho FIXO, não pode ser MODIFICADO | ||
@ e usualmente é mais rápido. | ||
- (vectorof number) | ||
'#(1 2 3) | ||
|
||
Com o plai-typed, podemos declarar tipos com 'define'. Para | ||
declarar EXPLICITAMENTE o tipo de um nome, colocamos de forma extra | ||
um ':': | ||
> (define c "string") | ||
- string | ||
"string" | ||
|
||
> (define c : number 2) | ||
- number | ||
2 | ||
|
||
> @ Já faz a verificação de tipagem previamente | ||
> (define c : number "string") | ||
- Erro | ||
|
||
Outra forma de lista, que na realidade age como uma struct de C/C++, | ||
é o 'value': | ||
> (values 9 8 "outro") | ||
- (number * number * string) | ||
'#(9 8 "outro") | ||
|
||
> @ Usando values para 'declarar' vários lugares. | ||
> (define-values (marca valor) (values "Prada" 3.50)) | ||
> marca | ||
- string | ||
"Prada" | ||
> valor | ||
- number | ||
3.5 | ||
|
||
Fazendo funções: | ||
|
||
> (define (3* n) (* 3 n)) | ||
> (3* 8) | ||
- number | ||
24 | ||
|
||
> @ Define a função com TIPO DE ARGUMENTO e TIPO DE RETORNO | ||
> (define 3* [n : number]) : number (* 3 n) | ||
|
||
> ((λ ([x : number]) : number (+ x x) 21) | ||
- number | ||
42 | ||
|
||
> @ A sintaxe abaixo funciona como um | ||
> (define mais2 : (number -> number) (λ(x) (+ x 2))) | ||
> (mais2 89) | ||
- number | ||
91 | ||
> mais2 | ||
- (number -> number) | ||
#procedure:... | ||
|
||
Ainda no plai-typed, existe o fechamento (em que a função leva o | ||
contexto de algumas variáveis no seu ambiente com ela). |