






Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!







List Constuction > ( list 'a 'b 'c) --> (A B C) > ( list 'my (+ 2 1) "sons") --> (MY 3 "sons") > ( cons 'a nil) --> (A)
List Alteration > ( cons 'a '(b c d)) --> (A B C D) > ( append '(a b) '(c d) '(e)) --> (A B C D E) > ( adjoin 'z '(a b c)) --> (Z A B C) > ( adjoin 'b '(a b c)) --> (A B C)
> ( reverse '(a b c)) --> (C B A) > ( sort '(0 2 1 3 8) #'>) --> (8 3 2 1 0) > ( substitute 'b 'a '(a x a y)) --> (B X B Y)
List Access > ( length '(a b c)) --> 3 > ( count 'a '(a b b a a)) --> 3
> ( car '(a b c)) --> A > ( cdr '(a b c)) --> (B C) > ( third '(a b c d)) --> C ; to tenth > ( last '(a b c)) --> (C) > ( butlast '(a b c d)) --> (A B C) > ( butlast '(a b c d) 2) --> (A B) > ( nth 0 '(a b c)) --> A ; 0-indexed! > ( nthcdr 2 '(a b c)) --> (C) ; 0-indexed!
> ( subseq '(a b c d e) 1) --> (B C D E) > ( subseq '(a b c d e) 1 3) --> (B C)
Removing Items > (setf lst '(c a r a t)) --> (C A R A T) > ( remove 'a lst) --> (C R T) > lst --> (C A R A T) > (setf lst ( remove 'a lst)) --> (C R T) > lst --> (C R T)
Stack Ops 1. These are all destructive!!!
> (setf x '(b)) --> (B) > ( push 'a x) --> (A B) > x --> (A B) > (setf y x) --> (A B) > ( pop x) --> A > x --> (B) > y --> (A B)
> (setf x '(a b)) --> (A B) > ( pushnew 'c x) --> (C A B) > x --> (C A B) > ( pushnew 'a x) --> (C A B)
List Copying > (setf x ( copy-list lst))
Set Ops > ( member 'b '(a b c)) --> (B C) > ( union '(a b c) '(c b s)) --> (A C B S) > ( intersection '(a b c) '(b b c)) --> (B C) > ( set-difference '(a b c d e) '(b e)) --> (A C D)
> ( member 'a '((a b) (c d)) : key #'car) ((A B) (C D))
—————————————————————————————————————————————————————————————————————————————
if-then Stmts ( if [test] [then do this] [else do this]) > (if (listp '(a b c)) (+ 1 2) (+ 5 6)) --> 3
when/unless ( when ([test]) Statements ([do this]) ([do this]) ([do this, which returns value]))
Case Statements ( cond ((= x 1) (format t "One. ~%")) ((= x 2) (format t "Two. ~%")) ((= x 3) (format t "Three. ~%")) ( t (format t "Default. ~%")))
Case Statements (defun month-length (mon) With Constants ( case mon ((jan mar may jul aug oct dec) 31) ((apr jun sep nov) 30) (feb (if (leap-year) 29 28)) ( otherwise "Unknown Month.")))
Pointer Behavior > (setf x '(a b c)) --> (A B C) > (setf y x) --> (A B C) > (eql x y) --> T
Local Vars > (let ((x 1) (y 2)) (+ x y)) --> 3
Local Vars With > ( let* ((x 1) --> 3 Dependence (y (+ x 1))) (+ x y))
Global Vars ( **defparameter *** maxHits ***** 93) ; * notation = convention
Global Constants ( defconstant limit 30)
Basic Functions ( defun name (parameters) (what to do))
(defun is-a-member (obj lst) (if (null lst) nil (if (eql obj (car lst)) lst (is-a-member obj (cdr lst)))))
Multiple Variable (defun foo (x &rest y ) Arg Lists (dolist (num y) (format t "~A * ~A = ~A ~%" x num (* x num))))
> (foo 4 5) 4 * 5 = 20
> (foo 4 5 10 20) 4 * 5 = 20 4 * 10 = 40 4 * 20 = 80
Optional (defun foo (x &optional y ) Arguments (list x 'is y))
(defun bar (x &optional (y 'green) ) (list x 'is y))
> (foo 'lisp) --> (LISP IS NIL) > (bar 'lisp) --> (LISP IS GREEN) > (foo 'lisp 'fun) --> (LISP IS FUN) > (bar 'lisp 'fun) --> (LISP IS FUN)
Lambda > (( lambda (x) (+ x 100)) 1) Expressions 101 > ( funcall #'( lambda (x) (+ x 100))
101
Apply a Fn to > ( apply #'+ '(1 2 3)) --> 6 Multiple Args > ( funcall #'+ 1 2 3) --> 6
Apply Fns to > ( mapcar #'(lambda (x) (+ x 10)) Lists '(1 2 3)) (11 12 13)
> ( mapcar #'list '(a b c) '(1 2 3 4)) ((A 1) (B 2) (C 3))
Apply Fn to > ( maplist #'(lambda (x) x) '(a b c)) Successive cdr's ((A B C) (B C) (C))
Return Multiple > ( values 'a nil (+ 2 4)) Values A NIL 6
Receive Multiple > ( multiple-value-bind (x y z) Values ( values 1 2 3) (list x y z)) (1 2 3)
Pass on Multiple > ( multiple-value-call #'+ (values 1 2 3)) Values 6
Incrementing & > (setf x 5) --> 5 Decrementing > ( incf x) --> 6 > x --> 6 > ( decf x 4) --> 2
Basic Math Stuff > ( abs -6.3) --> 6. > ( mod 23 5) --> 3 > ( mod 25 5) --> 0 > ( max 1 2 3 4 5) --> 5 > ( min 1 2 3 4 5) --> 1 > ( expt 2 5) --> 32 > ( float 1) --> 1. > ( float .5) --> 0. > ( float 2/3) --> 0. > ( numerator 2/3) --> 2 > ( denominator 2/3) --> 3
Basic Vectors > (setf vec ( make-array 4 :initial-element nil)) #(NIL NIL NIL NIL) > ( setf ( svref vec 1) 'blah) BLAH > vec #(NIL BLAH NIL NIL) > ( svref vec 1) BLAH
> ( vector 'blah 34 "hello") #(BLAH 34 "hello")
—————————————————————————————————————————————————————————————————————————————
Basic Structs > ( defstruct point x y) POINT > (setf p ( make-point :x 0 :y 0)) #S(POINT :X 0 :Y 0) > ( point-x p) 0 > ( setf ( point-x p) 2) 2 > ( point-x p) 2
Custom Print ( defstruct (point ( :print-function print-point)) Fns for Structs (x 0) (y 0))
( defun print-point (point stream depth) (format stream "<~A, ~A>" (point-x p) (point-y p)))
> (make-point) <0,0>
—————————————————————————————————————————————————————————————————————————————
Basic String Fns > ( format nil "~A or ~A" "x" "y") --> "x or y" > ( concatenate 'string "x " "y") --> "x y"
> ( char "abc" 1) --> #\b > ( char "a b c" 1) --> #\space > ( position #\a "fantasia") --> 1
> (setf str "merlin") > ( setf ( char str 3) #\k) --> #\k > str --> "merkin"
> ( string-capitalize "foo") --> "Foo" > ( string-upcase "foo") --> "FOO" > ( string-downcase "Foo") --> "foo"
> ( equal "fred" "fred") --> T > ( equal "fred" "Fred") --> NIL > ( string-equal "fred" "Fred") --> T
> ( sort "elbow" #'char<) --> "below" > ( remove-duplicates "abracadabra") --> "cdbra"
String string=, string/=, string<, string>, string<=, string>= Comparisons
—————————————————————————————————————————————————————————————————————————————
Basic Printing ( format [where to output] [a $ template] [other args])
> (format t "~A plus ~A equals ~A.~%" 2 3 (+ 2 3)) 2 plus 3 equals 5. NIL ; returned since format was called at top level
Printing 1. Use ~F template Decimals 2. Rounding behavior not guaranteed!
5 Args
> (format nil " ~,2F " 26.21875) "26.22"
Other String ~% newline ~A variable Templates ~3% 3 newlines ~S print as string
System File (setf mypath ( make-pathname :name "myfile.txt")) Functions (setf newpath ( make-pathname :name "newfile.txt")) ( delete-file mypath) ( rename-file mypath newpath)
Transcribe a ( dribble mypath) Lisp Session
Define a Macro ( defmacro nil! (x) (list 'setf x nil))
> (setf x 3) --> 3 > x --> 3 > (nil! x) --> NIL > x --> NIL
Expand a Macro > ( macroexpand-1 '(nil! x)) (SETF X NIL) T
—————————————————————————————————————————————————————————————————————————————
My Library Fns > ( nthmost 2 '(0 2 1 3 8)) --> 3
> ( starts-with '(a b c) 'a) --> T > ( starts-with '(a b c) 'b) --> NIL > ( rotate-left '(a b c d)) --> (B C D A) > ( rotate-right '(a b c d)) --> (D A B C) > ( insert-between 'x '(a b c d)) --> (A X B X C X D)
> ( permutations '(1 2)) ((1 2) (2 1)) > ( permutations '(1 2 3)) ((3 1 2) (3 2 1) (2 1 3) (2 3 1) (1 2 3) (1 3 2))
> ( palindrome-p '(a)) --> T > ( palindrome-p '(a a)) --> T > ( palindrome-p '(a b a)) --> T > ( palindrome-p '(a b b a)) --> T
Not Included Yet! Assoc Lists Loop
Dotted Lists Packages Property Lists Binary Streams Hash Tables Type Specifiers Garbage Explicit Type Declarations Exceptions CLOS Symbol Names w/ Whitespace pprint Tree Functions Backquotes Coerce Macro Design Issues Compile
———————————————————————————————————————————————————————————————————————————