clisp笔记

下面的代码大部分来自网络,版权归原作者所有。

 

规则:
loop
    read in an expression from the console;
    evaluate the expression;
    print the result of evaluation to the console;
end loop.

补充:
一切都是函数,任何函数都在括号内

1.hello world
(print "Hello, world!")

2.退出
(exit)

3.基本操作
(+ x1 x2 ... xn)     The sum of x1, x2, ..., xn
(* x1 x2 ... xn)     The product of x1, x2, ..., xn
(- x y)         Subtract y from x
(/ x y)         Divide x by y
(rem x y)         The remainder of dividing x by y
(abs x)         The absolute value of x
(max x1 x2 ... xn)     The maximum of x1, x2, ..., xn
(min x1 x2 ... xn)    The minimum of x1, x2, ..., xn

(zerop n)        Test if n is zero
(setq a 3)        set a's value to 3

(list 1 2 3)        Construct a list (1 2 3)
'(1 2 3)        The same as above

(cons 1 (2 3))        Add a member to the front of a list
(append (1 2) (3 4))    Append the 2nd list after the 1st list
(first '(1 2 3))    Return 1st member of a list, return 1
(rest '(1 2 3))        Return rest of a list, except the 1st member, return (2 3)
(length '(1 2 3 2 1))    Return length of a list, return 5
(atom '1)        return true
(atom '(1 2))        return false
(listp '(1 2))        return true

4.基本语法
1) if
(if (condition)
    do-sth            ;;if condition is true
    do-other-things        ;;if condition is false   
)

2) cond
(cond (A B)            ;;if A is true, do B
      (C D)            ;;if C is true, do D
      (t E))

3) let
(let ((<vbl1>  <expr1> )
      ...............
      (<vbln>  <exprn> ))
      <body> )


4) dotimes
(dotimes (<counter>  <limit>  <result> ) <body> )

5) dolist
(dolist (<next-element>  <target-list>  <result> ) <body> )

5.基本数据结构
Association Lists
((<key1>  ...<expressions> )
 (<key2>  ...<expressions> )....)

    > (get 'mary 'age)
    NIL

    > (setf (get 'mary 'age) 45)
    45

    > (get 'mary 'age)
    45

Array
    > (setf my-vector (make-array '(3)))
    #(NIL NIL NIL)

    > (aref my-vector 2)
    NIL

    > (setf (aref my-vector 0) t)
    T

    > my-vector
    #(T NIL NIL)

    > (setf my-array (make-array '(2 3)))
    #2A((NIL NIL NIL) (NIL NIL NIL))

    > (aref my-array 0 1)
    NIL

    > (setf (aref my-array 0 1) 'hi)
    HI

    > (setf (aref my-array 1 0) 'bye)
    BYE

    > my-array
    #2A((NIL HI NIL) (BYE NIL NIL))

    > (make-array '(2 3 4) :initial-contents
                 '(((a b c d) (e f g h) (i j k l))
                   ((m n o p) (q r s t) (u v w x))))
    #3A(((A B C D) (E F G H) (I J K L)) ((M N O P) (Q R S T)
         (U V W X)))

Struct
    > (defstruct employee
       age
       first-name
       last-name
       sex
       children)
    EMPLOYEE

    > (setf employee1 (make-employee))
    #S(EMPLOYEE AGE NIL FIRST-NAME NIL LAST-NAME NIL SEX NIL
         CHILDREN NIL)

    > (employee-age employee1)
    NIL

    > (employee-sex employee1)
    NIL

    > (setf (employee-age employee1) 56)
    56

    > (employee-age employee1)
    56

    > (setf employee2 (make-employee :age 34
                                    :last-name 'farquharson
                                    :first-name 'alice
                                    :sex 'female))
    #S(EMPLOYEE AGE 34 FIRST-NAME ALICE LAST-NAME FARQUHARSON
          SEX FEMALE CHILDREN NIL)

    > (employee-first-name employee2)
    ALICE

    > (defstruct trekkie
        (sex 'male)
        (intelligence 'high)
        age)
    TREKKIE

    > (setf t1 (make-trekkie))
    #S(TREKKIE SEX MALE INTELLIGENCE HIGH AGE NIL)

    > (setf t2 (make-trekkie :age 35))
    #S(TREKKIE SEX MALE INTELLIGENCE HIGH AGE 35)

    > (setf t3 (make-trekkie :age 28 :sex 'female))
    #S(TREKKIE SEX FEMALE INTELLIGENCE HIGH AGE 28)

    > (typep t1 'employee)
    NIL

    > (typep t1 'trekkie)
    T

    > (trekkie-p t1)
    T

    > (employee-p t3)
    NIL





6.函数定义
关键字:defun
语法:(defun func (args) (expression))

例子:
(defun sum3 (a b c) (+ (+ a b) c))
调用:
(sum3 1 2 3)        输出6

7.递归
(defun sum (n) (if (= n 1)
            1
            (+ n (sum (- n 1)))
        )

8.IO
1) print
(print "test string")

2) format
(format <destination>  <control-string>  <optional-arguments> )

    > (format t "this")
    this
    NIL

    > (format nil "this")
    "this"

;Inserting ~% in the control string causes a newline to be output:
    > (format t "~%This shows ~%printing with ~%newlines.~%")

3) read
(defun f-to-c ()
  (format t "~%Please enter Fahrenheit temperature: ")
  (let* ((ftemp (read))
         (ctemp (* (- ftemp 32) 5/9)))
    (format t
            "~%~s degrees Fahrenheit is ~s degrees Celsius~%"
              ftemp
              (float ctemp))           ;; print floated value
    ctemp))                            ;; return ratio value

> (f-to-c)

Please enter Fahrenheit temperature: 56   ;; user enters 56

56 degrees Fahrenheit is 13.333333333333333 degrees Celsius

40/3

4) file io
(with-open-file (<stream>  <filename> ) <body> )

(with-open-file (infile "foo")
   (do ((result nil (cons next result))
    (next (read infile nil 'eof) (read infile nil 'eof)))
       ((equal next 'eof) (reverse result))))


9.载入文件
(load "add.lisp")

你可能感兴趣的:(clisp笔记)