第叄章學題 Lisp 3rd Edition, Winston & Horn

3-1: Define EXCHANGE, a procedure that returns the elements of a two-elment list in reverse order:

* (setf sinners '(adam eve))

* (exchange sinners)

(EVE ADAM)

3-2: Some people are annoyed by abbreviations like CONS, which is really a shortened form of construct.  Define a new procedure, CONSTRUCT, that does the same thing.

3-3: Define ROTATE-LEFT, a procedure that takes a list as its argument and returns a new list in which the former first element becomes the last.

* (rotate-left '(a b c))

(B C A)

* (rotate-left (rotate-left '(a b c)))

(C A B)

3-4: Define ROTATE-RIGHT.  It is to be like ROTATE-LEFT except that it is to rotate in the other direction.  You will want to use BUTLAST.

3-5: A palindrome is a list that has the same sequence of elements when read from right to left that it does when read from left to right.  Define PALINDROMIZE such that it takes a list as its argument and returns a palindrome that is twice as long.

3-6: When converting between degrees Fahrenheit and degrees Celsius it is useful to note that -40 degree Fahrenheit equals -40 degree Celsius.  This observation makes for the following symmetric conversion formulas:

C = ( F + 40 ) * 5/9 - 40

F = ( C + 40 ) * 9/5 - 40

Define conversion procedures, F-TO-C and C-TO-F, using these formulas.

Here are some examples of your results in use:

* (f-to-c 32)

* (f-to-c 98.6)

37.0

* (f-to-c 212)

100

PROCEDURE DEFINITION AND BINDING
---------------------------------------------------------------------------
DEFUN is LISP Procedure-Definition Primitive

* (setf meals '(breakfast lunch tea dinner))
(BREAKFAST LUNCH TEA DINNER)

* (cons (first meals) (last meals))
(BREAKFAST DINNER)

* (setf route2 '(boston cambridge lincoln concord))
(BOSTON CAMBRIDGE LINCOLN CONCORD)
* (cons (first route2) (last route2)
(BOSTON CONCORD)

* (both-ends meals)
(BREAKFAST DINNER)
* (both-ends route2)
(BOSTON CONCORD)

(defun both-ends    ;Procedure's name is BOTH-ENDS.
   (whole-list)        ;Parameter list (just one parameter here).
   (cons (first whole-list)  ;The body(just one form here).
             (last whole-list)

(defun <procedure>   
    (<parameter>)          
    <form>)                        

* (both-ends meals)
(BREAKFAST DINNER)
* (both-ends route2)
(BOSTON CONCORD)

(defun both-ends (whole-list)   ;Parameter list on the first line.
    (cons (first whole-list)   ;The body (just one from here).
              (last whole-list)))

(defun both-ends (w) (cons (first w) (last w)))

* (defun both-ends (whole-list)
       (cons (first whole-list)
                 (last whole-list)))
BOTH-ENDS
 --------------------------------------------------------------------------------

Parameter Variable Values Are Isolated by Virtual Fences

* (setf whole-list '(monday tuesday wednesday thursday friday))

* (both-ends '(boston cambridge lincoln concord))
(BOSTON CONCORD)

* whole-list
(MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY)
-----------------------------------------------------------------------------------------

Special Variable Values Are Not Isolated by Virtual Fences

(defun both-ends-with-special-variable ()
    (setf whole-list
             (cons (first whole-list) (last whole-list))))

* (setf whole-list '(monday tuesday wednesday thursday friday))
(MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY)
* (both-ends-with-special-variable)
(MONDAY FRIDAY)
* whole-list
(MONDAY FRIDAY)

* A variable is a lexical variable if it is isolated by a virtual fence. For the moment, you may assume that a variable is a lexical variable if it appears inside a procedure in which it is a parameter.
* A variable is a special variable if it is not isolated by a virtual fence. For the moment, you may assume that a variable is a special variable if it appears inside a procedure in which it is not a parameter.
-------------------------------------------------------------------------------------------------------

Procedure Match Parameters to Arguments

(defun both-ends-with two-parameters (l m)
    (cons (first l) (last m)))

* (both-ends-with-two-parameters '(breakfast lunch)
                                                       '(tea dinner))
(BREAKFAST TEA)

(defun both-ends-with-side-effect (whole-list)
    (setf last-list-processed whole-list)    ;First form in the body.
    (cons (first whole-list)
             (last whole-list)))    ;Second form in the body.

(defun <procedure name>
    (<parameter 1> ... <parameter m>)    ;Many parameters.
    <form 1>    ;Many forms.
    ...
    <form n>)
---------------------------------------------------------------------------------------------

LET Forms Evaluate Initial-Value Forms in Parallel

* (setf x 'outside)
* (let ((x 'inside)       ;X's parameter value will be INSIDE.
        (y x))            ;Y's parameter value will be outside.
    (list x y))
(INSIDE OUTSIDE)
-----------------------------------------------------------------
LET* Forms Evaluate Initial-Value Forms sequentially

* (setf x 'outside)
* (let* ((x 'inside)      ;X's parameter value will be INSIDE.
         (y x))           ;Y's parameter value will be INSIDE too.
    (list x y))
(INSIDE OUTSIDE)

* (setf x 'outside)
* (let ((x 'inside))
    (let ((y x))
      (list x y)))
(INSIDE OUTSIDE)
----------------------------------------------------------------------------------

Progressive Envelopment and Comment Translation Help Define New Procedures

(setf whole-list '(breakfast lunch tea dinner))

* (first whole-list)
breakfast
* (last whole-list)
(DINNER)

* (cons (first whole-list)
            (last whole-list))
(BREAKFAST DINNER)

(defun both-ends (whole-list)    ;Progressive envelopment version.
    (cons (first whole-list)
             (last whole-list)))

* (both-ends '(breakfast lunch tea dinner))
(BREAKFAST DINNER)

(defun both-ends (whole-list)
    ;;Extract first element.
    ;;Extract last element.
    ;;Combine the first and last element.
    )

(defun both-ends (whole-list)
    ;;Extract first element.
    (first whole-list)
    ;;Extract last element.
    (first (last whole-list))
    ;;Combine the first and last elements.
    )

(define both-ends (whole-list)    ;Comment translation version.
    ;;Combine the first and last elements.
    (list
      ;;Extract first element.
      (first whole-list)
      ;;Extract last element.
      (first (last whole-list))))

解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解

3-1: 
(defun exchange (input) 
  (reverse input));

或書的解答 
(defun exchange (pair) 
  (list (second pair) (first pair)))

common lisp 有 primitive: first, second, third 到 tenth。

3-2: 
(defun construct (front back) (cons front back))

3-3: 
(defun rotate-left (l) (append (rest l) (list (first l))))

3-4: 
(defun rotate-right (l) (append (last l) (butlast l)))

butlast 也可有兩個 arguments, 試第贰個用數字。

3-5: 
(defun palindromize (l) 
  (append input (reverse l))) 
或 
(defun palindromize (l) 
  (append (reverse l) l))

3-6: 
(defun f-to-c (f) 
  (- (/ (* (+ f 40) 5) 9) 40)); 

(defun c-to-f (c) 
  (- (/ (* (+ c 40) 9) 5) 40))

試這些 primitive: float, round, MAX, MIN, expt, sqrt, abs

你可能感兴趣的:(erlang,Scheme,haskell,lisp,clojure)