Advertisement
tomasfdel

Racket Práctica 5

Jun 7th, 2016
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 3.04 KB | None | 0 0
  1. ; Un N es:
  2. ; – 0
  3. ; – (add1 N)
  4. ; interpretación: N representa los números naturales
  5.  
  6.  
  7. ;EJERCICIO 1
  8.  
  9. ;sumanat: N N -> N
  10. (check-expect (sumanat 0 3) 3)
  11. (check-expect (sumanat 5 0) 5)
  12. (check-expect (sumanat 7 6) 13)
  13. (define (sumanat a b) (cond [(zero? b) a]
  14.                             [else (add1 (sumanat a (sub1 b)))]))
  15.  
  16.  
  17.  
  18. ;EJERCICIO 2
  19.  
  20. ;duplicarnat: N -> N
  21. (check-expect (duplicarnat 0) 0)
  22. (check-expect (duplicarnat 5) 10)
  23. (define (duplicarnat n) (sumanat n n))
  24.  
  25.  
  26.  
  27. ;EJERCICIO 2*
  28.  
  29. ;multiplicarnat: N N -> N
  30. (check-expect (multiplicarnat 0 3) 0)
  31. (check-expect (multiplicarnat 7 0) 0)
  32. (check-expect (multiplicarnat 1 3) 3)
  33. (check-expect (multiplicarnat 8 1) 8)
  34. (check-expect (multiplicarnat 4 10) 40)
  35. (define (multiplicarnat a b) (cond [(zero? b) 0]
  36.                                    [else (sumanat a (multiplicarnat a (sub1 b)))]))
  37.  
  38.  
  39.  
  40. ;EJERCICIO 3
  41.  
  42. ;powernat: N N -> N
  43. (check-expect (powernat 0 5) 0)
  44. (check-expect (powernat 3 0) 1)
  45. (check-expect (powernat 2 5) 32)
  46. (define (powernat base exp) (cond [(zero? exp) 1]
  47.                                   [else (multiplicarnat base (powernat base (sub1 exp)))]))
  48.  
  49.  
  50.  
  51. ;EJERCICIO 4
  52.  
  53. ;factnat: N -> N
  54. (check-expect (factnat 0) 1)
  55. (check-expect (factnat 1) 1)
  56. (check-expect (factnat 5) 120)
  57. (define (factnat a) (cond [(zero? a) 1]
  58.                           [else (multiplicarnat a (factnat (sub1 a)))]))
  59.  
  60.  
  61.  
  62. ;EJERCICIO 5
  63.  
  64. ;fibnat: N -> N
  65. (check-expect (fibnat 0) 0)
  66. (check-expect (fibnat 1) 1)
  67. (check-expect (fibnat 2) 1)
  68. (check-expect (fibnat 20) 6765)
  69. (define (fibnat a) (cond [(zero? a) 0]
  70.                          [(= a 1) 1]
  71.                          [else (sumanat (fibnat (sub1 a)) (fibnat (sub1 (sub1 a))))]))
  72.  
  73.  
  74.  
  75. ;EJERCICIO 6
  76.  
  77. ;leq: N N -> bool
  78. (check-expect (leq 0 0) #t)
  79. (check-expect (leq 5 0) #f)
  80. (check-expect (leq 0 3) #t)
  81. (check-expect (leq 5 5) #t)
  82. (check-expect (leq 7 8) #t)
  83. (check-expect (leq 10 9) #f)
  84. (define (leq a b) (cond [(zero? a) #true]
  85.                         [(zero? b) #false]
  86.                         [else (leq (sub1 a) (sub1 b))]))
  87.  
  88.  
  89.  
  90. ;EJERCICIO 7
  91.  
  92. ;lt: N N -> bool
  93. (check-expect (lt 0 0) #f)
  94. (check-expect (lt 5 0) #f)
  95. (check-expect (lt 0 3) #t)
  96. (check-expect (lt 5 5) #f)
  97. (check-expect (lt 7 8) #t)
  98. (check-expect (lt 10 9) #f)
  99. (define (lt a b) (cond [(zero? b) #false]
  100.                        [(zero? a) #true]
  101.                        [else (lt (sub1 a) (sub1 b))]))
  102.  
  103. ;gt N N -> bool
  104. (check-expect (gt 0 0) #f)
  105. (check-expect (gt 5 0) #t)
  106. (check-expect (gt 0 3) #f)
  107. (check-expect (gt 5 5) #f)
  108. (check-expect (gt 7 8) #f)
  109. (check-expect (gt 10 9) #t)
  110. (define (gt a b) (cond [(zero? a) #false]
  111.                        [(zero? b) #true]
  112.                        [else (gt (sub1 a) (sub1 b))]))
  113.  
  114. ;geq: N N -> bool
  115. (check-expect (geq 0 0) #t)
  116. (check-expect (geq 5 0) #t)
  117. (check-expect (geq 0 3) #f)
  118. (check-expect (geq 5 5) #t)
  119. (check-expect (geq 7 8) #f)
  120. (check-expect (geq 10 9) #t)
  121. (define (geq a b) (cond [(zero? b) #true]
  122.                         [(zero? a) #false]
  123.                         [else (geq (sub1 a) (sub1 b))]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement