;; Stian Søiland, øvingsgruppe 3 '(oving 3 (oppgave 1 (oppgave 'a (defmacro /. (a b) `(float (/ ,a ,b))) (/. 1 3) => 0.33333334 (macroexpand-1 '(/. 1 3)) => (FLOAT (/ 1 3)) ; => T ) (oppgave 'b (defmacro root (x n) `(float (expt ,x (/ 1 ,n)))) (root 64 3) => 4.0 (macroexpand-1 '(root 64 3)) =>(FLOAT (EXPT 64 (/ 1 3))) ; =>T ) (oppgave 'c (defmacro ln (x) `(log ,x (exp 1))) (ln 2) => 0.69314724 (macroexpand-1 '(ln 2)) => (LOG 2 (EXP 1)) ; => T ) ) (oppgave 2 (oppgave 'a (defmacro my-delete (x elems) `(setf ,elems (delete ,x ,elems))) (setf lista '(a b c d e f g h)) (my-delete 'E lista) => (A B C D F G H) lista => (A B C D F G H) (macroexpand-1 '(my-delete 'E lista)) => (SETF LISTA (DELETE 'E LISTA)) ; => T ) (oppgave 'b (defmacro my-nreverse (elems) `(setf ,elems (nreverse ,elems))) (my-nreverse lista) => (H G F D C B A) (macroexpand-1 '(my-nreverse lista)) => (SETF LISTA (NREVERSE LISTA)) ; => T ) (oppgave 'c (defmacro swap (x y) (let ((a (gensym))) `(list (setf ,a ,x) (setf ,x ,y) (setf ,y ,a)))) (setf flakk 5 flukk 10) (list flakk flukk) => (5 10) (swap flakk flukk) (list flakk flukk) => (10 5) (macroexpand-1 '(swap flakk flukk)) => (LIST (SETF #:G1181 FLAKK) (SETF FLAKK FLUKK) (SETF FLUKK #:G1181)) ; => T ) ) (oppgave 3 (defmacro with-random-int ((x a b) &rest body) `(let ((,x (+ ,a (random(+ 1 (- ,b ,a)))))) ,@body)) (with-random-int (r 5 10) (print r) (print (* 10 r))) => 7 => 70 => 70 (macroexpand-1 '(with-random-int (r 5 10) (print r) (print (* 10 r)))) => (LET ((R (+ 5 (RANDOM (+ 1 (- 10 5)))))) (PRINT R) (PRINT (* 10 R))) ; => T ) (oppgave 4 (oppgave 'a (defun seq-pairs (elems) (butlast ;; lambda function returns the first and second of the rest ;; of the list called for each cdr of list by maplist (maplist #'(lambda (rest) (list (first rest)(second rest))) elems)))) (oppgave 'b (defun wrapped-seq-pairs (elems) (butlast (maplist #'(lambda (rest) (list (first rest)(second rest))) (append elems (list (car elems))))))) ) (oppgave 5 (oppgave 'a a b c d e f z=a a=b b=c c=d d=e e=f f=z (defmacro swap-left (&rest vars) (butlast