; Jade Yu Cheng (TA) ; ICS 313 ; Homework 16 ; Due April 14, 2009 ; --------------------------------------------------------------------------- ; ; 1. Write a function called add1 that adds one to the middle number in a list ; that contains an odd number of numbers. Try it out as follows: ; (add1 '(2 9 8 7 3)) ; The result should be (2 9 9 7 3) ; (defun add1 (X) (let ((c (/ (- (length X) 1) 2))) (setf (nth c X) (+ 1 (nth c X))))) (let ((mylist '(2 9 8 7 3))) (add1 mylist) (format t "1. (add1 '(2 9 8 7 3)) --> ~a ~%~%" mylist)) ; --------------------------------------------------------------------------- ; ; 2. Write a function called pick that squares a number if it is negative, and ; adds 2 to it otherwise. Try it on -3 and +2. (pick -3) should evaluate ; as 9, while (pick 2) should evaluate as 4. ; (defun pick (X) (if (minusp X) (* X X) (+ X 2))) (format t "2-a. (pick -3) --> ~d ~%~%" (pick -3)) (format t "2-b. (pick 4) --> ~d ~%~%" (pick 4)) ; --------------------------------------------------------------------------- ; ; 3. (a) Write a function call zero-it, which, with argument x, evaluates as ; zero if x is negative, and as x otherwise. So (zero-it -2) evaluates ; as 0, while (zero-it 3) evaluates as 3. ; (b) Now use mapcar together with zero-it to provide a function which ; replaces all the negative numbers in a list by zero. Try it on the ; list (3 -2 4 -6 8) ; (defun zero-it (X) (if (minusp X) 0 X)) (format t "3-a1. (zero-it -3) --> ~d ~%~%" (zero-it -3)) (format t "3-a2. (zero-it 3) --> ~d ~%~%" (zero-it 3)) (format t "3-b. (mapcar #' zero-it '(3 -2 4 -6 8))) --> ~a ~%~%" (mapcar #' zero-it '(3 -2 4 -6 8))) ; --------------------------------------------------------------------------- ; ; 4. Write a predicate call pal which takes determines whether a list is a ; plalindrome. A palindrome is a sequence such as (2 3 8 8 3 2) which ; consists of a first part (2 3 8) followed by that list in reverse order ; (8 3 2). So (pal '(2 3 8 8 3 2)) should evaluate as t, while ; (pal '(2 3 3 2 7)) should evaluate as nil. ; (defun pal (X) (equal (reverse X) X)) (format t "4-a. (pal '(2 3 8 8 3 2)) --> ~a ~%~%" (pal '(2 3 8 8 3 2))) (format t "4-b. (pal '(2 3 3 2 7)) --> ~a ~%~%" (pal '(2 3 3 2 7))) ; --------------------------------------------------------------------------- ; ; 5. Create an array A with 10 elements, and set its successive members to ; 2 4 6 . . . 20. ; (defun even-array (size) (let ((mylist (make-array size))) (dotimes (v size) (setf (aref mylist v) (* (+ v 1) 2))) mylist)) (format t "5. (even-array 10) --> ~a ~%~%" (even-array 10)) ; --------------------------------------------------------------------------- ; (quit) ; --------------------------------------------------------------------------- ; ; The program's output is as follows: ; ; 1. (add1 '(2 9 8 7 3)) --> (2 9 9 7 3) ; ; 2-a. (pick -3) --> 9 ; ; 2-b. (pick 4) --> 6 ; ; 3-a1. (zero-it -3) --> 0 ; ; 3-a2. (zero-it 3) --> 3 ; ; 3-b. (mapcar #' zero-it '(3 -2 4 -6 8))) --> (3 0 4 0 8) ; ; 4-a. (pal '(2 3 8 8 3 2)) --> T ; ; 4-b. (pal '(2 3 3 2 7)) --> NIL ; ; 5. (even-array 10) --> #(2 4 6 8 10 12 14 16 18 20) ;