-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions-dot.rkt
108 lines (90 loc) · 2.48 KB
/
functions-dot.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
#lang racket
; Pares
; (myfunction-lambda 2)
(define myfunction-lambda
(λ (x)
(display x)))
(define (myfunction arg)
(list arg))
(define (myfunction2 arg . args)
(list (car args)))
(define (move-to-next-elements lst) (cdr lst))
(define (get-current-element lst) (car lst))
(define (get-next-element lst)
(car (cdr lst)))
;; Tail Recursion
; (sum-elements (list 1 2 3 4 5))
(define (sum-elements lst)
(define (do-sum lst acc)
(cond
[(null? lst) acc]
[else (do-sum (cdr lst)
(+ (car lst) acc))]
))
(do-sum lst 0))
;; Natural Recursion
; (sum (list 1 2 3 4 5))
;; using cond
(define (sum lst)
(cond
[(null? lst) 0]
[else ( + (car lst)
(sum (cdr lst))
)
]))
;; using if
(define (sum-v2 lst)
(if
(null? lst) 0
(+ (car lst) (sum-v2 (cdr lst)))
))
; using apply
; (sum-apply (list 1 2 3 4 5))
(define (sum-apply lst) (apply + lst))
; Ejemplo: (merge-two-lists (list 1 2 3) (list 4 5 6))
; Ejecución: '(1 2 3 4 5 6)
(define (merge-two-lists lst1 lst2)
(if (null? lst1) lst2
(cons (car lst1)
(merge-two-lists (cdr lst1) lst2))))
; Merge intercalado
; Ejemplo: (merge-two-lst (list 1 2 3) (list 4 5 6))
; Ejecución: '(1 4 2 5 3 6)
(define (merge-two-lst lst1 lst2)
(cond [(null? lst1) lst2] ; if the first list is empty, return the second
[(null? lst2) lst1] ; if the second list is empty, return the first
[else (cons (car lst1) ; otherwise `cons` the first element of the first list
(merge-two-lst lst2 (cdr lst1)))])) ; and interleave the lists
; Ejemplo: (append (list 1 2 3))
; Ejemplo: (append (list 1 2 3) (list 4 5 6))
; Ejecución: '((1 2 3) (4 5 6))
(define (append . args) args)
; Ejemplo: (print-dot-notation (list 1 2 3) (list 4 5 6) (list 7 8 9))
(define (print-dot-notation arg . args)
(display arg)
(newline)
(display (car arg))
(newline)
(display (cdr arg))
(newline)
(display args)
(newline)
(display (car args))
(newline)
(display (cdr args)))
; Ejemplo: (print-dot-notation-v2 (list 1 2 3) (list 4 5 6) (list 7 8 9))
(define (print-dot-notation-v2 . args)
(display args)
(newline)
(display (car args))
(newline)
(display (cdr args))
(newline)
(display (car (cdr args))))
;; Merge Natural Recursion
;; (merge 1 2 3 4 5)
;; '(1 2 3 4 5)
;; (merge (list 1 2 3) (list 4 5 6))
(define (merge . args)
(cond [(null? (cdr args)) (car args)]
[else (cons (car args) (merge (cdr args)))]))