-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path02-functions.rkt
More file actions
73 lines (57 loc) · 1.47 KB
/
02-functions.rkt
File metadata and controls
73 lines (57 loc) · 1.47 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
73
#lang racket
;; Funciones
; Scheme tiene dos formas para definir una función
; 1. (define f (lambda (p1 p2) ...))
; 2. (define (f p1 p2) ...)
; donde f es el nombre de la función y px es el argumento
; Ambas formas son lo mismo
;; Ejemplo: (calculadora-lambda suma 2 3)
(define calculadora-lambda ; nombre-funcion
(lambda (operacion a b) ; argumentos
(operacion a b))) ; retorno
;; Ejemplo: (calculadora suma 2 3)
(define (calculadora operacion a b) ;(nombre-funcion arg1 arg2 arg3)
(operacion a b)) ; retorno
;; Ejemplo: (suma 2 3)
(define (suma a b)
(+ a b))
;; Ejemplo: (resta 2 3)
(define resta
(lambda (a b)
(- a b)))
;; Currrying
;; Currificación
;; Ejemplo: ((curry2) f) arg1) arg2)
(define curry2
(lambda (f)
(lambda (arg1)
(lambda (arg2)
(f arg1 arg2)))))
;; Ejemplo: (((curry3) f) arg1) arg2) arg3)
(define curry3
(lambda (f)
(lambda (arg1)
(lambda (arg2)
(lambda (arg3)
(f arg1 arg2 arg3))))))
(define mult (curry2 *))
(define double (mult 2))
(define add (curry2 +))
(define increment (add 1))
(define decrement (add -1))
;; Ejemplo: (((calculadora-curry suma) 2) 3)
(define calculadora-curry
(lambda (operacion)
(lambda (a)
(lambda (b)
(operacion a b)))))
; Descripcion: funcion que obtiene el valor absoluto
; Dom: number
; Rec: number
; Ejemplo: (abs 9)
; Ejemplo: (abs -9)
; Ejemplo: (abs 0)
(define (abs x)
(cond [ (< x 0) (- x) ]
[ (= x 0) 0 ]
[ (> x 0) x ]))