Lisp_1.Clisp笔记
来源:互联网 发布:网络上被诈骗了怎么办 编辑:程序博客网 时间:2024/06/03 14:30
下面的代码大部分来自网络,版权归原作者所有。
规则:
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))))
- Lisp_1.Clisp笔记
- clisp笔记
- CLISP学习笔记
- CLISP special
- CLisp 4:读写文件
- clisp中的调试展开
- clisp和slime安装
- clisp 记录:6. 循环
- clisp 中的 cons
- CLISP 记录: 1.引言
- CLisp 12:使用CLisp的命令行模式的调试器
- CLisp 1:安装和运行
- Windows 下搭建Clisp环境
- CLisp 21:模块化构建程序
- CLisp 26:测试框架fiveam
- CLISP Tutorial 中英对照版(一)
- CLISP Tutorial 中英对照版(一)
- CLisp 2:设置自己的工作环境
- 我的android学习笔记35—AppWidget
- 最长平台的递归实现
- Java并发编程实践之线程的基本控制
- AutoCAD2012 致命错误 Unhandled Access Violation ...
- 行转列问题总结1 - 行转列
- Lisp_1.Clisp笔记
- [C] C程序100例_022
- Android frameworks层启动分析
- 经典书籍
- 多个web应用webapp. root重用问题
- 3-20
- 线程 NSCondition NSThread
- 在64位系统中运行32位命令提示符
- Struts2+Spring2.5+Hibernate3.5的整合开发