-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeclarative-programming.rkt
More file actions
72 lines (57 loc) · 1.26 KB
/
declarative-programming.rkt
File metadata and controls
72 lines (57 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#lang racket
; Constructores
(cons 0 1)
(list 2 3 4 5 6)
; Funciones de pertenencia
(list? (list 2 3 4 5 6))
(null? (list 2 3 4 5 6))
(null? (list))
; Selectores
(car (list 2 3 4 5 6))
(cdr (list 2 3 4 5 6))
(car (cdr (list 2 3 4 5 6)))
(caddr (list 2 3 4 5 6))
; Funciones auxiliares
(length (list 2 3 4 5 6))
; Filter
(define myFilter
(lambda (f L)
(if (null? L)
null
(if (f (car L))
(cons (car L) (myFilter f (cdr L)))
(myFilter f (cdr L))
)
)
))
(define mayorQue3 (lambda (n) (> n 3)))
(myFilter even? (list 2 3 4 5 6))
(myFilter mayorQue3 (list 2 3 4 5 6))
; Map
(define myMap
(lambda (f L)
(if (null? L)
null
(cons (f (car L)) (myMap f (cdr L)))
)
))
(define cuadrado (lambda (n) (* n n)))
(myMap cuadrado (list 2 3 4 5 6))
; Reduce/Apply
(define myReduce
(lambda (f L)
(if (null? L)
null
(if (null? (cdr L))
(car L)
(f (car L) (myReduce f (cdr L)))
)
)
))
(myReduce + (list 2 3 4 5 6))
; Funciones declarativas como parte del lenguaje
(filter
(lambda (x) (and (positive? x) (even? x)))
(list 3 -5 22 0 -1 13 -44 88))
(map (lambda (x) (* 2 x)) (list 10 20 30 40 50))
(apply + (list 2 3 4))