86 lines
3.6 KiB
Plaintext
86 lines
3.6 KiB
Plaintext
(eql x y) -- Tests of it's the same object
|
|
|
|
(setf x '(a b c))
|
|
(setf y x) -- This will copy the poiner, so any changes to either will affect the other.
|
|
|
|
(setf x '(a b c)
|
|
y (copy-list x)) -- This will actually create a new list (eql x y) will return nil
|
|
|
|
(append '(a b) '(c d) '(e)) -- append lists together. Will result in a single combined list.
|
|
|
|
(nth 0 '(a b c)) -- Find the nth element. In this cas 'A'
|
|
(nthcdr 1 '(a b c)) -- Find the nth cdr. In this case (B C)
|
|
|
|
(zerop n) -- Checks if value is 0
|
|
(last '(a b c)) -- Returns last cons in a list. So it'll return "(C) here
|
|
(car(last '(a b c))) -- Returns last element of a list. In this case "C"
|
|
|
|
--first - tenth are defined as functions that are not zero indexed so for example:
|
|
(fifth '(a b c d e f g h i)) -- Will return "E" (the element, not the cons)
|
|
|
|
(mapcar #'(lambda (x) (+ x 10))
|
|
'(1 2 3)) -- Takes a function and applies everything in the list to it. In this case it will return (11 12 13)
|
|
|
|
(mapcar #'list
|
|
'(a b c)
|
|
'(1 2 3 4)) -- Would return:
|
|
((A1) (B2) (C 3))
|
|
|
|
(member 'b '(a b c d e)) -- (member) finds what it's looking for, and returns it as well as the cdr from that point. So here we would see "b" - "e"
|
|
|
|
-- Keywords are a symbol preceded by a colon... so ":test" for example
|
|
-- Member defaults to testing with eql, perhaps we want equal instead:
|
|
-- NOTE: Equal is less strict than eql. Equal returns true if it's arguemnts print the same.
|
|
-- EQL only if they are the same object
|
|
(member '(a) '((a) (z)) :test #'equal) -- Will result in ((A) (Z))
|
|
|
|
--There is also :key
|
|
(member 'a '((a b) (c d)) :key #'car) -- will return ((A B) (C D)) -- We asked if there was an element whose car was a
|
|
-- keywords are always at the end, and multiple are acceptable.
|
|
|
|
If we want to find an element satisfying an arbitrary predicate, like oddp, which returns true for odd ints, we can use member-if
|
|
|
|
(member-if #'oddp '(2 3 4)) -- This will find the first odd number, 3 in this case, and return it along with the rest of the list
|
|
|
|
(adjoin) -- conses an object into a list if it's not already a member
|
|
(adjoin 'b '(a b c)) -- (A B C)
|
|
(adjoin 'z '(a b c)) -- (Z A 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) -- So, expected stuff
|
|
-- The above creates sets, and as we know, sets have no specific order
|
|
|
|
(length ' (a b c)) -- 3
|
|
|
|
-- Grab a part of a list
|
|
-- Think python slices, third argument (second number) is optional.
|
|
(subseq '(a b c d) 1 2) -- (B)
|
|
(subseq '(a b c d) 1) -- (B C D)
|
|
|
|
(reverse '(a b c)) -- Obvious, returns (A B C)
|
|
|
|
(subseq(reverse '(a b c)) 0 2) -- (C B) -- A way to grab the last two
|
|
(subseq(reverse '(a b c)) 1) -- (B A) -- Start with second to last and go backwards. Basically cut off the last (or however many ) element
|
|
|
|
(sort '(0 2 1 3 8) #'>) -- (8 3 2 1 0) -- Is destructive to the origional list. Of course, < would sort in order
|
|
|
|
(every #'oddp '(1 3 5)) -- Are they all odd? -- True
|
|
(some #'evenp '(1 2 3)) -- Are some of the even? -- True
|
|
|
|
(every #'> '(1 3 5) '(0 2 4)) -- Think math, is every element greater than it's corresponding element so...
|
|
-- is 1 > 0? AND is 3 >2 AND is 5>4? True
|
|
If sequences are different lengths, the shortests determines how many tests are performed.
|
|
|
|
(push x y) push object x onto the front of list y
|
|
(pop x) -- remove and return the first element of list x
|
|
|
|
(let ((x '(a b)))
|
|
(pushnew 'c x)
|
|
(pushnew 'a x)
|
|
x) -- This will only push to "x" if the object doesn't already exist.
|
|
|
|
-- Conses are not just for building lists, an improper list, or dotted list, can be used make a structure with two fields
|
|
(setf pair (cons 'a 'b)) -- (A . B)
|
|
-- car will return A, while cdr will return B
|