Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Un N es:
- ; – 0
- ; – (add1 N)
- ; interpretación: N representa los números naturales
- ;EJERCICIO 1
- ;sumanat: N N -> N
- (check-expect (sumanat 0 3) 3)
- (check-expect (sumanat 5 0) 5)
- (check-expect (sumanat 7 6) 13)
- (define (sumanat a b) (cond [(zero? b) a]
- [else (add1 (sumanat a (sub1 b)))]))
- ;EJERCICIO 2
- ;duplicarnat: N -> N
- (check-expect (duplicarnat 0) 0)
- (check-expect (duplicarnat 5) 10)
- (define (duplicarnat n) (sumanat n n))
- ;EJERCICIO 2*
- ;multiplicarnat: N N -> N
- (check-expect (multiplicarnat 0 3) 0)
- (check-expect (multiplicarnat 7 0) 0)
- (check-expect (multiplicarnat 1 3) 3)
- (check-expect (multiplicarnat 8 1) 8)
- (check-expect (multiplicarnat 4 10) 40)
- (define (multiplicarnat a b) (cond [(zero? b) 0]
- [else (sumanat a (multiplicarnat a (sub1 b)))]))
- ;EJERCICIO 3
- ;powernat: N N -> N
- (check-expect (powernat 0 5) 0)
- (check-expect (powernat 3 0) 1)
- (check-expect (powernat 2 5) 32)
- (define (powernat base exp) (cond [(zero? exp) 1]
- [else (multiplicarnat base (powernat base (sub1 exp)))]))
- ;EJERCICIO 4
- ;factnat: N -> N
- (check-expect (factnat 0) 1)
- (check-expect (factnat 1) 1)
- (check-expect (factnat 5) 120)
- (define (factnat a) (cond [(zero? a) 1]
- [else (multiplicarnat a (factnat (sub1 a)))]))
- ;EJERCICIO 5
- ;fibnat: N -> N
- (check-expect (fibnat 0) 0)
- (check-expect (fibnat 1) 1)
- (check-expect (fibnat 2) 1)
- (check-expect (fibnat 20) 6765)
- (define (fibnat a) (cond [(zero? a) 0]
- [(= a 1) 1]
- [else (sumanat (fibnat (sub1 a)) (fibnat (sub1 (sub1 a))))]))
- ;EJERCICIO 6
- ;leq: N N -> bool
- (check-expect (leq 0 0) #t)
- (check-expect (leq 5 0) #f)
- (check-expect (leq 0 3) #t)
- (check-expect (leq 5 5) #t)
- (check-expect (leq 7 8) #t)
- (check-expect (leq 10 9) #f)
- (define (leq a b) (cond [(zero? a) #true]
- [(zero? b) #false]
- [else (leq (sub1 a) (sub1 b))]))
- ;EJERCICIO 7
- ;lt: N N -> bool
- (check-expect (lt 0 0) #f)
- (check-expect (lt 5 0) #f)
- (check-expect (lt 0 3) #t)
- (check-expect (lt 5 5) #f)
- (check-expect (lt 7 8) #t)
- (check-expect (lt 10 9) #f)
- (define (lt a b) (cond [(zero? b) #false]
- [(zero? a) #true]
- [else (lt (sub1 a) (sub1 b))]))
- ;gt N N -> bool
- (check-expect (gt 0 0) #f)
- (check-expect (gt 5 0) #t)
- (check-expect (gt 0 3) #f)
- (check-expect (gt 5 5) #f)
- (check-expect (gt 7 8) #f)
- (check-expect (gt 10 9) #t)
- (define (gt a b) (cond [(zero? a) #false]
- [(zero? b) #true]
- [else (gt (sub1 a) (sub1 b))]))
- ;geq: N N -> bool
- (check-expect (geq 0 0) #t)
- (check-expect (geq 5 0) #t)
- (check-expect (geq 0 3) #f)
- (check-expect (geq 5 5) #t)
- (check-expect (geq 7 8) #f)
- (check-expect (geq 10 9) #t)
- (define (geq a b) (cond [(zero? b) #true]
- [(zero? a) #false]
- [else (geq (sub1 a) (sub1 b))]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement