Advertisement
tomasfdel

Racket Práctica 4.3

Jun 2nd, 2016
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 3.51 KB | None | 0 0
  1. ;EJERCICIO 1:
  2. (check-expect (pares (list 0 1 2 3 4 5 6 7 8 9)) (list 0 2 4 6 8))
  3. (check-expect (pares (list 1 3 5)) empty)
  4. (define (pares l) (cond [(empty? l) empty]
  5.                         [(= (remainder (first l) 2) 0) (cons (first l) (pares (rest l)))]
  6.                         [else (pares (rest l))]))
  7.  
  8.  
  9.  
  10. ;EJERCICIO 2:
  11. (check-expect (cortas (list "Lista" "de" "palabras" "sin" "sentido")) (list "de" "sin"))
  12. (define (cortas l) (cond [(empty? l) empty]
  13.                         [(< (string-length (first l)) 5) (cons (first l) (cortas (rest l)))]
  14.                         [else (cortas (rest l))]))
  15.  
  16.  
  17.  
  18.  
  19.  
  20. ;EJERCICIO 3:
  21. (define MAX 5)
  22. (define (dist-origen n) (sqrt (+ (sqr (posn-x n)) (sqr (posn-y n)))))
  23. (check-expect (cerca (list (make-posn 3 5) (make-posn 1 2) (make-posn 0 1) (make-posn 5 6))) (list (make-posn 1 2) (make-posn 0 1)))
  24. (define (cerca l) (cond [(empty? l) empty]
  25.                         [(< (dist-origen (first l)) MAX) (cons (first l) (cerca (rest l)))]
  26.                         [else (cerca (rest l))]))
  27.  
  28.  
  29.  
  30.  
  31. ;EJERCICIO 4:
  32. (check-expect (positivos (list -5 37 -23 0 12)) (list 37 12))
  33. (define (positivos l) (cond [(empty? l) empty]
  34.                         [(> (first l) 0) (cons (first l) (positivos (rest l)))]
  35.                         [else (positivos (rest l))]))
  36.  
  37.  
  38.  
  39.  
  40.  
  41. ;EJERCICIO 5:
  42. (check-expect (raices (list 9 16 4)) (list 3 4 2))
  43. (define (raices l) (cond [(empty? l) empty]
  44.                             [else (cons (sqrt (first l)) (raices (rest l)))]))
  45.  
  46.  
  47.  
  48.  
  49. ;EJERCICIO 6:
  50. (define (dist-origen n) (sqrt (+ (sqr (posn-x n)) (sqr (posn-y n)))))
  51. (check-expect (distancias (list (make-posn 3 4) (make-posn 0 4) (make-posn 12 5))) (list 5 4 13))
  52. (define (distancias l) (cond [(empty? l) empty]
  53.                             [else (cons (dist-origen (first l)) (distancias (rest l)))]))
  54.  
  55.  
  56.  
  57.  
  58.  
  59. ;EJERCICIO 7:
  60. (check-expect (anchos (list (circle 30 "solid" "red") (rectangle 10 30 "outline" "blue"))) (list 60 10))
  61. (define (anchos l) (cond [(empty? l) empty]
  62.                             [else (cons (image-width (first l)) (anchos (rest l)))]))
  63.  
  64.  
  65.  
  66.  
  67. ;EJERCICIO 8:
  68. (define (sgn2 x) (cond [(< x 0) -1]
  69.                        [(= x 0) 0]
  70.                        [else 1]))
  71. (check-expect (signos (list 45 32 -23 0 12)) (list 1 1 -1 0 1))
  72. (define (signos l) (cond [(empty? l) empty]
  73.                             [else (cons (sgn2 (first l)) (signos (rest l)))]))
  74.  
  75.  
  76.  
  77.  
  78.  
  79. ;EJERCICIO 9:
  80. (check-expect (prod (list 1 2 3 4 5)) 120)
  81. (define (prod l) (cond [(empty? l) 1]
  82.                             [else (* (first l) (prod (rest l)))]))
  83.  
  84.  
  85.  
  86.  
  87. ;EJERCICIO 10:
  88. (check-expect (pegar (list "Las " "lis" "tas " "son " "complicadas" ".")) "Las listas son complicadas.")
  89. (define (pegar l) (cond [(empty? l) ""]
  90.                             [else (string-append (first l) (pegar (rest l)))]))
  91.  
  92.  
  93.  
  94.  
  95. ;EJERCICIO 11:
  96. (check-expect (maximo (list 23 543 325 0 75)) 543)
  97. (define (mayor a b) (if (> a b) a b))
  98. (define (maximo l) (cond [(empty? l) 0]
  99.                             [else (mayor (first l) (maximo (rest l)))]))
  100.  
  101.  
  102.  
  103.  
  104.  
  105. ;EJERCICIO 12:
  106. (check-expect (sum-dist (list (make-posn 3 4) (make-posn 0 4) (make-posn 12 5))) 22)
  107. (define (dist-origen n) (sqrt (+ (sqr (posn-x n)) (sqr (posn-y n)))))
  108. (define (sum-dist l) (cond [(empty? l) 0]
  109.                             [else (+ (dist-origen (first l)) (sum-dist (rest l)))]))
  110.  
  111.  
  112.  
  113.  
  114. ;EJERCICIO 13:
  115. (check-expect (sumcuad (list 1 2 3)) 14)
  116. (define (sumcuad l) (cond [(empty? l) 0]
  117.                             [else (+ (sqr (first l)) (sumcuad (rest l)))]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement