;; TDDC74 dugga 2 140306 ;; Lösningsskiss. Variationer kan såklart förekomma. ; --------------------------------------------------------------------------------------- ; Uppgift 1 ; --------------------------------------------------------------------------------------- 'Uppgift1 ; a (define a 57) (define b (lambda (x) 1)) (define x (cons a b)) (define y (cons 'a 'b)) (define z (list 1 2 3)) ; x ; y ; z ; (list x y z) ; (cons a 'a) ; (cons (b y) (cons 5 z)) ; b (define foo (let ((ab (list 'a 'b))) (cons ab (cdr ab)))) foo (define bar (let ((cd (cons 'c 'd))) (cons (cons cd 'a) (cons 'b cd)))) bar ; --------------------------------------------------------------------------------------- ; Uppgift 2 ; --------------------------------------------------------------------------------------- 'Uppgift2 (define (present? element list) (if (null? list) #f (if (eqv? element (car list)) #t (present? element (cdr list))))) #| Kan såklart även skrivas (define (present? element list) (cond ((null? list) #f) ((eqv? element (car list)) #t) (else (present? element (cdr list))))) eller mer kompakt (och marginellt mer minneskrävande för väldigt stora strukturer): (define (present? element list) (and (not (null? list)) (or (eqv? element (car list)) (present? element (cdr list))))) |# (define (union L1 L2) (if (null? L1) L2 (if (present? (car L1) L2) (union (cdr L1) L2) (cons (car L1) (union (cdr L1) L2))))) (union '(1 2 3) '(2 3 4)) ; --------------------------------------------------------------------------------------- ; Uppgift 3 ; --------------------------------------------------------------------------------------- 'Uppgift3 (define (accumulate proc null-value L) (if (null? L) null-value (proc (car L) (accumulate proc null-value (cdr L))))) (define (enumerate low high) (if (> low high) () (cons low (enumerate (+ low 1) high)))) (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2)))))) ; a) (map fib (enumerate 5 12)) ;eller (accumulate cons () (map fib (enumerate 5 12))) ; b) (accumulate + 0 (map fib (enumerate 5 12))) ; c) (accumulate (lambda (a b) (if (odd? a) (+ a b) b)) 0 (map fib (enumerate 5 12))) ; eller (accumulate + 0 (map (lambda (n) (if (odd? (fib n)) (fib n) 0)) (enumerate 5 12))) ; --------------------------------------------------------------------------------------- ; Uppgift 4 ; --------------------------------------------------------------------------------------- 'Uppgift4 (define (make-number n) (cons 'number n)) (define (make-rational a b) (cons 'rational (cons a b))) (define (make-complex a b) (cons 'complex (cons a b))) ;; Vi skriver my-number?, my-rational?, my-complex? i koden här eftersom ;; namnen number?, rational? och complex? är reserverade. (define (tag number) (car number)) (define (my-number? number) (eqv? (tag number) 'number)) (define (my-rational? number) (eqv? (tag number) 'rational)) (define (my-complex? number) (eqv? (tag number) 'complex)) (define (get-a number) (cadr number)) (define (get-b number) (cddr number)) (define (get-n number) (cdr number)) ;(define (my-abs number) (if (> number 0) number (- 0 number))) (define (square n) (* n n)) (define (mag number) (cond ((my-number? number) (abs (get-n number))) ((my-rational? number) (abs (/ (get-a number) (get-b number)))) ((my-complex? number) (sqrt (+ (square (get-a number)) (square (get-b number))))) (else 'unkown-number))) (define x (make-number -4)) (define y (make-rational 3 -5)) (define z (make-complex 3.4 -5.55)) (mag x) (mag y) (mag z)