-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursion.rkt
122 lines (95 loc) · 2.53 KB
/
recursion.rkt
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#lang racket
; Caso de estudio:
; Sumar todos los números de una lista de números
; Dominio: list number
; Rec: number
; Ejemplo:
; > (sumar-numeros (list 1 2 3 4))
; > 10
; Sumatoria de lista de números
; Recursión: Natural
; Ejemplo:
; > (lst-sum-natural (list 1 2 3 4))
; 10
(define (lst-sum-natural lst)
(if [null? lst] 0
[+ (car lst)
(lst-sum-natural (cdr lst))
]))
; El equivalente de la función previa es:
; (+ 1 (+ 2 (+ 3 (+ 4 0))))
; Sumatoria de lista de números
; Recursión: Tail Recursion
; Ejemplo:
; > (lst-sum-natural (list 1 2 3 4))
; 10
; a tail-recursive list summing procedure
(define (do-lst-sum-tail lst acc)
(cond [(null? lst) acc]
[else
(do-lst-sum-tail (cdr lst)
(+ acc (car lst)))
]))
; a friendly wrapper that supplies an initial running sum of 0
; (lst-sum-tail (list 1 2 3 4))
(define (lst-sum-tail lst)
(do-lst-sum-tail lst 0))
; Escrito de forma más limpia
(define (lst-sum-tail-clean lst acc)
(cond [(null? lst) acc]
[else
(do-lst-sum-tail (cdr lst)
(+ acc (car lst)))
]))
; Caso de estudio:
; Obtener el largo total de una lista
; Dominio: list number
; Rec: number
; Ejemplo:
; > (largo (list 1 2 3 4))
; > 4
; Recursión: Natural
; (length (list 1 2 3 4))
(define (len-natural lst)
(if [null? lst] 0
[+ 1 (len-natural (cdr lst))]))
; Recursión: Tail Recursion
; (len-tail (list 1 2 3 4))
(define (len-tail lst)
(define (do-len-tail lst acc)
(if (null? lst) acc
(do-len-tail (cdr lst)
(+ 1 acc))))
(do-len-tail lst 0))
;; Caso de estudio: multiplicar los números de una lista
(define (lst-prod-natural lst)
(if [null? lst] 1
[* (car lst)
(lst-sum-natural (cdr lst))
]))
(define (reduce fn base-value lis)
(if (null? lis)
base-value
(fn (car lis)
(reduce fn base-value (cdr lis)))))
(reduce * 1 (list 1 2 3 4))
(define (append list1 list2)
(reduce cons list2 list1))
(append (list 1 2 3 4) (list 5 6 7 8))
(define (list-copy lis)
(reduce cons '() lis))
(define (make-reducer fn base-value)
(lambda (lis)
(reduce fn base-value lis)))
((make-reducer + 0) '(1 2 3 4))
((make-reducer * 1) '(1 2 3 4))
;(define (suma . args)
; (reduce + 0 args))
;(suma + 0 (list 1 2 3 4))
(define (make-reducer2 fn base-value)
(define (reduce lis)
(if (null? lis)
base-value
(fn (car lis)
(reduce (cdr lis)))))
reduce) ;return new closure of local procedure