University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

oppgave.scm 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ;;;;;;;
  2. ;; 1 ;;
  3. ;;;;;;;
  4. ;; A-E: se oppgave1.pdf
  5. ;; F
  6. (car (cdr '(0 42 #t 'bar) ))
  7. ;; G
  8. (car (cdr (car '((0 42) (#t 'bar)) )))
  9. ;; H
  10. (car (car (cdr '((0) (42 #t) ('bar)) )))
  11. ;; I - bare cons
  12. (cons (cons 0
  13. (cons 42
  14. '()))
  15. (cons (cons #t
  16. (cons 'bar
  17. '()))
  18. '()))
  19. ;; I - bare list
  20. (list (list 0 42) (list #t 'bar))
  21. ;;;;;;;
  22. ;; 2 ;;
  23. ;;;;;;;
  24. ;; A
  25. (define (length2 items)
  26. (define (iter l acc)
  27. (if (null? l)
  28. acc
  29. (iter (cdr l) (+ acc 1))))
  30. (iter items 0))
  31. ;; B
  32. ;; recuce-reverse er halerekursiv, fordi det siste den gjør
  33. ;; er å kalle seg selv.
  34. ;; Den gir opphav til en iterativ prosess.
  35. (define (reduce-reverse proc init items)
  36. (if (null? items)
  37. init
  38. (reduce-reverse proc
  39. (proc (car items) init)
  40. (cdr items))))
  41. ;; C
  42. (define (all? pred items)
  43. (if (null? items)
  44. #t
  45. (and (pred (car items)) (all? pred (cdr items)))))
  46. (all? (lambda (x) (<= x 10))
  47. '(1 2 3 4 5))
  48. (all? (lambda (x) (<= x 10))
  49. '(1 2 3 4 50))
  50. ;; D
  51. (define (nth i items)
  52. (if (= i 0)
  53. (car items)
  54. (nth (- i 1) (cdr items))))
  55. ;; E
  56. (define (where item items)
  57. (define (iter l i)
  58. (if (null? l)
  59. #f
  60. (if (= item (car l))
  61. i
  62. (iter (cdr l) (+ i 1)))))
  63. (iter items 0))
  64. ;; F
  65. (define (map2 proc la lb)
  66. (if (or (null? la) (null? lb))
  67. '()
  68. (cons (proc (car la) (car lb))
  69. (map2 proc (cdr la) (cdr lb)))))
  70. ;; G
  71. (map2 (lambda (a b) (/ (+ a b) 2))
  72. '(1 2 3 4) '(3 4 5))
  73. ;; H
  74. (define (both? proc)
  75. (lambda (a b) (and (proc a) (proc b))))
  76. ;; I
  77. (define (self proc)
  78. (lambda (x) (proc x x)))