Special operators, functions, macros 0.0
format
CL-USER>(format t "hello world") ;t=>*standard-output*hello worldNILCL-USER>(format t "~a:~10t~a" "This is" "a shinny day")This is: a shinny dayNIL
~a=> beautify the output (such as String quotes, keywords to go to the leading colon), consume any one argument
~10t=> (a total of 10 spaces starting from the previous ~t)
~%=> line break
In short, variables start with ~
1. Function 1.0
y-or-n-p
Specialized input hint function
(y-or-n-p "continuing [y/n]?") ;当没有输入y Y N n时重新提示,输入这四个字符之一时则返回t or nil
1.1
DefVar、
Defparameter
Defining variables
CL-USER>(defvar *db* nil) ;定义变量值为nil(双*暗示这是一个全局变量)*DB*
A variable defined by DefVar can have no initial value, generally a persistent
Defparameter defined variables must have an initial value
1.2
Funcall、
Apply
With # ' mates, called functions that are packaged as data and then passed in
Funcall= number of actual arguments passed in is known
CL-USER>(funcall #‘(lambda (x y) (format t "~a:~a" x y)) 2 3)2:3NIL
Apply= unknown number of arguments passed in
Apply expects a list of parameters after passing in the function as data (there can be several orphaned arguments before the list is flexible)
1.3
Random
(Random number &optional state)
[0 Number]
1.4 Some mathematical functions 1.4.0 expt
CL-USER>expt(2 3)8
2 Special operator 2.0 if=> (if Test-form then-form [Else-form])
Exp:
(if x (format t"yes") (format t "no"))
Like the expression in C? x:y (the expression is true then X executes y otherwise), if-else
2.1 Block Evaluation 2.1.0 ' (quote)
Avoid the shorthand for ' or ' is the syntax sugar of the quote operator
CL-USER>(quote (+ 1 2))(+ 1 2)CL-USER>‘(+1 2)(+ 1 2)CL-USER>(+ 1 2)3
2.1.1 ' + anti-quote
And,, @ and other special symbols combined in the evaluation of not evaluated
CL-USER>`(0 ,(list 1 2 3))(0 (1 2 3))CL-USER>`(0 ,@(list 1 2 3))=>,@不仅后允求值而且将列表(必须是一个列表)的值提取出来(0 1 2 3)
2.2
EQL
Two Lisp objects to be sentenced and so on
equal More lenient allows the equivalent of a list with the same structure and content on a recursive view
Equalp More lenient allows ignoring the case when comparing strings when ignoring the same mathematical meaning of numbers equivalent to 1 and 1.0
eq is more stringent than eql , it does not guarantee that 1 is equivalent to 1, its use only a bit of the symbolic meaning of controversy
2.3
Return-from
Returns a specific value from a function-specific section (by default returns the value of the last expression at the last expression)
(defun foo (n) (dotimes (i 10) (dotimes (j 10) (when (> (* i j) n) ;;这个编辑器居然连括号匹配都没有。。。orz更不用说那硬编码风格的缩进。。。我对csdn开始有点失望
2.4 # ' (
function)
To package a function as data
2.5 lambda
Creating anonymous functions
(Lambda (parameters) body)
2.6 And, or, not
Connection expressions to support short-circuit evaluation
3 macros
The macros here are "functions" that generate code, and all macros are recursively expanded into functions and special forms of code when compiled with function Compile-file.
3.0
Defun
Defining functions
(defun name (parameter*)
"Optional documentation string."
body-form*)
CL-USER>(defun hello-world() (format t "hello world"))HELLO-WORLDCL-USER>(hello-world)hello world
3.0.1 &optional
Optional formal parameters
(defun foo (a b &optional (c 3 c-supplied-p) (d b) (list a b c c-supplied-p d))
Parameter optional optional parameters after &option can be added with default values and variables to detect if arguments are passed in (typically ending with-supplied-p)
The default value can be the previous required parameter
3.0.2 &rest
Remaining formal parameters
All remaining parameters that satisfy the necessary and optional parameters are collected into the remaining parameter list
(Defun + (&rest numbers) ...)
3.0.3 &key
Key Glyph parameter
Same as optional parameter (parameter name default value detects if the argument is passed in)
The call is preceded by a colon before the formal parameter
3.0.4 simultaneous use of multiple formal parameters
Necessary Parameters-"optional parameter-" remainder parameter-"key glyph parameter"
Obviously, key glyphs have an impact on the remaining parameters
And the optional participation causes the optional parameter to be eaten when the incoming extra argument cannot eat all the optional parameters.
CL-USER>(defun foo (x &optional y &key z))FOOCL-USER>(foo 1 :z 3) ;=>error
Therefore, it is recommended to mix the optional shapes to participate in the remaining parameters
3.1
SETF
Change the location of the macro
Since it is a macro, its scope must be quite wide.
(setf Name value)
3.1.03.1.1
INCF、
DECF
Self-increment \ Decrement (default = 1)
3.1.2
Aref
Gethash
Array access \ Hash Table lookup
(setf (aref a 0) 10) ;将数组a 的0号元素设置为10(setf (gethash ‘key hash) 10) ;将名为hash的哈希表检索项为key的值置为10
3.1.3
SHIFTF
Rotatef
Shifts values left/right between positions
(shiftf a b c)(rotatef a b c)
3.2
Let
Temporarily set certain variables to custom values in a control body, not affected outside the body (true • Multi-threaded)
(Let (parameter *) body-form*)
(Let ((x 1) (y 2) z) ...)
The function in the Let control body is closed (this sentence is awkward)
CL-USER>(defparameter *fn* (let ((count 0)) #‘(lambda () (setf count (1+ count)))))*FN*CL-USER>*fn*1CL-USER>*fn*2
3.3
Cond
Equivalent to a switch inside C
(cond a (test-1) b (test-2) t (test-final)) ;t=>非nil,相当于default
3.4
Do、
dolist、
Dotimes、
Loop
Loop related, can use return interrupt
3.4.0 dolist=> The loops on the convenience list, similar to those in Python
Map
(Dolist (Var list-form)
body-form*)
CL-USER>(dolist (x ‘(1 2 3)) (print x))123NIL
3.4.1 Dotimes=> Simple Counting cycle
(dotimes (var count-form) body-form*)CL-USER>(dolist (x 3) (print x))012NIL
3.4.2 Do=> Ah, the highest abstraction of the cycle, True and secret!!
(do (variable-definition*) ;(variable-definition)=>(var init-form next-value) (end-test-form result-form*) statement*)斐波那契数列第10个数CL-USER>(do ((n 0 (1+ n)) (cur 0 next) (next 1 (+ cur next)) ((= n 9) (return cur))) 34 ;返回值被设为curCL-USER>(= (1+ n) (+ 1 n)) ; =T
3.4.3 loop = A micro-language hidden in Lisp
Keyword Sendo
Across, and, below, collecting, counting, finally, for, from, summing, then, to
CL-USER>(loop for i from 1 to 10 collecting i)(1 2 3 4 5 6 7 8 9 10)CL-USER>(loop for x from 1 to 5 summing x)10CL-USER>(loop for x across "abc abc abc" counting (find x "ac"))6CL-USER>(loop for n below 9 and cur = 0 then next and next = 1 then (+ cur next) finally(return cur)) ;简直不是lisp了,总觉得括号少了点哪里不对劲(233),同样是求第十个斐波那契数字34
Keyword 0. Nil
False empty list (the only object that is an atom and a list)
1. T
True *standard-output*
Formatting habits 0. Indent
You should use the compiler's auto-indent to check for clerical errors instead of parentheses.
1.0 function Indent
At least four spaces, if the line is required, then the item at the same level should clearly be aligned
2.0 Macros and special forms
Two spaces
3.0 Semicolon
Keep the same indentation as the commented code
1. Brackets
Closing parentheses are always in the row of the last element of the list they are closing
Exp:
(defun foo () (dotimes (i 10) (format t "~d.hello~%"i)))
Do not force C language
2. Notes
Semicolons for comments
2.0;;;; The whole document should be annotated 2.1;;; The large segment code 2.2 followed by the comment; the next code 2.3; Just the line of the comment.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Common LISP Study Notes