id
int64
0
45.1k
file_name
stringlengths
4
68
file_path
stringlengths
14
193
content
stringlengths
32
9.62M
size
int64
32
9.62M
language
stringclasses
1 value
extension
stringclasses
6 values
total_lines
int64
1
136k
avg_line_length
float64
3
903k
max_line_length
int64
3
4.51M
alphanum_fraction
float64
0
1
repo_name
stringclasses
779 values
repo_stars
int64
0
882
repo_forks
int64
0
108
repo_open_issues
int64
0
90
repo_license
stringclasses
8 values
repo_extraction_date
stringclasses
146 values
sha
stringlengths
64
64
__index_level_0__
int64
0
45.1k
exdup_ids_cmlisp_stkv2
sequencelengths
1
47
17,064
ex-08-11.lisp
joncdavid_touretzky-lisp-exercises/chp8-recursion/ex-08-11.lisp
; file: ex-08-11.lisp ; author: Jon David ; date: Monday, August 18, 2014 ; description: ; exercise 8.11 - Define a fibonacci function. Where ; Fib(0) and Fib(1) both equal 1. (defun fib (n) (cond ((equal n 0) 1) ((equal n 1) 1) (t (+ (fib (- n 1)) (fib (- n 2))))))
276
Common Lisp
.lisp
10
26.1
54
0.611321
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
13c361d6c535187f794f539938070226f826d9a8a3ce6bb81d43bac09882cea1
17,064
[ -1 ]
17,065
ex-08-58.lisp
joncdavid_touretzky-lisp-exercises/chp8-recursion/ex-08-58.lisp
; file: ex-08-58.lisp ; author: Jon David ; date: Monday, September 29, 2014 ; description: ; exercise 8.58 - Write MERGE-LISTS, a function that takes two lists ; of numbers, each in increasing order, as input. The function should ; return a list that is a merger of the elements in its inputs, in ; order. (MERGE-LISTS '(1 2 6 8 10 12) '(2 3 5 9 13)) should return ; (1 2 2 3 5 6 8 9 10 12 13). (defun merge-lists (A B) (cond ((null A) B) ((null B) A) ((< (car A) (car B)) (cons (car A) (merge-lists (cdr A) B))) (t (cons (car B) (merge-lists A (cdr B)))))) ; unit test for exercise 8.58 (defun ut-08-58 () (let* ((A '(1 2 6 8 10 12)) (B '(2 3 5 9 13)) (expected '(1 2 2 3 5 6 8 9 10 12 13)) (actual (merge-lists A B))) (equal expected actual)))
781
Common Lisp
.lisp
21
35.047619
71
0.629482
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
ff18178068273533a3b4681b68613f2182f1451f3388d2eabacbbe99f380958f
17,065
[ -1 ]
17,066
ex-08-36.lisp
joncdavid_touretzky-lisp-exercises/chp8-recursion/ex-08-36.lisp
; file: ex-08-36.lisp ; author: Jon David ; date: Monday, August 25, 2014 ; description: ; exercise 8.36 - The function COUNT-ODD counts the number of odd ; elements in a list of numbers; for example, (COUNT-ODD '(4 5 6 7 8)) ; should return two. Show how to write COUNT-ODD using conditional ; augmentation. Then write another version of COUNT-ODD using the ; regular augmenting recursion template. (To do this you will need ; to write a conditional expression for the augmentation value.) (defun count-odd (num-list) (cond ((null num-list) 0) ((oddp (car num-list)) (+ 1 (count-odd (cdr num-list)))) (t (count-odd (cdr num-list))))) (defun count-odd-2 (num-list) (cond ((null num-list) 0) (t (+ (if (oddp (car num-list)) 1 0) (count-odd (cdr num-list))))))
789
Common Lisp
.lisp
19
39.578947
72
0.691406
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
0ef0b6f8e9381d9fa4351570e216a7102f29921fad881d479fadf27faf4a7997
17,066
[ -1 ]
17,067
ex-05-01.lisp
joncdavid_touretzky-lisp-exercises/chp5-variables-and-side-effects/ex-05-01.lisp
; file: ex-05-01.lisp ; author: Jon David ; date: Tuesday, July 15, 2014 ; description: ; exercise 5.1 - Rewrite function POOR-STYLE to create a new local ; variable Q using LET, instead of using SETF to change P. Call your ; new function GOOD-STYLE. (defun poor-style (p) ; This is the original definition of POOR-STYLE as defined in p.140. (setf p (+ p 5)) (list 'result 'is p)) (defun good-style (p) (let ((q (+ p 5))) (list 'result 'is q))) (defun ut-ex-05-01 () ; Unit test for exercise 5.1. Returns NIL if POOR-STYLE and ;GOOD-STYLE are not the same. (let ((p 10)) (equal (poor-style p) (good-style p))))
637
Common Lisp
.lisp
19
31.473684
70
0.677524
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
4f751ec3da051154a204acc8f7825eaf2e198c3dbb83ce585118e32fc902d7f8
17,067
[ -1 ]
17,068
ex-05-02.lisp
joncdavid_touretzky-lisp-exercises/chp5-variables-and-side-effects/ex-05-02.lisp
; file: ex-05-02-thru-05.lisp ; author: Jon David ; date: Tuesday, July 15, 2014 ; description: ; exercises 5.2 thru 5.5 ; Q5.2: What is a side effect? ; Q5.3: What is the difference between a local and global variable
227
Common Lisp
.lisp
7
30.857143
67
0.714286
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
039d61f8cca4ee407ff987b5b670567e2951cfe0f5f613af18067b80b9d9e2fd
17,068
[ -1 ]
17,069
ex-05-06-keyboard-exercise.lisp
joncdavid_touretzky-lisp-exercises/chp5-variables-and-side-effects/ex-05-06-keyboard-exercise.lisp
; file: ex-05-06-keyboard-exercise.lisp ; author: Jon David ; date: Tuesday, July 15, 2014 ; description: ; exercise 5.6 - This keyboard exercise is about dice. We will start ; with a function to throw one die and end up with a program to play ; craps. Be sure to include a documentation string for each function ; you write. ; a) Write a function THROW-DIE that returns a random number from 1 to ; 6, inclusive. Remember that (RANDOM 6) will pick numbers from 0 ; to 5. THROW-DIE doesn't need any inputs, so its argument should ; be NIL. (defun throw-die () ;Throws a 6-sided die whose values range from 1 to 6, inclusive. (+ (random 6) 1)) ; b) Write a function THROW-DICE that throws two dice and returns a ; list of two numbers: the value of the first die and the value ; of the second. We'll call this list a "throw." For example, ; (THROW-DICE) might return the throw (3, 5), indicating that ; the first die was a 3 and the second a 5. (defun throw-dice () (list (throw-die) (throw-die))) ; c) Throwing two ones is called "snake eyes"; two sixes is called ; "boxcars." Write predicates SNAKE-EYES-P and BOXCARS-P that ; take a throw as input and returns T if the throw is equal to ; (1 1) or (6 6), respectively. (defun snake-eyes-p (throw) (let ((snake-eyes '(1 1))) (equal throw snake-eyes))) (defun boxcars-p (throw) (let ((boxcars '(6 6))) (equal throw boxcars))) ; d) In playing craps, the first throw of the dice is crucial. A throw ; of 7 or 11 is an instant win. A throw of 2, 3, or 12 is ; an instant loss (American casino rules). Write predicates ; INSTANT-WIN-P and INSTANT-LOSS-P to detect thse conditions. Each ; should take a throw as input. (defun instant-win-p (throw) ; Determines if the throw is an instant win. A throw of 7 or 11 ; is an instant win. Returns NIL if not an instant win. (let ((sum (+ (car throw) (cadr throw)))) (or (equal sum 7) (equal sum 11)))) (defun instant-loss-p (throw) ; Determines if the throw is an instant loss. A throw of 2, 3, or 12 ; is an instant loss. Returns NIL if not an instant loss. (let ((sum (+ (car throw) (cadr throw)))) (or (equal sum 2) (equal sum 3) (equal sum 12)))) ;; Continue from here next time... ; e) Write a function SAY-THROW that takes a throw as input and ; returns either the sum of the two dice or the symbol SNAKE-EYES ; or BOXCARS if the sum is 2 or 12. (SAY-THROW '(3 4)) should return ; 7. (SAY-THROW '(6 6)) should returns BOXCARS. (defun say-throw (throw) (let ((sum (+ (car throw) (cadr throw)))) (cond ((snake-eyes-p throw) 'snake-eyes) ((boxcars-p throw) 'boxcars) (t sum)))) ; f) If you don't win or lose on the first throw of the dice, the ; value you threw becomes your "point," which will be explained ; shortly. Write a function, CRAPS, that produces the following sort ; of behavior. Your solution should make use of the functions you ; wrote in previous steps. (defun craps (throw) (let* ((die1 (car throw)) (die2 (cadr throw)) (point (+ die1 die2))) (cond ((instant-loss-p throw) (list 'throw die1 'and die2 '-- (say-throw throw) '-- 'you 'lose)) ((instant-win-p throw) (list 'throw die1 'and die2 '-- (say-throw throw) '-- 'you 'win)) (t (list 'throw die1 'and die2 '-- (say-throw throw) '-- 'your 'point 'is point))))) ; g) Once a point has been established, you continue throwing the ; dice until you either win by making the point again or lose by ; throwing a 7. Write the function TRY-FOR-POINT that simulates this ; part of the game, as follows: ; (try-for-point 6) -> (THROW 3 AND 5 -- 8 -- THROW AGAIN) ; (try-for-point 6) -> (THROW 5 AND 1 -- 6 -- YOU WIN) ; (craps) -> (THROW 3 AND 6 -- YOUR POINT IS 9) ; (try-for-point 9) -> (THROW 6 AND 1 -- 7 -- YOU LOSE) (defun try-for-point (target-point) (let* ((throw (throw-dice)) (die1 (car throw)) (die2 (cadr throw)) (point (+ die1 die2))) (cond ((instant-win-p throw) (list 'THROW die1 'AND die2 '-- (say-throw throw) '-- 'you 'win)) ((instant-loss-p throw) (list 'THROW die1 'AND die2 '-- (say-throw throw) '-- 'you 'lose)) ((equal point target-point) (list 'throw die1 'and die2 '-- (say-throw throw) '-- 'you 'win)) (t (list 'throw die1 'and die2 '-- (say-throw throw) '-- 'you 'lose))))) ;===================================================================== ; Unit tests for exercise 5.6. ;--------------------------------------------------------------------- (defun ut-throw-dice () ; Unit test for THROW-DICE. Returns NIL if unit test fails. (let* ((throw (throw-dice)) (die-1 (car throw)) (die-2 (cadr throw))) (and (<= die-1 6) (>= die-1 1) (<= die-2 6) (>= die-2 1)))) (defun ut-snake-eyes-p () ; Unit test for SNAKE-EYES-P. Returns NIL if unit test fails. (and (snake-eyes-p '(1 1)) (not (snake-eyes-p '(6 6))) (not (snake-eyes-p '(1 2))) (not (snake-eyes-p '(5 5))) )) (defun ut-boxcars-p () ; Unit test for BOXCARS-P. Returns NIL if unit test fails. (and (boxcars-p '(6 6)) (not (boxcars-p '(1 1))) (not (boxcars-p '(1 2))) (not (boxcars-p '(5 5))) )) (defun ut-instant-win-p () ; Unit test for INSTANT-WIN-P. Returns NIL if unit test fails. (and (instant-win-p '(4 3)) (instant-win-p '(9 2)) (not (instant-win-p '(1 1))) (not (instant-win-p '(6 6))) (not (instant-win-p '(4 4))) )) (defun ut-instant-loss-p () ; Unit test for INSTANT-LOSS-P. Returns NIL if unit test fails. (and (instant-loss-p '(1 1)) (instant-loss-p '(2 1)) (instant-loss-p '(10 2)) (not (instant-loss-p '(2 2))) (not (instant-loss-p '(4 5))) (not (instant-loss-p '(10 1))) )) (defun ut-say-throw () ; Unit test for SAY-THROW. Returns NIL if unit test fails. (and (equal 'snake-eyes (say-throw '(1 1))) (equal 'boxcars (say-throw '(6 6))) (equal 3 (say-throw '(2 1))) (equal 5 (say-throw '(3 2))))) (defun ut-craps () ; Unit test for CRAPS. Returns NIL if unit test fails. (let ((msg1 '(THROW 5 AND 2 -- 7 -- YOU WIN)) (msg2 '(THROW 5 AND 6 -- 11 -- YOU WIN)) (msg3 '(THROW 1 AND 1 -- snake-eyes -- YOU LOSE)) (msg4 '(THROW 6 AND 6 -- boxcars -- YOU LOSE))) (and (equal msg1 (craps '(5 2))) (equal msg2 (craps '(5 6))) (equal msg3 (craps '(1 1))) (equal msg4 (craps '(6 6)))))) (defun ut-ex-05-06-all () ; Unit test for all exercise 5.6 functions. Returns NIL if ; any of the unit tests fail. (and (ut-throw-dice) (ut-snake-eyes-p) (ut-boxcars-p) (ut-instant-win-p) (ut-instant-loss-p) (ut-craps)))
6,606
Common Lisp
.lisp
152
40.381579
100
0.629998
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
c80846c67344356904ddc92358ee311efc07611b02b186deb0cecfe80c80426b
17,069
[ -1 ]
17,070
ex-07-01.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-01.lisp
; file: ex-07-01.lisp ; author: Jon David ; date: Sunday, August 03, 2014 ; description: ; exercise 7.1 - Write an ADD1 function that adds one to its input. ; Then write an expression to add one to each element of the list ; (1 3 5 7 9). ; (defun add1 (n) (+ 1 n)) (defun ut-add1 () (let* ((test-list '(1 3 5 7 9)) (actual (mapcar #'add1 test-list)) (expected '(2 4 6 8 10))) (equal actual expected)))
424
Common Lisp
.lisp
15
26.2
69
0.649383
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
7039406072f0c57ab104844786f4205c7a8e115d71cd91b0313c0ab819b6c2a6
17,070
[ -1 ]
17,071
ex-07-29-keyboard-exercise.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-29-keyboard-exercise.lisp
; file: ex-07-29-keyboard-exercise.lisp ; author: Jon David ; date: Thursday, August 07, 2014 ; description: ; exercise 7.29 - If the blocks database is already stored on the ; computer for you, load the file containing it. If not, you will ; have to type it in as it appears in Figure 7-3 (page 223). Save ; the database in the global variable DATABASE. (defvar database '((b1 shape brick) (b1 color green) (b1 size small) (b1 supported-by b2) (b1 supported-by b3) (b2 shape brick) (b2 color red) (b2 size small) (b2 supports b1) (b2 left-of b3) (b3 shape brick) (b3 color red) (b3 size small) (b3 supports b1) (b3 right-of b2) (b4 shape pyramid) (b4 color blue) (b4 size large) (b4 supported-by b5) (b5 shape cube) (b5 color green) (b5 size large) (b5 supports b4) (b6 shape brick) (b6 color purple) (b6 size large))) ; a) Write a function MATCH-ELEMENT that takes two symbols as inputs. ; If the two are equal, or if the second is a question mark, ; MATCH-ELEMENT should return T. Otherwise it should return NIL. ; Thus (MATCH-ELEMENT 'RED 'RED) and (MATCH-ELEMENT 'RED '?) should ; return T, but (MATCH-ELEMENT 'RED 'BLUE) should return NIL. Make ; sure your function works corretly before proceeding further. (defun match-element (sym-1 sym-2) (or (equal sym-1 sym-2) (equal sym-2 '?))) (defun ut-match-element () (let* ((test-sym-1 'red) (test-sym-2 'red) (test-sym-3 'blue) (actual-1 (match-element test-sym-1 test-sym-2)) (expected-1 t) (actual-2 (match-element test-sym-1 test-sym-3)) (expected-2 nil) (actual-3 (match-element test-sym-1 '?)) (expected-3 t)) (and (equal actual-1 expected-1) (equal actual-2 expected-2) (equal actual-3 expected-3)))) ; b) Write a function MATCH-TRIPLE that takes an assertion and a ; pattern as input, and returns T if the assertion matches the ; pattern. Both inptus will be three-element lists. ; (MATCH-TRIPLE '(B2 COLOR RED) '(B2 COLOR ?)) should return T. ; (MATCH-TRIPLE '(B2 COLOR RED) '(B1 COLOR GREEN)) should return ; NIL. Hint: Use MATCH-ELEMENT as a building block. (defun match-triple (assertion pattern) (every #'match-element assertion pattern)) (defun ut-match-triple () (let* ((test-assertion '(b2 color red)) (test-pattern-1 '(b2 color ?)) (test-pattern-2 '(b1 color green)) (actual-1 (match-triple test-assertion test-pattern-1)) (expected-1 t) (actual-2 (match-triple test-assertion test-pattern-2)) (expected-2 nil)) (and (equal actual-1 expected-1) (equal actual-2 expected-2)))) ; c) Write the function FETCH that takes a pattern as input and ; returns all assertions in the database that match the pattern. ; Remember that DATABASE is a global variable. ; (FETCH '(B2 COLOR ?)) should return ((B2 COLOR RED)), and ; (FETCH '(? SUPPORTS B1)) should return ((B2 SUPPORTS B1) ; (B3 SUPPORTS B1)). (defun fetch (pattern) (remove-if-not #'(lambda (assertion) (match-triple assertion pattern)) database)) (defun ut-fetch () (let* ((test-pattern '(b2 color ?)) (actual (fetch test-pattern)) (expected (list '(b2 color red)))) (equal actual expected))) ; d) Use FETCH with patterns you construct yourself to answer ; the following questions: ; What shape is block B4? (fetch '(b4 shape ?)) ; Which blocks are bricks? (fetch '(? shape brick)) ; What relation is block B2 to block B3? (fetch '(b2 ? b3)) ; List the color of every block. (fetch '(? color ?)) ; What facts are known about block B4? (fetch '(b4 ? ?)) ; e) Write a function that takes a block name as input and returns a ; pattern asking the color of a block. For example, given the input ; B3, your function should return the list (B3 COLOR ?). (defun color-pattern-of (blockname) (list blockname 'color '?)) (defun ut-color-pattern-of () (let* ((blockname 'b3) (actual (color-pattern-of blockname)) (expected '(b3 color ?))) (equal actual expected))) ; f) Write a function SUPPORTERS that takes one input, a block, and ; returns a list of the blocks that support it. (SUPPORTERS 'B1) ; should return the list (B2 B3). Your function should work by ; constructing a pattern containing the block's name, using that ; pattern as input to FETCH, and then extracting the block names ; from the resulting list of assertions. (defun supporters (blockname) (mapcar #'car (fetch (list '? 'supports blockname)))) (defun ut-supporters () (let* ((blockname 'b1) (actual (supporters blockname)) (expected '(b2 b3))) (equal actual expected))) ; g) Write a predicate SUPP-CUBE that takes a block as input and ; returns true if that block is supported by a cube. ; (SUPP-CUBE 'B4) should return a true value; (SUPP-CUBE 'B1) ; should not because B1 is supported by bricks but not cubes. ; Hint: use the result of the supporters function as a starting ; point. (defun supp-cube (blockname) (let ((supporters-list (supporters blockname)) (cube-list (car (fetch '(? shape cube))))) (not (null (intersection supporters-list cube-list))))) ; h) We are going to write a DESCRIPTION functino that returns the ; description of a block. (DESCRIPTION 'B2) will return ; (SHAPE BRICK COLOR RED SIZE SMALL SUPPORTS B1 LEFT-OF B3). ; We will do this in steps. First, write a function DESC1 that ; takes a block as input and returns all assertions dealing with ; that block. (DESC1 'B6) should return ((B6 SHAPE BRICK) ; (B6 COLOR PURPLE) (B6 SIZE LARGE)). (defun desc1 (blockname) (fetch (list blockname '? '?))) ; i) Write a function DESC2 of one input that calls DESC1 and strips ; the block name off each element of the result. (DESC 'B6) should ; return the list ((SHAPE BRICK) (COLOR PURPLE) (SIZE LARGE)). (defun desc2 (blockname) (mapcar #'cdr (desc1 blockname))) ; j) Write the DESCRIPTION function. It should take one input, call ; DESC2, and merge the resulting list of lists into a single list. ; (DESCRIPTION 'B6) should return (SHAPE BRICK COLOR PURPLE SIZE ; LARGE). (defun description (blockname) (reduce #'append (desc2 blockname))) ; k) What is the description of block B1? of block B4? ; B1: (SHAPE BRICK COLOR GREEN SIZE SMALL SUPPORTED-BY B2 ; SUPPORTED-BY B3) ; B4: (SHAPE PYRAMID COLOR BLUE SIZE LARGE SUPPORTED-BY B5) ; ; l) Block B1 is made of wood, but block B2 is made of plastic. How ; Would you add this information to the database. ; ; Answer: ; Append these properties to the list. ; (append '(b1 material wood)) ; (append '(b2 material plastic)).
6,709
Common Lisp
.lisp
160
39.26875
70
0.691576
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
e80d3eba8b724fbba0c59be90a60fea79fb77b06f11fa1a3748828d7f961cb3d
17,071
[ -1 ]
17,072
ex-07-14.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-14.lisp
; file: ex-07-14.lisp ; author: Jon David ; date: Monday, August 04, 2014 ; description: ; exercise 7.14 - Here is a version of SET-DIFFERENCE written with ; REMOVE-IF: ; ; (defun my-setdiff (x y) ; (remove-if #'(lambda (e) (member e y)) ; x)) ; ; Show how the INTERSECTION and UNION functions can be written using ; REMOVE-IF or REMOVE-IF-NOT. (defun my-intersection (X Y) (remove-if-not #'(lambda (e) (member e Y)) X)) (defun my-union (X Y) (append X (remove-if #'(lambda (e) (member e X)) Y)))
537
Common Lisp
.lisp
18
28.222222
70
0.639535
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
e0b2248958d54cec63e3c010d351f70733a58830fbece026b66333e83b4e75af
17,072
[ -1 ]
17,073
ex-07-02.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-02.lisp
; file: ex-07-02.lisp ; author: Jon David ; date: Sunday, August 03, 2014 ; description: ; exercise 7.2 - Let the global variable DAILY-PLANET contain the ; following table: (setf daily-planet '((olsen jimmy 123-76-4535 cub-reporter) (kent clark 089-52-6787 reporter) (lane lois 951-26-1438 reporter) (white perry 355-16-7439 editor))) ; Each table entry consists of a last name, a first name, a social ; security number, and a job title. Use MAPCAR on this table to ; extract a list of social security numbers. (defun ut-ex-07-02 () (let* ((actual (mapcar #'third daily-planet)) (expected '(123-76-4535 089-52-6787 951-26-1438 355-16-7439))) (equal actual expected)))
709
Common Lisp
.lisp
17
38.764706
67
0.707849
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
74174657fdbfd58014fbd64f234ba2859f257149654456e357f60efba864cacf
17,073
[ -1 ]
17,074
ex-07-11.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-11.lisp
; file: ex-07-11-keyboard-exercise.lisp ; author: Jon David ; date: Sunday, August 03, 2014 ; description: ; exercise 7.11 - Write a function to pick out those numbers in a ; list that are greater than one and less than five. (defun pick-numbers (numbers-list) (remove-if-not #'(lambda (n) (and (> n 1) (< n 5))) numbers-list)) (defun ut-pick-numbers () (let* ((test-numbers '(-1 0 1 2 3 4 5 6)) (actual (pick-numbers test-numbers)) (expected '(2 3 4))) (equal actual expected)))
499
Common Lisp
.lisp
13
36.230769
68
0.677019
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
1771e3f5874ffa27ab6de1171a84b7cb03972d87ad0ba302936427dbb91c64e0
17,074
[ -1 ]
17,075
ex-07-04.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-04.lisp
; file: ex-07-04.lisp ; author: Jon David ; date: Sunday, August 03, 2014 ; description: ; exercise 7.4 - Suppose we want to solve a problem similar to the ; preceding one, but instead of testing whether an element is zero, ; we want to test whether it is greater than five. We can't use > ; directly because > is a function of two inputs; MAPCAR will only ; give it one input. Show how first writing a one-input function ; called GREATER-THAN-FIVE-P would help. (defun greater-than-five-p (n) (> n 5)) (defun ut-ex-07-04 () (let* ((test-list '(2 0 3 4 0 -5 6)) (actual (mapcar #'greater-than-five-p test-list)) (expected '(nil nil nil nil nil nil t))) (equal actual expected)))
704
Common Lisp
.lisp
17
39.647059
69
0.699708
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
ea3d90d8000c9b5f7dc00ac6afacfa90ef7cdc53eba1aed029455dca860085a5
17,075
[ -1 ]
17,076
ex-07-27.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-27.lisp
; file: ex-07-27.lisp ; author: Jon David ; date: Thursday, August 07, 2014 ; description: ; exercise 7.27 - Show how to write EVERY given REMOVE-IF. ; EVERY returns true if predicate returns true for all elements. ; REMOVE-IF will return an empty list if predicate is true for all ; elements. ; REMOVE-IF = EVERY, only when REMOVE-IF returns an empty list. (defun ut-ex-07-27 () (let* ((test-list '(-2 0 2 4 5 6)) (actual (every #'evenp test-list)) (expected (null (remove-if #'evenp test-list)))) (equal actual expected)))
539
Common Lisp
.lisp
14
36.714286
66
0.708015
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
762a71f2c52ec701e4c1f4feea61ec33bbcb26e5bfdda876c00087413dc72912
17,076
[ -1 ]
17,077
ex-07-03.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-03.lisp
; file: ex-07-03.lisp ; author: Jon David ; date: Sunday, August 03, 2014 ; description: ; exercise 7.3 - Write an expression to apply the ZEROP predicate to ; each element of the list (2 0 3 4 0 -5 -6). The answer you get ; should be a list of Ts and NILs. (defun ut-ex-07-03 () (let* ((test-list '(2 0 3 4 0 -5 -6)) (actual (mapcar #'zerop test-list)) (expected '(nil t nil nil t nil nil))) (equal actual expected)))
438
Common Lisp
.lisp
12
34.416667
70
0.661939
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
346e7b5ab19cad7a32eb1c3a6bcc9f67c2f03cbcba9141d86f64e58cac5a289b
17,077
[ -1 ]
17,078
ex-07-12.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-12.lisp
; file: ex-07-12.lisp ; author: Jon David ; date: Monday, August 04, 2014 ; description: ; exercise 7.12 - Write a function that counts how many times the ; word "the" appears in a sentence. (defun count-thes (sentence) (length (remove-if-not #'(lambda (n) (equal n 'the)) sentence))) (defun ut-count-thes () (let* ((test-sentence '(the cat in the hat)) (actual (count-thes test-sentence)) (expected 2)) (equal actual expected)))
450
Common Lisp
.lisp
13
32.384615
67
0.690531
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
c192ba005f38cec2017fcbf9dc37e2c5a571245a475a3c90d6ab483c35b4bd2c
17,078
[ -1 ]
17,079
ex-07-10-keyboard-exercise.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-10-keyboard-exercise.lisp
; file: ex-07-10-keyboard-exercise.lisp ; author: Jon David ; date: Sunday, August 03, 2014 ; description: ; exercise 7.10 - In this exercise we will write a program to ; transpose a song from one key to another. In order to manipulate ; notes more efficiently, we will translate them into numbers. Here ; is the correspondence between notes and numbers for a one-octave ; scale. ; a) Write a table to represent this information. Store it in a global ; variable called NOTE-TABLE. (defvar note-table '((c 1) (c-sharp 2) (d 3) (d-sharp 4) (e 5) (f 6) (f-sharp 7) (g 8) (g-sharp 9) (a 10) (a-sharp 11) (b 12))) ; b) Write a function called TO-NUMBERS that takes a list of notes as ; input and returns the corresponding list of numbers. ; (NUMBERS '(E D C D E E E)) should return (5 3 1 3 5 5 5). This ; list represents the first seven notes of "Mary Had a Little ; Lamb". (defun to-numbers (notes-list) (mapcar #'(lambda (n) (cadr (assoc n note-table))) notes-list)) (defun ut-to-numbers () (let* ((test-notes '(e d c d e e e)) (actual (to-numbers test-notes)) (expected '(5 3 1 3 5 5 5))) (equal actual expected))) ; c) Write a function called TO-NOTES that takes a list of numbers as ; input and returns the corresponding list of notes. ; (NOTES '(5 3 1 3 5 5 5)) should return (E D C D E E E). ; Hint: Since NOTE-TABLE is assigned by note, ASSOC can't look up ; numbers in it; neither can RASSOC, since the elements are lists, ; not dotted pairs. Write your own table-searching function to ; search NOTE-TABLE by number instead of by note. (defun number-to-note (number) (car (find-if #'(lambda (n) (equal number (cadr n))) note-table))) (defun to-notes (numbers-list) (mapcar #'(lambda (n) (number-to-note n)) numbers-list)) (defun ut-number-to-note () (let* ((test-number 12) (actual (number-to-note test-number)) (expected 'b)) (equal actual expected))) (defun ut-to-notes () (let* ((test-numbers '(5 3 1 3 5 5 5)) (actual (to-notes test-numbers)) (expected '(e d c d e e e))) (equal actual expected))) ; d) Notice that NOTES and NUMBERS are mutual inverses: ; For X a list of notes: X = (NOTES (NUMBERS X)), ; For Y a list of numbers: Y = (NUMBERS (NOTES Y)). ; ; Question: ; What can be said about (NOTES (NOTES Y)) and ; (NUMBERS (NUMBERS X)).? ; ; Answer: Both expressions result in error. ; Given that Y is a list of numbers, (NOTES Y) returns a list ; of notes. But the NOTES function expects its input to be ; numbers, not symbols. Vice-versa for the other expression. ; ; (note: my implmentation calls these functions to-notes and ; to-numbers, respectively.) ; e) To transpose a piece of music up by n half steps, we begin ; by adding the value n to each note in the piece. Write a function ; called RAISE that takes a number n and list a numbers as input ; and raises each number in the list by the value of n. ; (RAISE 5 '(5 3 1 3 5 5 5)) should return (10 8 6 8 10 10 10), ; which is "Mary Had a Little Lamb" transposed five half-steps from ; the key of C to the key of F. (defun raise (n numbers-list) (mapcar #'(lambda (k) (+ n k)) numbers-list)) (defun ut-raise () (let* ((test-numbers '(5 3 1 3 5 5 5)) (n 5) (actual (raise n test-numbers)) (expected '(10 8 6 8 10 10 10))) (equal actual expected))) ; f) Sometimes when we raise the value of a note, we may raise it ; right into the next octave. For instance, if we raise the triad ; C-E-G represented by the list (1 5 8) into the key of F by adding ; five to each note, we get (6 10 13), or F-A-C. Here the C note, ; represented by the number 13, is an octave above the regular C, ; represented by 1. Write a function called NORMALIZE that takes a ; list of numbers as input and "normalizes" them to make them be ; between 1 and 12. A number greater than 12 should have 12 ; subtracted from it; a number less than 1 should have 12 adde to ; it. (NORMALIZE '(6 10 13)) should return (6 10 1). (defun normalize (numbers-list) (mapcar #'(lambda (n) (cond ((> n 12) (- n 12)) ((< n 1) (+ n 12)) (t n))) numbers-list)) (defun ut-normalize () (let* ((test-numbers '(6 10 13)) (actual (normalize test-numbers)) (expected '(6 10 1))) (equal actual expected))) ; g) Write a function TRANSPOSE that takes a number n and a song as ; input, and returns the song transposed by n half steps. ; (TRANSPOSE 5 '(E D C D E E E)) should return (A G F G A A A). ; Your solution should assume the availability of the NUMBERS, ; NOTES, RAISE, AND NORMALIZE functions. Try transposing "Mary Had ; a Little Lamb" by 11 half steps. What happens if you transpose it ; by 12 half steps? How about -1 half-steps? (defun transpose (n notes-list) (let ((normalized-list (normalize (raise n (to-numbers notes-list))))) (to-notes normalized-list))) (defun ut-transpose () (let* ((test-notes '(e d c d e e e)) (n-1 5) (actual-1 (transpose n-1 test-notes)) (expected-1 '(a g f g a a a)) (n-2 11) (actual-2 (transpose n-2 test-notes)) (expected-2 '(d-sharp c-sharp b c-sharp d-sharp d-sharp d-sharp)) (n-3 -1) (actual-3 (transpose n-3 test-notes)) (expected-3 expected-2)) (and (equal actual-1 expected-1) (equal actual-2 expected-2) (equal actual-3 expected-3))))
5,488
Common Lisp
.lisp
136
37.610294
70
0.664353
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
f9080a978da9976d6f31a89fd05da7dd42a2413377461e666b82bf19f0c3d0c4
17,079
[ -1 ]
17,080
ex-07-30.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-30.lisp
; file: ex-07-30.lisp ; author: Jon David ; date: Saturday, August 09, 2014 ; description: ; exercise 7.30 - Recall the English-French dictionary we stored in ; the global variable WORDS earlier in the chapter. Given this ; dictionary plus the list or corresponding Spanish words (UNO DOS ; TRES QUATRO CINCO), write an expression to return a trilingual ; dictionary. The first entry of the dictionary should be (ONE UN UNO). (defvar words '((one un) (two deux) (three trois) (four quatre) (five cinq))) (defvar spanish-words '(uno dos tres quatro cinco)) (defvar trilingual-dictionary '((one un uno) (two deux dos) (three trois tres) (four quatre quatro) (five cinq cinco))) (defun merge-dictionary (english-french spanish) (mapcar #'(lambda (ef s) (append ef (list s))) english-french spanish)) (defun ut-merge-dictionary () (let* ((english-french words) (spanish spanish-words) (actual (merge-dictionary english-french spanish)) (expected trilingual-dictionary)) (equal actual expected)))
1,056
Common Lisp
.lisp
30
32.4
73
0.72549
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
b9eda7d0fe52c5cb3ca5612533e4002536b73e4add356f20dbab908140fd8e3c
17,080
[ -1 ]
17,081
ex-07-16.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-16.lisp
; file: ex-07-16.lisp ; author: Jon David ; date: Thursday, August 07, 2014 ; description: ; exercise 7.16 - Suppose we had a list of sets: ; ((A B C) (C D A) (F B D) (G)) ; ; that we wanted to collapse into one big set. If we use APPEND for ; for our reducing function, the result won't be a true set, because ; some elements will appear more than once. What reducing function ; should be used instead? ; Answer: UNION (defun ut-ex-07-16 () (let* ((test-set '((a b c) (c d a) (f b d) (g))) (actual-set (reduce #'union test-set)) (expected-set '(a b c d f g))) ; the two sets are equal if their differences are both nil. (and (equal nil (set-difference actual-set expected-set)) (equal nil (set-difference expected-set actual-set)))))
763
Common Lisp
.lisp
19
38.473684
70
0.675639
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
6d7997644b45671ff6680aa10ae4888a29cbe6f2ad734de413633ca9eebb6b37
17,081
[ -1 ]
17,082
ex-07-15-keyboard-exercise.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-15-keyboard-exercise.lisp
; file: ex-07-15.lisp ; author: Jon David ; date: Wednesday, August 06, 2014 ; description: ; exercise 7.15 - In this keyboard exercise we will manipulate ; playing cards with applicative operators. A card will be ; represented by a list of form (rank suit), for example (ACE ; SPADES) or (2 CLUBS). A hand will be represented by a list of ; cards. ; a) Write the functions RANK and SUIT that return the rank and suit ; of a card, respectively. (RANK '(2 CLUBS)) should return 2, and ; (SUIT '(2 CLUBS)) should returns CLUBS. (defun rank (card) (car card)) (defun suit (card) (cadr card)) (defun ut-rank () (let* ((test-card-1 '(2 clubs)) (test-card-2 '(3 hearts)) (actual-1 (rank test-card-1)) (actual-2 (rank test-card-2)) (expected-1 2) (expected-2 3)) (and (equal actual-1 expected-1) (equal actual-2 expected-2)))) (defun ut-suit () (let* ((test-card-1 '(2 clubs)) (test-card-2 '(3 hearts)) (actual-1 (suit test-card-1)) (actual-2 (suit test-card-2)) (expected-1 'clubs) (expected-2 'hearts)) (and (equal actual-1 expected-1) (equal actual-2 expected-2)))) ; b) Set the global variable MY-HAND to the following hand of cards: (defvar my-hand '((3 hearts) (5 clubs) (2 diamonds) (4 diamonds) (ace spades))) ; Now write a function COUNT-SUIT that takes two inputs, a suit ; and a hand of cards, and returns the number of cards belonging ; to that suit. (COUNT-SUIT 'DIAMONDS MY-HAND) should return 2. (defun count-suit (suit hand) (length (remove-if-not #'(lambda (card) (equal suit (suit card))) hand))) (defun ut-count-suit () (let* ((test-hand my-hand) (test-suit 'diamonds) (actual (count-suit test-suit test-hand)) (expected 2)) (equal actual expected))) ; c) Set the global variable COLORS to the following table: (defvar colors '((clubs black) (diamonds red) (hearts red) (spades black))) ; Now write a function COLOR-OF that uses the table COLORS to ; retrieve the color of a card. (COLOR-OF '(2 CLUBS)) should return ; BLACK. (COLOR-OF '(6 HEARTS)) should return RED. (defun color-of (card) (let ((suit (suit card))) (cadr (assoc suit colors)))) (defun ut-color-of () (let* ((test-card-1 '(2 clubs)) (test-card-2 '(6 hearts)) (actual-1 (color-of test-card-1)) (actual-2 (color-of test-card-2)) (expected-1 'black) (expected-2 'red)) (and (equal actual-1 expected-1) (equal actual-2 expected-2)))) ; d) Write a function FIRST-RED that returns the first card of a hand ; that is of a red suit, or NIL if none are. (defun first-red (hand) (find-if #'(lambda (card) (equal (color-of card) 'red)) hand)) (defun ut-first-red () (let* ((test-hand my-hand) (actual (first-red test-hand)) (expected '(3 hearts))) (equal actual expected))) ; e) Write a function BLACK-CARDS that returns a list of all the black ; cards in a hand. (defun black-cards (hand) (remove-if-not #'(lambda (card) (equal (color-of card) 'black)) hand)) (defun ut-black-cards () (let* ((test-hand my-hand) (actual (black-cards test-hand)) (expected '((5 clubs) (ace spades)))) (equal actual expected))) ; f) Write a function WHAT-RANKS that takes two inputs, a suit and a ; hand, and returns the ranks of all cards belonging to that suit. ; (WHAT-RANKS 'DIAMONDS MY-HAND) should return the list (2 4). ; (WHAT-RANKS 'SPADES MY-HAND) should return the list (ACE). Hint: ; First extract all the cards of the specified suit, then use ; another operator to get all the ranks of those cards. (defun what-ranks (suit hand) (let ((suit-cards (remove-if-not #'(lambda (card) (equal (suit card) suit)) hand))) (mapcar #'car suit-cards))) (defun ut-what-ranks () (let* ((test-hand my-hand) (actual (what-ranks 'diamonds test-hand)) (expected '(2 4))) (equal actual expected))) ; g) Set the global variable ALL-RANKS to the list (defvar all-ranks '(2 3 4 5 6 7 8 9 10 jack queen king ace)) ; Then write a predicate higher-rank-p that takes two cards as input ; and returns true if the first card has a higher rank than the second ; Hint: look at the BEFOREP predicate on page 171 of Chapter 6. (defun higher-rank-p (card-1 card-2) (member card-2 (member card-1 all-ranks))) (defun ut-higher-rank-p () (let* ((test-card-1 '(4 hearts)) (test-card-2 '(5 hearts)) (actual (higher-rank-p test-card-1 test-card-2)) (expected nil)) (equal actual expected))) ; h) Write a function HIGH-CARD that returns the highest ranked card ; in a hand. Hint: One way to solve this is to use FIND-IF to ; search a list of ranks (ordered from high to low) to find the ; highest rank that appears in the hand. Another solution would ; be to use REDUCE (defined in the next section) to repeatedly ; pick the highest card of each pair. (defun high-card (hand) (assoc (find-if #'(lambda (r) (assoc r hand)) (reverse all-ranks)) hand)) (defun ut-high-card () (let* ((test-hand my-hand) (actual (high-card test-hand)) (expected '(ace spades))) (equal actual expected)))
5,126
Common Lisp
.lisp
140
33.7
70
0.679386
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
3fe0da443cae822e8c72956d234f77091ab86365575d9feecf1ac6cc437f1a69
17,082
[ -1 ]
17,083
ex-07-09.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-09.lisp
; file: ex-07-09.lisp ; author: Jon David ; date: Sunday, August 03, 2014 ; description: ; exercise 7.9 - Write a function FIND-NESTED that returns the first ; element of a list that is itself a non-NIL list. (defun find-nested (list) (find-if #'(lambda (n) (and (listp n) (not (null n)))) list)) (defun ut-find-nested () (let* ((test-list '(nil () a (a b))) (actual (find-nested test-list)) (expected '(a b))) (equal actual expected)))
475
Common Lisp
.lisp
15
28.4
70
0.646667
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
39d26e2474d4beed23fec3f61534eb54d238f103e5d93305e382312b92f80c11
17,083
[ -1 ]
17,084
ex-07-13.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-13.lisp
; file: ex-07-13.lisp ; author: Jon David ; date: Monday, August 04, 2014 ; description: ; exercise 7.13 - Write a function that picks from a list of lists ; those of exactly length two. (defun pick-nested-lists-size-2 (list) (remove-if-not #'(lambda (n) (and (listp n) (equal 2 (length n)))) list)) (defun ut-pick-nested-list-size-2 () (let* ((test-list '(a (a) (a b) (c d) (e f g))) (actual (pick-nested-lists-size-2 test-list)) (expected '((a b) (c d)))) (equal actual expected)))
515
Common Lisp
.lisp
15
31.533333
68
0.642424
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
b23efb8888d0b9bd2af9c45e82c017559d09fdb22d4e2ed705a3e6703285041c
17,084
[ -1 ]
17,085
ex-07-17.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-17.lisp
; file: ex-07-17.lisp ; author: Jon David ; date: Thursday, August 07, 2014 ; description: ; exercise 7.17 - Write a funciton that, given a list of lists, ; returns the total length of all the lists. This problem can be ; solved two different ways. (defun num-elements (list-of-lists) (reduce #'+ (mapcar #'length list-of-lists))) (defun ut-num-elements () (let* ((test-list-of-lists '((a) (a b) (a b c))) (actual (num-elements test-list-of-lists)) (expected 6)) (equal actual expected)))
509
Common Lisp
.lisp
14
34.357143
66
0.687627
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
f5a3155042de7c4c9bd48cffdf06d85b79a8782186591af49067c6486d9ced43
17,085
[ -1 ]
17,086
ex-07-19.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-19.lisp
; file: ex-07-19.lisp ; author: Jon David ; date: Thursday, August 07, 2014 ; description: ; exercise 7.19 - Write a function ALL-ODD that returns T if every ; element of a list of numbers is odd. (defun all-odd (numbers-list) (every #'oddp numbers-list)) (defun ut-all-odd () (let* ((test-list '(-1 1 3 5 7)) (actual (all-odd test-list)) (expected t)) (equal actual expected)))
398
Common Lisp
.lisp
13
28.461538
69
0.678851
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
6b72a1f89012454fcaa6d6ec48ccf3817d5c05163b0691538493883abfef7b5c
17,086
[ -1 ]
17,087
ex-07-05.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-05.lisp
; file: ex-07-05.lisp ; author: Jon David ; date: Sunday, August 03, 2014 ; description: ; exercise 7.5 - Write a lambda expression to subtract seven from ; a number. (defun ut-ex-07-05 () (let* ((test-list '(0 1 2 3 4 5 6 7 8)) (actual (mapcar #'(lambda (n) (- n 7)) test-list)) (expected '(-7 -6 -5 -4 -3 -2 -1 0 1))) (equal actual expected)))
361
Common Lisp
.lisp
11
30.818182
67
0.621777
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
b52c118a6908ae2183395bc92451c42f273fcd2b125a1de86411aa2776ac428b
17,087
[ -1 ]
17,088
ex-07-06.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-06.lisp
; file: ex-07-06.lisp ; author: Jon David ; date: Sunday, August 03, 2014 ; description: ; exercise 7.6 - Write a lambda expression that returns T if its ; input is T or NIL, but NIL for any other input. (defun ut-ex-07-06 () (let* ((test-list '(t nil 'a '(a) '(a b) '((a) (b)))) (actual (mapcar #'(lambda (n) (or (equal n t) (equal n nil))) test-list)) (expected '(t t nil nil nil nil))) (equal actual expected)))
443
Common Lisp
.lisp
13
31.230769
66
0.619159
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
fccf4af81dc98c95805d17e069a0434beec182a4a713ada75151b03e6d57fb89
17,088
[ -1 ]
17,089
ex-07-26.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-26.lisp
; file: ex-07-26.lisp ; author: Jon David ; date: Thursday, August 07, 2014 ; description: ; exercise 7.26 - Show how to write FIND-IF given REMOVE-IF-NOT. (defun ut-ex-07-26 () (let* ((test-list '(-2 0 2 4 5 6)) (actual (find-if #'oddp test-list)) (expected (car (remove-if-not #'oddp test-list)))) (print test-list) (print actual) (print expected) (equal actual expected)))
399
Common Lisp
.lisp
13
28.076923
66
0.656331
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
6334ab5faf0d5397855014791499d6b5f3610f9c338338a7bb4434737cdd462e
17,089
[ -1 ]
17,090
ex-07-21.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-21.lisp
; file: ex-07-21.lisp ; author: Jon David ; date: Thursday, August 07, 2014 ; description: ; exercise 7.21 - Write a function NOT-ALL-ODD that returns T if not ; every element of a list of numbers is odd. (defun not-all-odd (numbers-list) (not (every #'oddp numbers-list))) (defun ut-not-all-odd () (let* ((test-list '(-1 1 3 4 5)) (actual (not-all-odd test-list)) (expected t)) (equal actual expected)))
426
Common Lisp
.lisp
13
30.461538
70
0.681373
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
9e27e5d64e650e735c2e0d04238f93c11648b87bf93dffa9a4f833c9c771216e
17,090
[ -1 ]
17,091
ex-07-07.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-07.lisp
; file: ex-07-07.lisp ; author: Jon David ; date: Sunday, August 03, 2014 ; description: ; exercise 7.7 - Write a function that takes a list such as ; (UP DOWN UP UP) and "flips" each element, returning ; (DOWN UP DOWN DOWN). Your function should include a lambda ; expression that knows how to flip an individual element, ; plus an applicative operator to do this to every element ; of the list. (defun flip (list) (mapcar #'(lambda (n) (cond ((equal n 'up) 'down) ((equal n 'down) 'up) (t nil))) list)) (defun ut-flip () (let* ((test-list '(up down up up)) (actual (flip test-list)) (expected '(down up down down))) (equal actual expected)))
694
Common Lisp
.lisp
21
30.238095
62
0.665172
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
d2dbad10be027d115f676205e429aaef24d6db948309f1eae49138625b28d1a0
17,091
[ -1 ]
17,092
ex-07-08.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-08.lisp
; file: ex-07-08.lisp ; author: Jon David ; date: Sunday, August 03, 2014 ; description: ; exercise 7.8 - Write a function that takes two inputs, X and K, and ; returns the first number in the list X that is roughly equal to K. ; Let's say that "roughly equal" means no less than K-10 and no more ; than K+10. ; Returns the first number in the list X that is (k-10)<=k<=(k+10). (defun roughly-equal-p (X k) (find-if #'(lambda (n) (and (>= n (- k 10)) (<= n (+ k 10)))) X)) (defun ut-roughly-equal-p () (let* ((test-list '(-13 -9 -3 0 3 6 9 12)) (k 0) (actual (roughly-equal-p test-list k)) (expected -9)) (equal actual expected)))
675
Common Lisp
.lisp
20
30.95
71
0.626728
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
9cf553a91b742160b5d65c74cf3fbe04512074a40c6db105288b49a0d55c5531
17,092
[ -1 ]
17,093
ex-07-22.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-22.lisp
; file: ex-07-22.lisp ; author: Jon David ; date: Thursday, August 07, 2014 ; description: ; exercise 7.22 - Write a function NOT-NONE-ODD that returns T if it ; is not the case that a list of numbers contains no odd elements. (defun not-none-odd (numbers-list) (not (every #'evenp numbers-list))) (defun ut-not-none-odd () (let* ((test-list '(-2 0 2 4 5 6)) (actual (not-none-odd test-list)) (expected t)) (equal actual expected)))
455
Common Lisp
.lisp
13
32.615385
70
0.690367
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
9b28c5dd9971b29bd95b1f48f3fe9e481ee40337d2a844d09b93801ebffe2530
17,093
[ -1 ]
17,094
ex-07-20.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-20.lisp
; file: ex-07-20.lisp ; author: Jon David ; date: Thursday, August 07, 2014 ; description: ; exercise 7.20 - Write a function NONE-ODD that returns T if every ; element of a list of numbers is not odd. (defun none-odd (numbers-list) (every #'evenp numbers-list)) (defun ut-none-odd () (let* ((test-list '(-2 0 2 4 6 8)) (actual (none-odd test-list)) (expected t)) (equal actual expected)))
409
Common Lisp
.lisp
13
29.307692
69
0.684478
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
d04aa88b345f6589f561e9c5e738f445b069ca304e0049e8e1b4e49c8bdab181
17,094
[ -1 ]
17,095
ex-07-18.lisp
joncdavid_touretzky-lisp-exercises/chp7-applicative-programming/ex-07-18.lisp
; file: ex-07-18.lisp ; author: Jon David ; date: Thursday, August 07, 2014 ; description: ; exercise 7.18 - (REDUCE #'+ NIL) returns 0, but (REDUCE #'* NIL) ; returns 1. Why do you think this is? ; ; answer: ; NIL is equivalent to an empty list. In addition, adding zero to ; anything is the identity property. In multiplication, multiplying ; anything by 1 is the identity property. Maybe Lisp wants to ; preserve arithmetic identity properties.
460
Common Lisp
.lisp
12
37.333333
69
0.727679
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
662349581886e02b8ee35f669ae28b2731e5460709f70249c3ae496370a0ca76
17,095
[ -1 ]
17,096
ex-06-11.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-11.lisp
; file: ex-06-11.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.11 - Write a function MAKE-PALINDROME that makes a ; palindrome out of a list, for example, given (YOU AND ME) as input ; it should return (YOU AND ME ME AND YOU). (load "ex-06-10.lisp") (defun make-palindrome (list) (append list (reverse list))) ;===================================================================== ; Unit Tests (defun ut-make-palindrome () (let* ((quote '(YOU AND ME)) (palin-quote (make-palindrome quote)) (expected-quote '(YOU AND ME ME AND YOU))) (equal palin-quote expected-quote)))
632
Common Lisp
.lisp
17
35.176471
70
0.622951
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
0c2ab8b0866e0b56a4a9820327bcbd44a88f2195857c66d735bae2fca22bcd8b
17,096
[ -1 ]
17,097
ex-06-18.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-18.lisp
; file: ex-06-18.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.18 - Write a function ADD-VOWELS that takes a set of ; letters as input and adds the vowels (A E I O U) to the set. For ; example, calling ADD-VOWELS on the set (X A E Z) should produce ; the set (X A E Z I O U), except that the exact order of the ; elements in the result is unimportant. (defun add-vowels (set) (let ((vowel-set '(a e i o u))) (union vowel-set set))) ;===================================================================== ; Unit tests (defun ut-add-vowels () ; Unit test for ADD-VOWELS. Returns NIL if a vowel wasn't added ; to the input set. (let* ((test-set1 '(X A E Z)) (new-set (add-vowels test-set1))) (and (member 'a new-set) (member 'e new-set) (member 'i new-set) (member 'o new-set) (member 'u new-set))))
872
Common Lisp
.lisp
24
34.208333
70
0.614472
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
f36f40ceca412ec7af0b703290da241e00aa4b75b61aac1d63611c076adf6073
17,097
[ -1 ]
17,098
ex-06-42.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-42.lisp
; file: ex-06-42.lisp ; author: Jon David ; date: Sunday, July 27, 2014 ; description: ; exercise 6.42 - Write a function called ROYAL-WE that changes ; every occurence of the symbol I in a list to the symbol WE. ; Calling this function on the list (IF I LEARN LISP I WILL BE ; PLEASED) should return the list (IF WE LEARN LISP WE WILL BE ; PLEASED). (defun royal-we (list) (subst 'we 'i list)) (defun ut-royal-we () (let* ((sentence '(if i learn lisp i will be pleased)) (actual (royal-we sentence)) (expected '(if we learn lisp we will be pleased))) (equal actual expected)))
604
Common Lisp
.lisp
16
35.75
65
0.702055
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
d3331c0e9314ca4aeff4c190a64449a20399006ba4f66d75e4da8965111719e9
17,098
[ -1 ]
17,099
ex-06-25.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-25.lisp
; file: ex-06-25.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.25 - A set X is a proper subset of a set Y if X ; is a subset of Y but not equal to Y. Thurs, (A C) is a proper ; subset of (C A B). (A B C) is a subset of (C A B), but not a ; proper subset of it. Write the PROPER-SUBSETP predicate, which ; returns T if its first input is a proper subset of its second ; input. (load "ex-06-24.lisp") (defun proper-subsetp (X Y) (and (subsetp X Y) (not (set-equal X Y)))) ;===================================================================== ; Unit tests (defun ut-proper-subset () (and (proper-subsetp '(A C) '(C A B)) (not (proper-subsetp '(A B C) '(C A B)))))
738
Common Lisp
.lisp
19
36.578947
70
0.580645
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
81c129787c7cefcd80e0fbcaf69ce5bb9d9caacba4af6c6bf4fdce39adba5750
17,099
[ -1 ]
17,100
ex-06-09.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-09.lisp
; file: ex-06-09.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.9 - What primitive function does the following ; reduce to? ; ; answer: MYSTERY is equivalent to CAR. (defun mystery (x) (first (last (reverse x)))) ;===================================================================== ; Unit Tests (defun ut-mystery () ; Unit test to test the claim that (mystery x) = (car x). Returns NIL ; if this claim is false. (let ((quote '(roses are red))) (equal (mystery quote) (car quote))))
538
Common Lisp
.lisp
17
29.941176
70
0.597679
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
55fe0259b2fac50696a85180a71466e1cf078e932e8c6254ee244f26d9524d12
17,100
[ -1 ]
17,101
ex-06-10.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-10.lisp
; file: ex-06-10.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.10 - A palindrome is a sequence that reads the same ; forwards and backwards. The list (A B C D C B A) is a palindrome; ; (A B C A B C) is not. Write a function PALINDROMEP that returns T ; if its input is a palindrome. (defun palindromep (list) (equal (reverse list) list)) ;===================================================================== ; Unit Tests (defun ut-palindromep () (and (equal t (palindromep '(A B C D C B A))) (equal nil (palindromep '(A B C A B C)))))
598
Common Lisp
.lisp
15
37.866667
70
0.599309
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
b46bc088d9befbc16e1eb7895a64a52823c4d38500529f2a9a3f98fedbe08355
17,101
[ -1 ]
17,102
ex-06-02.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-02.lisp
; file: ex-06-02.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.3 - What is the value of (NTH 3 '(A B C . D) ; and why? ; ; solution: ; (NTH 3 '(A B C . D)) produces an error. It takes three CDRs ; of its input, which produces the symbol D. Taking the CAR ; of D then causes an error because D is not a list.
358
Common Lisp
.lisp
11
31.545455
63
0.665706
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
3b8732bef3d7f1df8f76b83175a3edb1369e241122ca7a0342990aafb1c2e66e
17,102
[ -1 ]
17,103
ex-06-40.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-40.lisp
; file: ex-06-40.lisp ; author: Jon David ; date: Sunday, July 27, 2014 ; description: ; exercise 6.40 - Show how to transform the list (A B C D) into a ; table so that the ASSOC function using the table gives the same ; result as MEMBER using the list. (setf x-list '(a b c d)) (setf x-table '((A B C D) (B C D) (C D) (D))) (defun ut-ex-06-40 () (and (equal (member 'a x-list) (assoc 'a x-table)) (equal (member 'b x-list) (assoc 'b x-table)) (equal (member 'c x-list) (assoc 'c x-table)) (equal (member 'd x-list) (assoc 'd x-table))))
577
Common Lisp
.lisp
17
31
67
0.624101
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
bdcbb3c2af99e9b543046439d4f1f6d468399fa05a3ec45be08dd16017d369ca
17,103
[ -1 ]
17,104
ex-06-07.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-07.lisp
; file: ex-06-07.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.7 - Use REVERSE to write a NEXT-TO-LAST function that ; returns the next-to-last element of a list. Write another version ; using NTH. (defun next-to-last-a (list) (cadr (reverse list))) (defun next-to-last-b (list) (let ((n (length list))) (nth (- n 2) list))) ;===================================================================== ; Unit Tests (defun ut-next-to-last-a () (let ((quote '(roses are red))) (equal 'are (next-to-last-a quote)))) (defun ut-next-to-last-b () (let ((quote '(roses are red))) (equal 'are (next-to-last-b quote))))
678
Common Lisp
.lisp
20
31.5
70
0.585253
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
94c1412b6a6462bdf7e307e04a204dc4136143f3044b2caf2c44e85c7e5ae8ce
17,104
[ -1 ]
17,105
ex-06-24.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-24.lisp
; file: ex-06-24.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.24 - Sets are said to be equal if they contain exactly ; the same elements. Order does not matter in a setm so the sets ; (RED BLUE GREEN) and (GREEN BLUE RED) are considered equal. ; However, the EQUAL predicate does not consider them equal, because ; it treats them as lists, not as sets. Write a SET-EQUAL predicate ; that returns T if two things re equal as sets. (Hint: if two sets ; are equal, then each is a subset of the other.) (defun set-equal (X Y) (and (subsetp X Y) (subsetp Y X))) ;===================================================================== ; Unit tests (defun ut-set-equal () (and (set-equal '(RED BLUE GREEN) '(GREEN BLUE RED)) (set-equal '(A B) '(B A)) (not (set-equal '(A B C) '(A B D))) (not (set-equal '(A B C) '(A B C D)))))
909
Common Lisp
.lisp
21
40.571429
70
0.608597
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
ee7f720f8edf64b3035b6933659f9ef4ad657f008717b973b8baf2778c6bf269
17,105
[ -1 ]
17,106
ex-06-30.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-30.lisp
; file: ex-06-30.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.30 - Make a table called BOOKS of five books and their ; authors. The first entry might be (WAR-AND-PEACE LEO-TOLSTOY). ; Defines the BOOKS table. (setf BOOKS '((war-and-peace leo-tolstoy) (gentle-introduction-to-common-lisp david-touretzky) (pride-and-prejudice jane-austen) (frankenstein mary-shelly) (dracula bram-stoker)))
464
Common Lisp
.lisp
12
35.083333
69
0.712695
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
abf1f0a5e31a5fec66ea8a719c60ee13584ef122847f5cfcc77ce9859d27fac9
17,106
[ -1 ]
17,107
ex-06-26.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-26.lisp
; file: ex-06-26.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.26 - We are going to write a program that compare the ; descriptions of two objects and tells how many features they have ; in common. The descriptions will be represented as a list of ; features, with the symbol -VS- separating the first object from ; the second. Thus, when given a list like ; ; (large red shiny cube -vs- small shiny red four-sided pyramid) ; ; the program will respond with (2 common features). We will ; compose this program from several small functions that you will ; write and test one at a time. ; a) Write a function RIGHT-SIDE that returns all the features to ; the right of the -VS-symbol. RIGHT-SIDE of the list shown above ; should return (SMALL SHINY RED FOUR-SIDED PYRAMID). ; Hint: remember that the MEMBER function returns the entire ; sublist starting with the item for which you are searching. ; Test your function to make sure it works correctly. (defun right-side (list) (cdr (member '-vs- list))) ; b) Write a function LEFT-SIDE that returns all the features to the ; left of the -VS-. You can't use the MEMBER trick directly for ; this one, but you can use it if you do something to the list ; first. (defun left-side (list) (cdr (member '-vs- (reverse list)))) ; c) Write a function COUNT-COMMON that returns the number of features ; the left and right sides of the input have in common. (defun count-common (list) (let* ((right-side (right-side list)) (left-side (left-side list)) (common-set (intersection right-side left-side))) (length common-set))) ; d) Write the main function, COMPARE, that returns the number of ; features describing two objects, with a -VS- between them, and ; reports the number of features they have in common. COMPARE ; should return a list of form (n COMMON FEATURES). (defun compare (list) (let ((n (count-common list))) (list n 'common 'features))) ; e) Try the expression ; (compare '(small red metal cube -vs- red plastic small cube)) ; You should get (3 common features) as the result. ; ; This is tested in ut-compare. ;===================================================================== ; Unit test (defun ut-right-side () (let* ((sentence '(large red shiny cube -vs- small shiny red four-sided pyramid)) (right-side (right-side sentence)) (expected '(small shiny red four-sided pyramid))) (equal right-side expected))) (defun ut-left-side () (let* ((sentence '(large red shiny cube -vs- small shiny red four-sided pyramid)) (left-side (left-side sentence)) (expected '(cube shiny red large))) (equal left-side expected))) (defun ut-count-common () (let* ((sentence '(large red shiny cube -vs- small shiny red four-sided pyramid)) (counted (count-common sentence)) (expected-count 2)) (equal counted expected-count))) (defun ut-compare () (let* ((sentence '(small red metal cube -vs- red plastic small cube)) (actual (compare sentence)) (expected '(3 common features))) (equal actual expected))) (defun ut-ex-06-26-all () (and (ut-right-side) (ut-left-side) (ut-count-common) (ut-ompare)))
3,268
Common Lisp
.lisp
83
36.807229
70
0.690852
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
2439fe5e7eb19bf6f0853736c066ef007d042319f48b0c6f9c009bfede6c1d97
17,107
[ -1 ]
17,108
ex-06-31.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-31.lisp
; file: ex-06-31.lisp ; author: Jon David ; date: Sunday, July 27, 2014 ; description: ; exercise 6.31 - Write the function WHO-WROTE that takes the name of ; a book as input and returns the book's author. (load "ex-06-30.lisp") (defun who-wrote (book-name) (cadr (assoc book-name BOOKS)))
301
Common Lisp
.lisp
9
31.666667
71
0.714286
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
17df229eb382815b971094d48b4504df7861dd5c007629e4d1005c571b67316d
17,108
[ -1 ]
17,109
ex-06-37.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-37.lisp
; file: ex-06-37.lisp ; author: Jon David ; date: Sunday, July 27, 2014 ; description: ; exercise 6.37 - ROTATE-LEFT and ROTATE-RIGHT are functions that ; rotate the elements of a list. (ROTATE-LEFT '(A B C D E)) returns ; (B C D E A), whereas ROTATE-RIGHT returns (E A B C D). Write ; these functions. (defun rotate-right (list) ; hehe, see what I did here? (reverse (rotate-left (reverse list)))) (defun ut-rotate-right () (let* ((sentence '(A B C D E)) (actual (rotate-right sentence)) (expected '(E A B C D))) (equal actual expected))) (defun rotate-left (list) (append (cdr list) (list (car list)))) (defun ut-rotate-left () (let* ((sentence '(A B C D E)) (actual (rotate-left sentence)) (expected '(B C D E A))) (equal actual expected)))
785
Common Lisp
.lisp
23
31.695652
69
0.664011
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
24a52ed1e4aea52d9766aa875d17f23a880f47864ff740d676cd85b14a83c89a
17,109
[ -1 ]
17,110
ex-06-08.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-08.lisp
; file: ex-06-08.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.8 - Write a function MY-BUTLAST that returns a list ; with the last element removed. (MY-BUTLAST '(ROSES ARE RED)) ; should return the list (ROSES ARE). (MY-BUTLAST '(G A G A)) ; should return (G A G). (defun my-butlast (list) (reverse (cdr (reverse list)))) ;===================================================================== ; Unit Tests (defun ut-my-butlast () (let ((quote1 '(ROSES ARE RED)) (quote2 '(G A G A))) (and (equal '(ROSES ARE) (my-butlast quote1)) (equal '(G A G) (my-butlast quote2)))))
630
Common Lisp
.lisp
17
35.235294
70
0.586885
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
e2b037dd1780100bdd7fd366481654f8603658fc1878ced977914eb6224219ed
17,110
[ -1 ]
17,111
ex-06-41-keyboard-exercise.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-41-keyboard-exercise.lisp
; file: ex-06-41.lisp ; author: Jon David ; date: Sunday, July 27, 2014 ; description: ; exercise 6.41 - If the table of rooms is already stored on the ; computer for you, load the file containing it. If not, you will ; have to type the table in as it appears in Figure 6-3 (pg. 188). (setf ROOMS '((living-room (north front-stairs) (south dining-room) (east kitchen)) (kitchen (south pantry) (west living-room)) (pantry (north kitchen) (west dining-room)) (dining-room (north living-room) (west downstairs-bedroom) (east pantry)) (downstairs-bedroom (east dining-room) (north back-stairs)) (back-stairs (south downstairs-bedroom) (north library)) (library (south back-stairs) (east upstairs-bedroom)) (upstairs-bedroom (west library) (south front-stairs)) (front-stairs (north upstairs-bedroom) (south living-room)))) ; a) Write a function CHOICES that takes the name of a room as input ; and returns the table of permissible directions Robbie may take ; from that room. For example, (CHOICES 'PANTRY) should return the ; list ((NORTH KITCHEN) (WEST DINING-ROOM)). Test your function ; to make sure it returns the correct result. (defun choices (current-room) (cdr (assoc current-room ROOMS))) (defun ut-choices () (let* ((current-room 'pantry) (actual (choices current-room)) (expected '((north kitchen) (west dining-room)))) (equal actual expected))) ; b) Write a function LOOK that takes two inputs, a direction and a ; room, and tells where Robbie would end up if he moved in that ; direction from that room. For example, (LOOK 'NORTH 'PANTRY) ; should return KITCHEN. (LOOK 'WEST 'PANTRY) should return NIL. ; Hint: The CHOICES function will be a useful building block. (defun look (direction current-room) (cadr (assoc direction (choices current-room)))) (defun ut-look () (let* ((direction 'north) (current-room 'pantry) (actual (look direction current-room)) (expected 'kitchen)) (equal actual expected))) ; c) We will use the global variable LOC to hold Robbie's location. ; Type in an expression to set his pantry to be the pantry. The ; following function should be use whenever you want to change ; his location. (defun set-robbie-location (room) "Moves Robbie to room by setting the variable LOC." (setf LOC room)) ; Sets Robbie's location to PANTRY. (set-robbie-location 'pantry) ; d) Write a function HOW-MANY-CHOICES that tells how many choices ; Robbie has for where to move next. Your function should refer ; to the global variable LOC to find his current location. If ; he is in the pantry, (HOW-MANY-CHOICES) should return 2. (defun how-many-choices () (length (choices LOC))) ; e) Write a predicate UPSTAIRSP that returns T if its input is an ; upstairs location. (The library and the upstairs bedroom are ; the only two locations upstairs.) Write a predicate ONSTAIRSP ; that returns T if its input is either FRONT-STAIRS or ; BACK-STAIRS. (defun upstairsp (room) (or (equal 'library room) (equal 'upstairs-bedroom room))) (defun ut-upstairsp () (and (upstairsp 'library) (upstairsp 'upstairs-bedroom) (not (upstairsp 'front-stairs)) (not (upstairsp 'back-stairs)) (not (upstairsp 'living-room)) (not (upstairsp 'kitchen)) (not (upstairsp 'downstairs-bedroom)) (not (upstairsp 'dining-room)) (not (upstairsp 'pantry)))) (defun onstairsp (room) (or (equal 'front-stairs room) (equal 'back-stairs room))) (defun ut-onstairsp () (and (onstairsp 'front-stairs) (onstairsp 'back-stairs) (not (onstairsp 'upstairs-bedroom)) (not (onstairsp 'library)) (not (onstairsp 'downstairs-bedroom)) (not (onstairsp 'living-room)) (not (onstairsp 'dining-room)) (not (onstairsp 'pantry)) (not (onstairsp 'kitchen)))) ; f) Where's Robbie? Write a function of no inputs called WHERE that ; tells where Robbie is. If he is in the library, (WHERE) should ; say (ROBBIE IS UPSTAIRS IN THE LIBRARY). If he is in the kitchen, ; it should say (ROBBIE IS DOWNSTAIRS IN THE KITCHEN). If he is ; on t he front stairs, it should say (ROBBIE IS ON THE ; FRONT-STAIRS). (defun where () (let ((upstairs-text '(Robbie is upstairs in the)) (downstairs-text '(Robbie is downstairs in the)) (onstairs-text '(Robbie is on the)) (room (list LOC))) (cond ((upstairsp LOC) (append upstairs-text room)) ((onstairsp LOC) (append onstairs-text room)) (t (append downstairs-text room))))) ; g) Write a function MOVE that takes one input, a direction, and ; moves Robbie in that direction. MOVE should make use of the ; LOOK function you wrote previously, and should call ; SET-ROBBIE-LOCATION to move him. If Robbie can't move in the ; specified direction an appropriate message should be returned. ; For example, something like (OUCH! ROBBIE HIT A WALL). (MOVE ; 'NORTH) should change Robbie's location and return (ROBBIE IS ; DOWNSTAIRS IN THE KITCHEN). (defun move (direction) (let ((next-room (look direction LOC))) (if next-room (block true-condition (set-robbie-location next-room) (where)) '(ouch! robbie hit a wall))))
5,441
Common Lisp
.lisp
136
35.786765
70
0.685806
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
da7ced869e7f4a82a90cb14a52bea2486f85c2443aa774f2d17407310e1aad33
17,111
[ -1 ]
17,112
ex-06-21.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-21.lisp
; file: ex-06-21.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.21 - If set x is a subset of set y, then subtracting ; y from x should leave the empty set. Write MY-SUBSETP, a version ; of the SUBSETP predicate that returns T if its first input is a ; subset of its second input. ; ; if x is a subset of y, then x-y=nil. ; if x-y!=nil, then x is not a subset of y. (defun my-subsetp (x y) (null (set-difference x y))) ;===================================================================== ; Unit tests
556
Common Lisp
.lisp
15
35.666667
70
0.607076
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
7173b6f790139fd0b9cad66a547f5d83dca0af69814f35d99d1e36e307dce19c
17,112
[ -1 ]
17,113
ex-06-34.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-34.lisp
; file: ex-06-34.lisp ; author: Jon David ; date: Sunday, July 27, 2014 ; description: ; exercise 6.34 - Here is a table of states and some of their ; cities stored in the global variable ATLAS: ; ; (setf ATLAS '((pennsylvania pittsburgh) ; (new-jersey newark) ; (pennsylvania johnstown) ; (ohio columbus) ; (new-jersey princeton) ; (new-jersey trenton))) ; ; Suppose we wanted to find all the cities a given state contains. ; ASSOC returns only the first entry with a matching key, not ; all such entries, so for this table ASSOC cannot solve our ; problem. Redesign the table so that ASSOC can be used successfully. (setf ATLAS '((pennsylvania pittsburgh johnstown) (new-jersey newark princeton trenton) (ohio columbus))) ; example usage: ; (cdr (assoc 'new-jersey ATLAS)) -> (NEWARK PRINCETON TRENTON)
914
Common Lisp
.lisp
23
37.913043
70
0.670429
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
afa257c43201ec09a0e0af492cfc299f761c13e30f1072fb00935fdb2b71a6c9
17,113
[ -1 ]
17,114
ex-06-15.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-15.lisp
; file: ex-06-15.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.15 - We can use MEMBER to write a predicate that ; returns a true value if a sentence contains the word "the". ; ; (defun contains-the-p (sentence) ; (member 'the sentence)) ; ; Suppose we instead want a predicate CONTAINS-ARTICLE-P that ; returns a true value if a sentence contains any article, such as ; "the", "a", or "an". ; Write a version of this predictae using INTERSECTION. (defun contains-article-p-a (sentence) (intersection '(a an the) sentence)) ; Write another version using MEMBER and OR. (defun contains-article-p-b (sentence) (or (member 'a sentence) (member 'an sentence) (member 'the sentence))) ; Q: Could you solve this problem with AND instead of OR? ; A: No, because in that case it is non-nil only when ; the sentence as all three predicates. ;===================================================================== ; Unit tests (defun ut-contains-article-p-a () ; Unit test for ut-contains-article-p-a. Returns NIL when function ; incorrectly detects or fails to detect an article in a sentence. (and (contains-article-p-a '(The cat in the hat)) (contains-article-p-a '(A table for two)) (contains-article-p-a '(An essay is due)) (null (contains-article-p-a '(No articles here))))) (defun ut-contains-article-p-b () ; Unit test for ut-contains-article-p-b. Returns NIL when function ; incorrectly detects or fails to detect an article in a sentence. (and (contains-article-p-b '(The cat in the hat)) (contains-article-p-b '(A table for two)) (contains-article-p-b '(An essay is due)) (null (contains-article-p-b '(No articles here)))))
1,753
Common Lisp
.lisp
40
41.025
70
0.676833
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
60ccdd8f78ce3b0b8f0466900ea89ba1c8342023c1f28e9c2311e2cc08897d52
17,114
[ -1 ]
17,115
ex-06-01.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-01.lisp
; file: ex-06-01.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.1 - Why is (NTH 4 '(A B C)) equal to NIL? ; ; solution: ; (NTH 4 '(A B C)) is NIL because NTH4 requires four CDRs ; on a 3-element list.
245
Common Lisp
.lisp
9
26.222222
59
0.65678
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
e10daf24acab8f753f9d14d7f56e61af148e810b6147cfa63c02ee0af53b8e3e
17,115
[ -1 ]
17,116
ex-06-12.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-12.lisp
; file: ex-06-12.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.12 - Does MEMBER have to copy its input to produce its ; results? Explain your reasoning. ; ; answer: ; No because it only has to return NIL or the cons cell of the first ; instance.
293
Common Lisp
.lisp
10
28.3
70
0.724382
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
324b0c53d01ba3cd1965cf1f79f47e2e83854a74d3abc74ea2e50646850ef582
17,116
[ -1 ]
17,117
ex-06-06.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-06.lisp
; file: ex-06-06.lisp ; author: Jon David ; date: Saturday, July 26, 2014 ; description: ; exercise 6.6. - Use the LAST function to write a function called ; LAST_ELEMENT that returns the last element of a list instead of ; the last cons cell. Write another version of LAST-ELEMENT using ; REVERSE instead of last. Write another version using NTH and ; LENGTH. (defun last-element-a (list) (car (last list))) (defun last-element-b (list) (car (reverse list))) (defun last-element-c (list) (let ((n (length list))) (nth (- n 1) list))) ;===================================================================== ; Unit tests ; (defun ut-last-element-a () (let ((quote '(roses are red))) (equal 'red (last-element-a quote)))) (defun ut-last-element-b () (let ((quote '(roses are red))) (equal 'red (last-element-b quote)))) (defun ut-last-element-c () (let ((quote '(roses are red))) (equal 'red (last-element-c quote))))
961
Common Lisp
.lisp
28
32.035714
70
0.62635
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
775a3d6306a843e25e02f1378ac69f00cc865cc1f18527911937f0a2af2421d3
17,117
[ -1 ]
17,118
ex-06-36.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-36.lisp
; file: ex-06-36.lisp ; author: Jon David ; date: Sunday, July 27, 2014 ; description: ; exercise 6.36 - Write a function to swap the first and last ; elements of any list. (SWAP-FIRST-LAST '(YOU CANT BUY LOVE)) ; should return (LOVE CANT BUY YOU). (defun swap-first-last (list) (let* ((but-last (reverse (cdr (reverse list)))) (middle (cdr but-last)) (first (list (car list))) (last (last list))) (append last middle first))) (defun ut-swap-first-last () (let* ((sentence '(you cant buy love)) (actual (swap-first-last sentence)) (expected '(love cant buy you))) (equal actual expected)))
625
Common Lisp
.lisp
18
32.111111
64
0.676667
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
a3595f0277b21f250761a3dd5d8e03b907b5f969404c3455c8ffd95f0176d36b
17,118
[ -1 ]
17,119
ex-06-35-keyboard-exercise.lisp
joncdavid_touretzky-lisp-exercises/chp6-list-data-structures/ex-06-35-keyboard-exercise.lisp
; file: ex-06-35.lisp ; author: Jon David ; date: Sunday, July 27, 2014 ; description: ; exercise 6.35 - In this problem we will simulate the behavior of ; a very simple-minded creature, Nerdus Americanis (also known as ; Computerus Hackerus). This create hash only five states: ; Sleeping ; Eating ; Waiting-for-a-computer ; Programming ; Debugging ; Its behavior is cyclic: After it sleeps it always eats; after it ; eats it always waits for a computer, and so on, until after ; debugging it goes back to sleep for a while. ; ; a) What type of data structure would be useful for representing the ; connection between a state and its successor? Write such a ; data structure for the five-state cycle given above, and store it ; in a global variable called NERD-STATES. (setf NERD-STATES '((sleeping eating) (eating waiting-for-a-computer) (waiting-for-a-computer programming) (programming debugging) (debugging sleeping))) ; b) Write a function NERDUS that takes the name of a state as input ; and uses the data structure you designed to determine the next ; state the creature will be in. (NERDUS 'SLEEPING) should return ; EATING, for example. (NERDUS 'DEBUGGING) should return SLEEPING. (defun nerdus (state) (cadr (assoc state NERD-STATES))) ; c) Q: What is the result of (NERDUS 'PLAYING-GUITAR)? ; A: NIL, because PLAYING-GUITAR is not a state in NERD-STATES. ; d) When Nerdus Americanis ingests too many stimulants, it stops ; sleeping. After finishing debugging, it immediately goes to ; state Eating. Write a function SLEEPLESS-NERD that works just ; like NERDUS except it never sleeps. Your function should ; refer to the global variable NERD-STATES, as NERDUS does. (defun sleepless-nerd (state) (if (equal 'debugging state) (nerdus (nerdus state)) (nerdus state))) ; e) Exposing Nerdus Americanis to extreme amounts of chemical ; stimulants produces pathological behavior. Instead of an ; orderly advance to its next state, the creature advances two ; states. For example, it goes from Eating directly to ; Programming, and from there to Sleeping. Write a function ; NERD-ON-CAFFEINE that exhibits this unusual pathology. Your ; function should use the same table as NERDUS. (defun nerd-on-caffeine (state) (nerdus (nerdus state)))
2,381
Common Lisp
.lisp
51
44.666667
70
0.740086
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
698bdc63175f96a318e9e79041706d7ebf753ca1cf9161ed0cbcc44d23b891bb
17,119
[ -1 ]
17,120
ex-04-20.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-20.lisp
; file: ex-04-20.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.20 - Write a version of the COMPARE function using IF ; instead of COND. Also write a version using AND and OR. ; ; Note: ; COMPARE function returns NUMBERS-ARE-THE-SAME, FIRST-IS-BIGGER, ; and FIRST-IS-SMALLER. (defun compare-if (x y) (if (> x y) 'FIRST-IS-BIGGER (if (< x y) 'FIRST-IS-SMALLER 'NUMBERS-ARE-THE-SAME))) (defun compare-and-or (x y) (or (and (equal x y) 'NUMBERS-ARE-THE-SAME) (and (> x y) 'FIRST-IS-BIGGER) 'FIRST-IS-SMALLER))
586
Common Lisp
.lisp
18
29.944444
68
0.666667
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
3b583b701cdcbf6ef55de0940dac960501f6b3c55e4d811273d671f04a2a5d38
17,120
[ -1 ]
17,121
ex-04-17.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-17.lisp
; file: ex-04-17.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.17 - Write a predicate that returns T if the first ; input is either BOY or GIRL and the second is CHILD, ; or if the first input is either MAN or WOMAN and the second ; input is ADULT. (defun gender-age-p (gender age) (not (null (or (equal '(boy child) (list gender age)) (equal '(girl child) (list gender age)) (equal '(man adult) (list gender age)) (equal '(woman adult) (list gender age))))))
522
Common Lisp
.lisp
13
38
65
0.691089
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
eca61f86ef46137273bfa460f130bfcf786f53cb4593fed861d5384f8325cc97
17,121
[ -1 ]
17,122
ex-04-03.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-03.lisp
; file: ex-04-03.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.3 - Recall the primitive function NOT: It returns NIL ; for a true input and T for a false one. Suppose Lisp didn't have ; a NOT primitive. Show how to write NOT using just IF and ; constants (no other functions). Call your function MY-NOT. (defun my-not (x) (if x nil t))
389
Common Lisp
.lisp
10
37.6
69
0.71164
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
1077612d73ee8c48157bd921bbbae2cdcac04ef8cc9ab7bb978f1b2dedbde9c1
17,122
[ -1 ]
17,123
ex-04-16.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-16.lisp
; file: ex-04-16.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.16 - Write a function that: ; squares a number if it is odd and positive, ; doubles if it is odd and negative, ; and otherwise divides the number by 2. (defun ex-4-16 (x) (cond ((and (oddp x) (> x 0)) (* x x)) ((and (oddp x) (< x 0)) (* x 2)) (t (/ x 2))))
378
Common Lisp
.lisp
12
29.583333
47
0.623955
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
93a511596325de359bb832dcbb0bb8d40f91292964666b3d628418ad94ad6e5d
17,123
[ -1 ]
17,124
ex-04-14.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-14.lisp
; file: ex-04-14.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.14 - What results do the following expressions ; produce? Read the evaluation rules for AND and OR carefully ; before answering. ; ; Evaluation rule for AND: return nil if any clause is nil, ; otherwise return the value of the last clause. ; ; Evaluation rule for OR: return the value of the first non-nil ; value. otherwise, return nil if no non-nil alues are found. ; ; solution: ; (no coding for this question). ; ; (and 'fee 'fie 'foe) => foe ; (or 'fee 'fie 'foe) => fee ; (or nil 'foe nil) => foe ; (and 'fee 'fie nil) => nil ; (and (equal 'abc 'abc) 'yes) => yes ; (or (equal 'abc 'abc) 'yes) => t
725
Common Lisp
.lisp
23
30.391304
65
0.680973
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
073e47c0a7b808b07dd64a1d0aaf610762fd2813a5b2da70f6e432f43a9dcb9a
17,124
[ -1 ]
17,125
ex-04-01.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-01.lisp
; file: ex-04-01.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.1 - Write a function MAKE-EVEN that makes an odd number ; even by adding one to it. If the input to MAKE-EVEN is already ; even, it should be returned unchanged. (defun make-even (n) (if (oddp n) (+ n 1) n))
320
Common Lisp
.lisp
9
34.222222
70
0.696774
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
22339a1e089cbe80a93a77dc543544a8a544e0fe5ee58524a925e0389455d0ae
17,125
[ -1 ]
17,126
ex-04-35.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-35.lisp
; file: ex-04-35.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.35 - Write down the DeMorgan equations for the ; three-input versions of AND and OR. ; ; Review (page 134): ; (and x y) = (not (or (not x) (not y))) ; "If x and y are true, then neither is x false nor is y false." ; ; (or x y) = (not (and (not x) (not y))) ; "If either x or y is true, then x and y can't both be false" ; ; Solution: ; This is a conceptual exercise. But the tests are provided ; below. ; ; If x, y, and z are true, then neither is x false, nor is y false, ; nor is z false. ; (and x y z) = (not (or (not x) (not y) (not z))) ; ; If either x, y, or z is true, then x, y, and z can't all be false. ; (or x y z) = (not (and (not x) (not y) (not z))) (defun not-nullp (x) (not (null x))) (defun demorgan-and-test (x y z) ;Tests if demorgan's expressions is equivalent to logical and. ;Should always return T if the two expressions are equivalent. (equal (not-nullp (and x y z)) (not-nullp (not (or (not x) (not y) (not z)))))) (defun demorgan-or-test (x y z) ;Tests if demorgan's expressions is equivalent to logical or. ;Should always return T if the two expressions are equivalent. (equal (not-nullp (or x y z)) (not-nullp (not (and (not x) (not y) (not z))))))
1,319
Common Lisp
.lisp
36
35.277778
70
0.650273
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
cb38951b952fd2580d6633fe4e4cecb4b152ac195bc8e18583917f1da82efbf5
17,126
[ -1 ]
17,127
ex-04-29.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-29.lisp
; file: ex-04-29.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.29 - Write versions of LOGICAL-AND using IF and COND ; instead of AND. (defun logical-and-if (x y) (if x (if y t))) (defun logical-and-cond (x y) (cond (x (cond (y t)))))
284
Common Lisp
.lisp
10
26.7
67
0.667897
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
d9f0123902f99684f636642b155fa05493fc214ce367ad3679ac066371d79a5f
17,127
[ -1 ]
17,128
ex-04-04.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-04.lisp
; file: ex-04-04.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.4 - Write a function ORDERED that takes two numbers ; as input and makes a list of them in ascending order. ; (ORDERED 3 4) should return the list (3 4). (ORDERED 4 3) should ; also return (3 4), in other words, the first and second inputs ; should appear in reverse order when the first is greater than ; the second. (defun ordered (x y) (if (> x y) (list y x) (list x y)))
494
Common Lisp
.lisp
12
39.833333
69
0.697917
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
21c6c6e365cda0fa5f810eb823ed50c7fcf25f25e9b978e44249b4bf02827a0f
17,128
[ -1 ]
17,129
ex-04-05.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-05.lisp
; file: ex-04-05.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.5 - For each of the following calls to COMPARE, ; write "1", "2", or "3" to indicate which clause of the ; COND will have a predicate that evaluates to true. ; ; solution ; -------------------------+------------- ; ___ (compare 9 1) + first-is-bigger ; ___ (compare (+ 2 2) 5) + first-is-smaller ; ___ (compare 6 (* 2 3)) + numbers are the same ; ; COND and IF are similar functions. COND may appear more ; versatiles since it accepts any number of clauses, but there is ; a way to do the same thing with nested IFs. This is explained ; later in this chapter. ; This function is given: (defun compare (x y) (cond ((equal x y) 'numbers-are-the-same) ((< x y) 'first-is-smaller) ((> x y) 'first-is-bigger)))
872
Common Lisp
.lisp
23
36.652174
66
0.612751
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
aa92823f3f2b3cca7ba8071a00be10a0da8c45fb67db6697099601fab9921816
17,129
[ -1 ]
17,130
ex-04-12.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-12.lisp
; file: ex-04-12.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.12 - Write a function CYCLE that cyclically counts from ; 1 to 99. CYCLE called with an input of 1 should return 2, with ; and input of 2 should return 3, etc. With an input of 99, CYCLE ; should return 1. That's the cyclical part. Do not try to solve ; this with 99 COND clauses! (defun cycle (n) (if (equal n 99) 1 (+ n 1)))
448
Common Lisp
.lisp
11
39.272727
70
0.695853
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
3504a901f155eb5d3eb761cf823d781fa9248d454122c530df3b912a38663ee9
17,130
[ -1 ]
17,131
ex-04-10.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-10.lisp
; file: ex-04-10.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.10 - Write a function CONSTRAIN that takes three ; inputs called X, MAX, and MIN. If X is less than MIN, then ; it should return MIN; if X is greater than MAX, it should ; return MAX. Otherwise, since X is between MIN and MAX, it ; should return X. (CONSTRAIN 3 -50, 50) should return 3. ; (CONSTRAIN 92 -50 50) should return 50. Write one version ; using COND and another using nested IFs. (defun constrain-cond (x min max) (cond ((> x max) max) ((< x min) min) (t x))) (defun constrain-if (x min max) (if (> x max) max (if (< x min) min x)))
692
Common Lisp
.lisp
21
30.619048
63
0.664168
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
887315e3195e451f4ea057f6581186b9d94954961a8a45b4b52d616f57170b53
17,131
[ -1 ]
17,132
ex-04-37.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-37.lisp
; file: ex-04-37.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.37 - NAND is called a logically complete function ; because we can construct all other boolean functions from ; various combinations of NAND. For example, here is a version ; of NOT called NOT2 constructed from NAND: ; ; SOLUTION: ; NAND truth table: ; | x | y | (and x y) | (nand x y) | ; |-----+-----+-----------+------------| ; | nil | nil | nil | t | ; | nil | | nil | t | ; | t | nil | nil | t | ; | t | t | t | nil | ; ; ; AND truth table ; | x | y | (and x y) || (nand x y) | (nand (nand x y) (nand x y)) | ; |-----+-----+-----------||------------|------------------------------| ; | nil | nil | nil || t | nil | ; | nil | t | nil || t | nil | ; | t | nil | nil || t | nil | ; | t t | t || nil | t | ; ; OR truth table ; | x | y | (or x y) || (nand x t) | (nand y t) | (nand (nand x t) | ; | | | || | | (nand y t))| ; |-----+-----+----------||------------|-------------------------------| ; | nil | nil | nil || t | t | nil | ; | nil | t | t || t | nil | t | ; | t | nil | t || nil | t | t | ; | t t | t || nil | nil | t | ; (defun nand (x y) (not (and x y))) (defun not2 (x) (nand x x)) (defun and2 (x y) (nand (nand x y) (nand x y))) (defun or2 (x y) (nand (nand x t) (nand y t))) ;===================================================================== ; Unit tests ;===================================================================== (defun ut-not2 () ; Unit test for not2. Returns NIL if unit test failed. (and (equal (not t) (not2 t)) (equal (not nil) (not2 nil)))) (defun ut-and2 () ; Unit test for and2. Returns NIL if unit test failed. (and (equal (and nil nil) (and2 nil nil)) (equal (and nil t) (and2 nil t)) (equal (and t nil) (and2 t nil)) (equal (and t t) (and2 t t)))) (defun ut-or2() ; Unit test for or2. Returns NIL if unit test failed. (and (equal (or nil nil) (or2 nil nil)) (equal (or nil t) (or2 nil t)) (equal (or t nil) (or2 t nil)) (equal (or t t) (or2 t t))))
2,536
Common Lisp
.lisp
63
38.047619
72
0.37627
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
8e4769ddb8a01634a1a79b8d2e543533ab890d982dcfc2ec2c38f9b44ef84dc7
17,132
[ -1 ]
17,133
ex-04-23.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-23.lisp
; file: ex-04-23.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.23 - The WHERE-IS function has four COND clauses, so ; WHERE-IS-2 needs three nested IFs. Suppose WHERE-IS had eight ; COND clauses. How many IFs would WHERE-IS-2 need? How many ORs ; would WHERE-IS-3 need? How many ANDs would it need? ; SOLUTION: ; this is a conceptual problem-no programming. ; ; This is the original WHERE-IS function defined on page 126: ; (defun where-is (x) ; (cond ((equal x 'paris) 'france) ; ((equal x 'london) 'england) ; ((equal x 'beijing) 'china) ; (t 'unknown))) ; ; Suppose WHERE-IS had eight COND clauses. ; Q1: How many IFs would WHERE-IS-2 need? ; A1: WHERE-IS-2 needs seven nested IFs. ; ; Q2: How many ORs would WHERE-IS-3 need? ; A2: One OR. ; ; Q3: How many ANDs would WHERE-IS-3 need? ; A3: Seven ANDs.
907
Common Lisp
.lisp
27
32.481481
68
0.668187
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
c2621de9b8ed11b0bef171d73316efa17a3081c79d8195c0ae03c19a5d59b277
17,133
[ -1 ]
17,134
ex-04-22.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-22.lisp
; file: ex-04-22.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.22 - Use COND to write a predicate BOILINGP that takes ; two inputs, TEMP and SCALE, and returns T if the temperature is ; above the boiling point of water on the specified scale. If the ; scale is FAHRENHEIT, the boiling point is 212 degrees; if CELSIUS, ; the boiling point is 100 degrees. Also write versions using IF ; and AND/OR instead of COND. (defun boilingp-cond (temp scale) (cond ((equal scale 'FAHRENHEIT) (> temp 212)) ((equal scale 'CELSIUS) (> temp 100)) (t '(Err invalid scale value)))) (defun boilingp-if-andor (temp scale) (or (and (equal scale 'FAHRENHEIT) (> temp 212)) (and (equal scale 'CELSIUS) (> temp 100))))
766
Common Lisp
.lisp
17
43.117647
70
0.710067
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
a151a34b1d918a4ec95ad2fdc2417e51e38c9527c16f0c7f9088d65c0b01bae0
17,134
[ -1 ]
17,135
ex-04-09.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-09.lisp
; file: ex-04-09.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.9 - Type in the following suspicious function ; definition: ; ; (defun make-odd (x) ; (cond (t x) ; ((not (oddp x)) (+ x 1)))) ; ; The problem is the first test always evaluates to true. This ; means MAKE-ODD always returns exactly what it was given. ; ; Rewrite it so it works correctly: (defun make-odd (x) (cond ((evenp x) (+ x 1)) (t x)))
477
Common Lisp
.lisp
18
25.166667
64
0.64693
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
d76b76d32eb2dc8312b11c3df360b5af56e6dd3b406e1187f0acc97553d7483e
17,135
[ -1 ]
17,136
ex-04-38.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-38.lisp
; file: ex-04-38.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.38 - Consider the NOR function (short for Not OR). Can ; you write versions of NOT, LOGICAL-AND, NAND, and LOGICAL-OR by ; putting NORs together? ;===================================================================== ; NOR truth table ; | x | y | (or x y) | (not (or x y)) | ; |-----+-----+-----------+----------------| ; | nil | nil | nil | t | ; | nil | t | t | nil | ; | t | nil | t | nil | ; | t | t | t | nil | (defun nor (x y) (not (or x y))) ;===================================================================== ; NOT truth table (represented by NORs) ; | x | (not x) || (nor x x | ; |-----+---------++-------------| ; | nil | t || t | ; | t | nil || nil | (defun not2 (x) (nor x x)) ;===================================================================== ; Deriving LOGICAL-AND from NORs. ; (and x y) = (not (or (not x) (not y))), by DeMorgan's theorem ; = (not (or (nor x x) (nor y y))), by (not z) = (nor z z) ; = (nor (nor x x) (nor y y)), by definition of NOR. ;--------------------------------------------------------------------- ; LOGICAL-AND truth table (represented by NORs); ; | x | y | (and x y) || (nor (nor x x) (nor y y)) ; |-----+-----+-----------++---------------------------| ; | nil | nil | nil || nil | ; | nil | t | nil || nil | ; | t | nil | nil || nil | ; | t | t | t || t | (defun and2 (x y) (nor (nor x x) (nor y y))) ;===================================================================== ; Deriving NAND from NORs. ; (nand x y) = (not (and x y)), by definition of NAND ; = (not (not (or (not x) (not y)))), by DeMorgan's theorem ; = (not (not (or (nor x x) (nor y y)))), by NOT equivalent in NORs ; = (not (nor (nor x x) (nor y y))), by definition of NOR ; = (nor (nor (nor x x) (nor y y)) ; (nor (nor x x) (nor y y))), by NOT equivalent in NORs ;--------------------------------------------------------------------- ; NAND truth table (represented by NORs) ; | x | y | (nand x y) || (nor (nor (nor x x) (nor y y)) | ; | | | || (nor (nor x x) (nor y y))) | ; |-----+-----+------------++---------------------------------| ; | nil | nil | t || t | ; | nil | t | t || t | ; | t | nil | t || t | ; | t | t | nil || nil | (defun nand2 (x y) (nor (nor (nor x x) (nor y y)) (nor (nor x x) (nor y y)))) ;===================================================================== ; Deriving OR from NORs. ; (or x y) = (not (and (not x) (not y))), by DeMorgan's theorem ; = (not (not (or (not (not x)) (not (not y))))), by DeMorgan's ; = (not (not (or x y))), by double negation ; = (not (nor x y)), by definition of NOR ; = (nor (nor x y) (nor x y)), by equivalence of NOT using NORs ;--------------------------------------------------------------------- ; LOGICAL-OR truth table (represented by NORs) ; | x | y | (or x y) || (nor (nor x y) (nor x y)) ; |-----+-----+----------||---------------------------| ; | nil | nil | nil || nil | ; | nil | t | t || t | ; | t | nil | t || t | ; | t | t | t || t | (defun or2 (x y) (nor (nor x y) (nor x y))) ;===================================================================== ; Unit tests ;===================================================================== (defun logical-and (x y) (if x (if y t))) (defun logical-or (x y) (if x t (if y t))) (defun logical-nand (x y) (not (and x y))) (defun ut-not2 () ;Unit test for NOT2. Compares NOT2 against NOT. ;Returns NIL if unit test fails. (and (equal (not nil) (not2 nil)) (equal (not t) (not2 t)))) (defun ut-and2 () ;Unit test for AND2. Compares AND2 against LOGICAL-AND. ;Returns NIL if unit test fails. (and (equal (logical-and nil nil) (and2 nil nil)) (equal (logical-and nil t) (and2 nil t)) (equal (logical-and t nil) (and2 t nil)) (equal (logical-and t t) (and2 t t)))) (defun ut-nand2 () ;Unit test for NAND2. Compares NAND2 against LOGICAL-NAND. ;Returns NIL if unit test fails. (and (equal (logical-nand nil nil) (nand2 nil nil)) (equal (logical-nand nil t) (nand2 nil t)) (equal (logical-nand t nil) (nand2 t nil)) (equal (logical-nand t t) (nand2 t t)))) (defun ut-or2 () ;Unit test for OR2. Compares OR2 against LOGICAL-OR. ;Returns NIL if unit test fails. (and (equal (logical-or nil nil) (or2 nil nil)) (equal (logical-or nil t) (or2 nil t)) (equal (logical-or t nil) (or2 t nil)) (equal (logical-or t t) (or2 t t)))) (defun ut-ex-04-38 () ;Unit test for all functions in ex-04-38. ;Returns NIL if any unit fails. (and (ut-not2) (ut-and2) (ut-nand2) (ut-or2)))
5,351
Common Lisp
.lisp
119
42.714286
70
0.392356
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
fca3e45d6fbda3f8b2a42899bad81724654d6612c16856f1424fe9f851252e49
17,136
[ -1 ]
17,137
ex-04-07.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-07.lisp
; file: ex-04-07.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.7 - For each of the following COND expressions, tell whether ; the parenthesization is correct or incorrect. If incorrect, explain ; where the error lies. ; (cond (symbolp x) 'symbol ; (t 'not-a-symbol)) ; This should be... (defun ex-04-07-a (x) (cond ((symbolp x) 'symbol) (t 'not-a-symbol))) ; (cond ((symbolp x) 'symbol) ; (t 'not-a-symbol)) ; This is correct. ; (cond ((symbolp x) ('symbol)) ; (t 'not-a-symbol)) ; Noticed that 'symbol is not a function... ; (cond ((symbolp x) 'symbol) ; ((t 'not-a-symbol))) ; This version has one too many sets of parentheses on the line #2.
722
Common Lisp
.lisp
22
31.5
75
0.656609
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
49defc07294b7e1e330de616b26506bc6aaa54ec57987f238c7ca1da323291bc
17,137
[ -1 ]
17,138
ex-04-06.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-06.lisp
; file: ex-04-06.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.6 - Write a version of the absolute value function ; MY-ABS using COND instead of IF. (defun my-abs (n) (cond ((< n 0) (- n)) (t n)))
247
Common Lisp
.lisp
9
25.666667
65
0.662393
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
2e6c8104a31413bcc6ffe70c4dfe2bf03592d889e85ec895e82e7b13fe3321fb
17,138
[ -1 ]
17,139
ex-04-13.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-13.lisp
; file: ex-04-13.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.13 - Write a function HOWCOMPUTE that is the inverse ; of the COMPUTE function described previously. HOWCOMPUTE takes ; three numbers as input and figures out what operation would produce ; the third from the first two. ; (HOWCOMPUTE 3 4 7) should return SUM-OF. ; (HOWCOMPUTE 3 4 12) should return PRODUCT-OF. ; HOWCOMPUTE should return the list (BEATS ME) if it can't find a ; relationship between the first two inputs and the third. Suggest ; some ways to extend HOWCOMPUTE. ; ; suggestions: difference, power-of, log-of (defun howcompute (x y z) (cond ((equal (+ x y) z) 'SUM-OF) ((equal (* x y) z) 'PRODUCT-OF) ((equal (- x y) z) 'DIFFERENCE-OF) ((equal (- y x) z) 'DIFFERENCE-OF) ((equal (expt x y) z) 'POWER-OF) ((equal (log x y) z) 'LOGARITHM-OF) (t '(BEATS ME))))
910
Common Lisp
.lisp
23
38
71
0.695357
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
12bb7d6e8347b4befaa51cde0cd371120bb41e106036ea9f0db5aeb1503fcd1f
17,139
[ -1 ]
17,140
ex-04-02.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-02.lisp
; file: ex-04-02.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.2 - Write a function FURTHER that makes a positive ; number larger by adding one to it, and a negative number smaller ; by subtracting one from it. What does your function do if given ; the number 0? (defun further (n) (cond ((> n 0) (+ n 1)) ((< n 0) (- n 1)) (t n)))
386
Common Lisp
.lisp
12
30.666667
68
0.672043
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
3bd613344dd5bf91e3b05c5225e12565e376ab0935498acfd02f763a7d104427
17,140
[ -1 ]
17,141
ex-04-18.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-18.lisp
; file: ex-04-18.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.18 - Write a function to act as referee in the ; ROCK-SCISSORS-PAPER game. (defun rockp (hand) (equal hand 'ROCK)) (defun scissorp (hand) (equal hand 'SCISSOR)) (defun paperp (hand) (equal hand 'PAPER)) (defun play (p1 p2) (cond ((equal p1 p2) 'TIE) ((and (rockp p1) (scissorp p2)) 'FIRST-WINS) ((and (paperp p1) (rockp p2)) 'FIRST-WINS) ((and (scissorp p1) (paperp p2)) 'FIRST-WINS) (t 'SECOND-WINS)))
527
Common Lisp
.lisp
18
27.277778
61
0.676587
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
263e81d25d2376e68c6282557c5fc3f2a1f6fce856b83703699170c8d0f31ca8
17,141
[ -1 ]
17,142
ex-04-36.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-36.lisp
; file: ex-04-36.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.36 - The NAND function (NAND is short for Not AND) is ; very commonly found in computer circuitry. Here is a definition ; of NAND. Write down its truth table. ; ; SOLUTION: ; ; NAND truth table: ; | x | y | (and x y) | (nand x y) | ; |-----+-----+-----------+------------| ; | nil | nil | nil | t | ; | nil | t | nil | t | ; | t | nil | nil | t | ; | t | t | t | nil | ; (defun nand (x y) (not (and x y)))
590
Common Lisp
.lisp
20
28.4
68
0.484211
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
4b1fd7f174688c105eb0127aa41f3e01dd5636e75116fcebd340e890aa5653d1
17,142
[ -1 ]
17,143
ex-04-19.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-19.lisp
; file: ex-04-19.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.19 - Show how to write the expression (AND X Y Z W) ; using COND instead of AND. Then show how to write it using IFs ; instead of AND. (defun and-cond (x y z w) (cond ((null x) nil) ((null y) nil) ((null z) nil) ((null w) nil) (t w))) (defun and-if (x y z w) (if (null x) (if (null y) (if (null z) (if (null w) nil w) z) y) x))
479
Common Lisp
.lisp
21
19.809524
67
0.592105
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
36f65648f955cb5f6fb11ee361114df450839a2b38ee494ec870266ee7dd9d2b
17,143
[ -1 ]
17,144
ex-04-30.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-30.lisp
; file: ex-04-30.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.30 - Write LOGICAL-OR. Make sure it only returns T ; or NIL for its result. (defun logical-or (x y) (or (not (null x)) (not (null y))))
248
Common Lisp
.lisp
8
29.375
66
0.670886
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
784c5b923f60ea04e186dd6776189febcb59a0ce1ce36963416b4c850cd0e6e5
17,144
[ -1 ]
17,145
ex-04-15.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-15.lisp
; file: ex-04-15.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.15 - Write a predicate called GEQ that returns T if ; its first input is greater than or equal to its second input. (defun geq (x y) (or (> x y) (equal x y)))
272
Common Lisp
.lisp
8
32.125
66
0.694981
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
530bda2f36e46cd4591ef32bfabe928f311b6de38c371c661aff1d27d16060c4
17,145
[ -1 ]
17,146
ex-04-08.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-08.lisp
; file: ex-04-08.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.8 - Write EMPHASIZE3, which is like EMPHASIZE2 but adds ; the symbol VERY onto the list if it doesn't know how to emphasize ; it. For example, EMPHASIZE3 of (LONG DAY) shold produce ; (VERY LONG DAY). What does EMPHASIZE3 of (VERY LONG DAY) produce? (defun emphasize3 (x) (cond ((equal (car x) 'very) x) (t (cons 'very x))))
441
Common Lisp
.lisp
11
38.727273
70
0.703963
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
eed9d156715d2bab5f443714100e6436da4104ff5b35148bb044956c1328224d
17,146
[ -1 ]
17,147
ex-04-11.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-11.lisp
; file: ex-04-11.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.11 - Write a function FIRSTZERO that takes a list of ; three numbers as input and returns a word (one of "first", ; "second", "third", or "none") indicating where the first zero ; appears in the list. Example: (FIRSTZERO '(3 0 4)) should return ; SECOND. What happens if you try to call FIRSTZERO with three ; three separate numbers instead of a list of three numbers, ; as in (FIRSTZERO 3 0 4)? (defun firstzero (list) (cond ((zerop (car list)) 'first) ((zerop (cadr list)) 'second) ((zerop (caddr list)) 'third) (t 'none)))
651
Common Lisp
.lisp
16
39.25
69
0.696682
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
93c37509cc29ba2116c37bab9d57ed122174a03b492d35408a49600494880517
17,147
[ -1 ]
17,148
ex-04-21.lisp
joncdavid_touretzky-lisp-exercises/chp4-conditionals/ex-04-21.lisp
; file: ex-04-21.lisp ; author: Jon David ; date: Saturday, July 12, 2014 ; description: ; exercise 4.21 - Write a version of the GTEST function using ; IF and COND. ; ; NOTE: ; gtest returns true if the x > y or if either one is not 0. (defun gtest-cond (x y) (cond ((> x y) t) ((equal x 0) t) ((equal y 0) t) (t nil))) (defun gtest-if (x y) (if (or (equal x 0) (equal y 0)) t (if (> x y) t nil)))
427
Common Lisp
.lisp
18
21.5
63
0.60688
joncdavid/touretzky-lisp-exercises
3
1
0
GPL-3.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
5bb5ad977da5c6ca30e164b56d09f39d9195bf3a838a390df60fc5c4dcc11cbe
17,148
[ -1 ]
17,298
packages.lisp
jnjcc_cl-ml/packages.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; ;;;; Machine Learning in Common Lisp (in-package #:cl-user) (defpackage #:cl-ml (:nicknames #:ml) (:use #:cl #:cl-ml/probs #:cl-ml/linalg #:cl-ml/algo) (:export #:precision-score #:recall-score #:f1-score #:accuracy-score #:confusion-matrix #:roc-curve #:roc-auc-score #:mean-squared-error #:euclidean-distance #:cosine-distance ;;; common APIs of Estimators and Transformers #:fit #:transform #:fit-transform #:inv-transform #:predict #:predict-proba ;;; preprocessing #:standard-scaler #:poly-transformer #:label-encoder ;;; Linear Estimators #:linear-regressor #:ridge-regressor #:sgd-regressor #:logistic-regression #:linear-svc #:kernel-svc ;;; Tree Estimators #:dtree-classifier #:dtree-regressor #:rforest-classifier #:rforest-regressor #:gboost-classifier #:gboost-regressor #:xgboost-classifier #:xgboost-regressor #:feature-importance ;;; Neural Network #:mlp-classifier #:mlp-regressor ;;; Unsupervised > Clustering #:kmeans-cluster ;;; Unsupervised > Mixture Model #:gaussian-mixture ))
1,360
Common Lisp
.lisp
33
31.454545
71
0.595906
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
727b981e30112232e9b2478ca1f683fb0bc737855578fd793cd74dab39c5052a
17,298
[ -1 ]
17,299
kmeans-test.lisp
jnjcc_cl-ml/test/kmeans-test.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; ;;;; Unsupervised Learning > clustering > k-Means clustering (in-package #:cl-ml/test) (define-test unsupervised-kmeans-test (let ((X (copy-matrix *iris-train*)) (kmeans (make-instance 'kmeans-cluster :epochs 300 :ninit 1 :init :kmeans++ :nclusters 3)) (pred nil)) (with-random-seed (5) (fit kmeans X) (setf pred (predict kmeans X)) ;; [0, 50): Iris-setosa; [50, 100): Iris-versicolor; [100, 150): Iris-virginica (let ((bins1 (mbincount (mhead pred 50))) (bins2 (mbincount (make-matrix-view pred :row-view `(50 . 100)))) (bins3 (mbincount (mhead pred -50)))) (assert-true 1 (length bins1)) ;; all examples predicted to the same class (setf bins2 (sort (cl-ml/probs::probability bins2) #'>)) (assert-true (equal '(48/50 2/50) bins2)) (setf bins3 (sort (cl-ml/probs::probability bins3) #'>)) (assert-true (equal '(36/50 14/50) bins3)) ))))
1,061
Common Lisp
.lisp
22
40.363636
85
0.597878
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
8fe85234f3f69dac0a22e881337a84355ec45671af92cd954eb06d0a032a2956
17,299
[ -1 ]
17,300
linear-test.lisp
jnjcc_cl-ml/test/linear-test.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; (in-package #:cl-ml/test) (defvar *train* (make-square-matrix 3 :initial-contents '((2 3 1) (0 1 3) (1 2 5)))) (defvar *weight* (make-vector 3 :initial-contents '(1 1 1))) (defvar *label* (m* *train* *weight*)) ;; Linear Regression using Normal Equation (define-test linear-regression-normal (let ((lreg (make-instance 'linear-regressor :expand-bias nil))) (fit lreg *train* *label*) (assert-true (matrix-eq *label* (predict lreg *train*))))) ;; Ridge Regression using Normal Equation (define-test ridge-regression-normal (let ((ridge (make-instance 'ridge-regressor :alpha 1.0 :solver :normal)) (float-eq (make-float-eq 0.6))) (fit ridge *train* *label*) (format *test-stream* "Ridge regression: ~A vs. ~A~%" (mt (predict ridge *train*))(mt *label*)) (assert-true (matrix-eq *label* (predict ridge *train*) float-eq)))) ;; Batch gradient descent, with L2 regularizer (define-test batch-gradient-descent (let ((sgd (make-instance 'sgd-regressor :epochs 50 :eta0 0.1 :expand-bias nil)) (sgd-l2 (make-instance 'sgd-regressor :epochs 50 :eta0 0.1 :regularizer :l2)) (float-eq (make-float-eq 0.5))) (fit sgd *train* *label*) (format *test-stream* "Batch gd: ~A vs. ~A~%" (mt (predict sgd *train*)) (mt *label*)) (assert-true (matrix-eq *label* (predict sgd *train*) float-eq)) (fit sgd-l2 *train* *label*) (format *test-stream* "Batch gd L2: ~A vs. ~A~%" (mt (predict sgd-l2 *train*)) (mt *label*)) (assert-true (matrix-eq *label* (predict sgd-l2 *train*) float-eq)))) ;; Linear SVC (define-test linear-kernel-svc (let ((y (load-iris-label-binary)) ;; petal length, petal width (X (copy-matrix (make-matrix-view *iris-train* :col-view '(2 3)))) (stand (make-instance 'standard-scaler)) (linsvc (make-instance 'linear-svc)) (rbfsvc (make-instance 'kernel-svc :kernel :rbf :epochs 100)) (sigsvc (make-instance 'kernel-svc :kernel :sigmoid :coef0 1)) (test (make-matrix 1 2 :initial-contents '((5.5 1.7))))) (fit-transform stand X) (transform stand test) (fit linsvc X y) (fit rbfsvc X y) (fit sigsvc X y) (let ((ylin (predict linsvc X)) (yrbf (predict rbfsvc X)) (ysig (predict sigsvc X))) (format *test-stream* "accuracy: ~A; ~A; ~A~%" (accuracy-score y ylin) (accuracy-score y yrbf) (accuracy-score y ysig)) (assert-true (> (accuracy-score y ylin) 0.95)) (assert-true (> (accuracy-score y yrbf) 0.95))) (let ((lpred (predict linsvc test)) (rpred (predict rbfsvc test)) (spred (predict sigsvc test))) (assert-eq +1 (vref lpred 0)) (assert-eq +1 (vref rpred 0)) (assert-eq +1 (vref spred 0)))))
2,931
Common Lisp
.lisp
60
41.683333
100
0.60985
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
57b63b4618ac0a16c2165cb3f87f85607e0378eaf692cf67cc86b69ec2bebf5a
17,300
[ -1 ]
17,301
linalg-test.lisp
jnjcc_cl-ml/test/linalg-test.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; (in-package #:cl-ml/test) (defvar *matrix* (make-matrix 3 5 :initial-contents '((1 2 3 1 5) (6 0 1 3 0) (7 1 2 5 1)))) (defvar *square* (make-square-matrix 3 :initial-contents '((2 3 1) (0 1 3) (1 2 5)))) ;; *SQUARE* + I (defvar *square+1* (make-square-matrix 3 :initial-contents '((3 3 1) (0 2 3) (1 2 6)))) (defvar *identity* (make-identity-matrix 3)) ;; inverse of *SQUARE* (defvar *square-inv* (make-square-matrix 3 :initial-contents '((-1/6 -13/6 4/3) (1/2 3/2 -1) (-1/6 -1/6 1/3)))) ;; vector of *SMATRIX* first column (defvar *vector* (make-vector 3 :initial-contents '(1 6 7))) ;; vector of one's (defvar *vector-one* (make-vector 3 :initial-element 1)) (defvar *matrix-transp* (mt *matrix*)) ;; transpose of transpose of *MATRIX*, which will be *MATRIX* itself (defvar *matrix-self* (mt *matrix-transp*)) (defvar *matrix-view0* (make-matrix-view *matrix*)) ;; element of *MATRIX-VIEW1* is same as *SQUARE* (defvar *matrix-view1* (make-matrix-view *matrix* :col-view '(1 2 3))) ;; same as *MATRIX-VIEW1* (defvar *matrix-view2* (make-matrix-view *matrix* :col-view '(1 . 4))) (define-test smatrix-creation (assert-eq 3 (nrow *matrix*)) (assert-eq 5 (ncol *matrix*)) ;; square matrix (assert-eq 3 (nrow *square*)) (assert-eq 3 (ncol *square*)) ;; column vector (assert-eq 1 (ncol *vector*)) ;; transpose matrix (assert-eq 5 (nrow *matrix-transp*)) (assert-eq 3 (ncol *matrix-transp*)) ;; tranpose of transposed matrix is the original matrix (assert-eq *matrix* *matrix-self*) ;; matrix view (assert-eq (nrow *matrix*) (nrow *matrix-view0*)) (assert-eq 3 (nrow *matrix-view1*)) (assert-eq 3 (ncol *matrix-view1*))) (define-test smatrix-mref (assert-eq 6 (mref *matrix* 1 0)) (assert-eq 1 (mref *identity* 1 1)) (assert-eq 7 (vref *vector* 2)) (assert-eq 6 (mref *matrix-transp* 0 1)) (assert-eq 1 (mref *matrix-view0* 0 0)) (assert-eq 2 (mref *matrix-view1* 0 0)) (assert-true (matrix-eq *matrix-view1* *matrix-view2*))) (define-test smatrix-setf (let ((orig (mref *matrix* 0 1)) (tmp 100)) (setf (mref *matrix* 0 1) tmp) (assert-eq tmp (mref *matrix* 0 1)) ;; modify back through view (setf (mref *matrix-view1* 0 0) orig) (assert-eq orig (mref *matrix* 0 1)) (setf orig (vref *vector* 2)) (setf (vref *vector* 2) tmp) (assert-eq tmp (vref *vector* 2)) (setf (vref *vector* 2) orig)) ;; vector access from matrix (setf (mcv *matrix* 0) 1) (assert-true (matrix-eq *vector-one* (mcv *matrix* 0))) (setf (mcv *matrix* 0) *vector*)) (define-test smatrix-loop-return (let ((return-success nil) (return-counter 0) (go-success t)) (do-matrix (i j *matrix*) (incf return-counter) (when (= i j 0) (setf return-success t) (return)) (setf return-success nil)) (assert-true return-success) (assert-eq 1 return-counter) (do-matrix (i j *matrix*) (when (>= (+ i j) 0) (go end-of-loop)) (setf go-success nil) end-of-loop) (assert-true go-success))) (define-test smatrix-properties (let ((square (make-square-matrix 3 :initial-contents '((1 2 3) (4 5 6) (7 8 0))))) (assert-eq 27 (mdet square)))) (define-test smatrix-operation (assert-true (matrix-eq *square+1* (m+ *square* *identity*))) (assert-true (matrix-eq *identity* (m* *square* *square-inv*))) (assert-true (matrix-eq *square-inv* (minv *square*))) ;; operation through view (assert-true (matrix-eq *square-inv* (minv *matrix-view1*))) (let ((A (make-matrix 3 3 :initial-contents '((1 -1 -1) (2 -1 -3) (-3 4 4)))) (B (make-matrix 3 3 :initial-contents '((1 2 3) (2 2 1) (3 4 3)))) (A+2B+3 (make-matrix 3 3 :initial-contents '((6 6 8) (9 6 2) (6 15 13)))) (BtA (make-matrix 3 3 :initial-contents '((-4 9 5) (-6 12 8) (-4 8 6)))) (Ainv (make-matrix 3 3 :initial-contents '((4 0 1) (0.5 0.5 0.5) (2.5 -0.5 0.5))))) (assert-true (matrix-eq A+2B+3 (m+ 3 A (m* 2 B)))) (assert-true (matrix-eq BtA (m* (mt B) A))) (assert-true (matrix-eq Ainv (minv A))))) (defparameter *svd-ma* (make-matrix 4 2 :initial-contents '((2 4) (1 3) (0 0) (0 0)))) ;; bidiagonalize ;; - (m* (mt *U*) *U*) shoulde be I (defparameter *ma-small* (make-matrix 3 2 :initial-contents '((1 4) (9 16) (25 36)))) (defparameter *ma* (make-matrix 6 5 :initial-contents '((1 2 3 4 5) (6 7 8 9 10) (11 12 13 14 15) (16 17 18 19 20) (21 22 23 24 25) (26 27 28 29 30)))) (defparameter *U* nil) (defparameter *V* nil) (defparameter *D* nil) (defparameter *U2* nil) (defparameter *V2* nil) (defparameter *D2* nil) (multiple-value-setq (*U* *V* *D*) (cl-ml/linalg::bidiagonalize (copy-matrix *ma*))) (multiple-value-setq (*U2* *V2* *D2*) (cl-ml/linalg::bidiagonalize-forward (copy-matrix *ma*))) ;; hessenberg ;; - (m* (mt *Q*) *Q*) shoule be I ;; - (m* (mt *Q*) *A* *Q*) shoule be *H* (defparameter *sq* (make-square-matrix 5 :initial-contents '((1 2 3 4 5) (6 7 8 9 10) (11 12 13 14 15) (16 17 18 19 20) (21 22 23 24 25)))) (defparameter *Q* nil) (defparameter *H* nil) (defparameter *Q2* nil) (defparameter *H2* nil) (multiple-value-setq (*Q* *H*) (cl-ml/linalg::hessenberg (copy-matrix *sq*))) (multiple-value-setq (*Q2* *H2*) (cl-ml/linalg::hessenberg-forward (copy-matrix *sq*)))
6,920
Common Lisp
.lisp
158
31.158228
95
0.484813
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
6b49f571a5168ff749128a05b03bec7b825d96e67fe85682e22fef8fef44f570
17,301
[ -1 ]
17,302
algo-test.lisp
jnjcc_cl-ml/test/algo-test.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; (in-package #:cl-ml/test) (define-test split-string-test (let* ((str "ab,cd,ab,ef,gh,abab") (bychr (split-string str #\,)) (bystr (split-string str "ab")) (byspc (split-string str #\Space))) ;; '("ab" "cd" "ab" "ef" "gh" "abab") (assert-eq 6 (length bychr)) (assert-true (string= "ef" (nth 3 bychr))) ;; '("" ",cd," ",ef,gh," "" "") (assert-eq 5 (length bystr)) (assert-true (string= "" (nth 0 bystr))) (assert-true (string= ",cd," (nth 1 bystr))) (assert-true (string= "" (nth 3 bystr))) ;; '(str) (assert-eq 1 (length byspc)) (assert-true (string= str (nth 0 byspc))))) (defun %load-tree-preorder (elems) (let ((root (make-binary-tree (car elems))) (elems (cdr elems)) (tree-queue (make-queue))) (queue-enque tree-queue root) (do ((child (queue-front tree-queue) (queue-front tree-queue))) ((queue-empty tree-queue)) (when (null elems) (return)) (unless (null (car elems)) (set-left-data child (car elems)) (queue-enque tree-queue (get-left-tree child))) (setf elems (cdr elems)) (when (null elems) (return)) (unless (null (car elems)) (set-right-data child (car elems)) (queue-enque tree-queue (get-right-tree child))) (setf elems (cdr elems)) (queue-deque tree-queue)) root)) (define-test binary-tree-test "A binary tree of the form: A / \ B E \ \ C F / / D G / \ H K preorder-visit: ABCDEFGHK" (let ((vlist nil) (root (%load-tree-preorder '(a b e nil c nil f d nil g nil nil nil h k)))) (labels ((collect (data) (push data vlist))) (cl-ml::inorder-visit root #'collect) (assert-true (equal '(b d c a e h g k f) (nreverse vlist))) (setf vlist nil) (cl-ml::preorder-visit root #'collect) (assert-true (equal '(a b c d e f g h k) (nreverse vlist))) (setf vlist nil) (cl-ml::postorder-visit root #'collect) (assert-true (equal '(d c b h k g f e a) (nreverse vlist)))))) (define-test huffman-tree-test "Huffman tree of the form (inner nodes being sum of frequency): 15 / \ 6 9 / \ / \ 3 B D A / \ E C " (let ((text '(a a b c c b b a d e d d a a d))) (multiple-value-bind (huff coding) (huffman-from-list (bincount text) :coded t) (declare (ignore huff)) (assert-true (equal '(1 1) (binvalue coding 'a))) (assert-true (equal '(0 1) (binvalue coding 'b))) (assert-true (equal '(1 0) (binvalue coding 'd))) (assert-true (equal '(0 0 0) (binvalue coding 'e))))))
2,819
Common Lisp
.lisp
81
28.123457
83
0.55185
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
fa1d77b0a36ef7977dd33b613821cba80838425a1fba7edfbfce19a0a62f856c
17,302
[ -1 ]
17,303
forest-test.lisp
jnjcc_cl-ml/test/forest-test.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; ;;;; Random Forest test (in-package #:cl-ml/test) (define-test random-forest-classifier (let ((y (copy-matrix *iris-label*)) ;; 0: petal length, 1: petal width (X (copy-matrix (make-matrix-view *iris-train* :col-view '(2 3)))) (labenc (make-instance 'label-encoder)) (rfclf (make-instance 'rforest-classifier :max-depth 3 :nestimators 10)) (rfoob (make-instance 'rforest-classifier :max-depth 3 :nestimators 10 :oob-score t)) (pred nil)) (with-random-seed (3) (fit-transform labenc y) (fit rfclf X y) (setf pred (predict rfclf X)) (format *test-stream* "~A~%" (confusion-matrix y pred)) (format *test-stream* "~A~%" (accuracy-score y pred)) (assert-true (> (accuracy-score y pred) 0.95)) (format *test-stream* "~A~%" (mt (feature-importance rfclf))) (fit rfoob X y) (format *test-stream* "~A~%" (cl-ml::oob-score rfoob)) (assert-true (> (cl-ml::oob-score rfoob) 0.95)) )))
1,069
Common Lisp
.lisp
24
38.375
93
0.616715
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
2cbeaded8a371bf077712d17c3a5291264d12ca10404259a253da0bf14aa430a
17,303
[ -1 ]
17,304
tests.lisp
jnjcc_cl-ml/test/tests.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; (in-package #:cl-ml/test) (defun run-all-tests () (let ((result (run-tests :all :cl-ml/test))) (test-names result) result))
208
Common Lisp
.lisp
7
27
66
0.648241
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
209be3096078cda410f9b82cac2a25b424ef737e1700942de052f7f5121e3934
17,304
[ -1 ]
17,305
graph-test.lisp
jnjcc_cl-ml/test/graph-test.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; (in-package #:cl-ml/test) (define-test undirected-graph-test (let ((copy (copy-graph *karate-graph*))) (assert-eq 34 (node-count *karate-graph*)) (assert-eq 76 (edge-count *karate-graph*)) (assert-true (has-node *karate-graph* 1)) (assert-true (has-edge *karate-graph* 1 2)) (assert-true (has-edge *karate-graph* 2 1)) (assert-eq 16 (length (get-neighbors *karate-graph* 1))) (remove-node copy 1) (assert-eq 33 (node-count copy)) (assert-eq 60 (edge-count copy)) (assert-true (not (has-node copy 1))) (add-node copy 100) (add-node copy 200) (assert-eq 35 (node-count copy)) (assert-true (not (has-edge copy 100 200))) (add-edge copy 100 200) (assert-eq 61 (edge-count copy)) (assert-eq 16 (length (get-neighbors *karate-graph* 1))) (assert-eq 76 (edge-count *karate-graph*))))
927
Common Lisp
.lisp
23
35.913043
66
0.651111
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
96604d12b691ad532d774d935d6b36b8aec7ed922413da688456fbdadaedb8ce
17,305
[ -1 ]
17,306
neural-test.lisp
jnjcc_cl-ml/test/neural-test.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; (in-package #:cl-ml/test) (defun mlp-classifier-test (epochs lrate) (let ((mlpclf (make-instance 'mlp-classifier :hidden-units '(2) :epochs epochs :learn-rate lrate)) (X (copy-matrix *iris-train*)) (y (load-iris-label-binary 0)) (pred nil)) (fit mlpclf X y) (setf pred (predict mlpclf X)) (format *test-stream* "~A~%" (accuracy-score y pred))))
457
Common Lisp
.lisp
11
36.909091
100
0.641892
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
aa759de676a0080c1ef443f3697d97001bc2acb10641c8311aac0c5392040638
17,306
[ -1 ]
17,307
gbtree-test.lisp
jnjcc_cl-ml/test/gbtree-test.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; ;;;; Gradient Boosting Tree test (in-package #:cl-ml/test) (define-test gradient-boost-regressor (let* ((sz 1000) (X (copy-matrix (mhead *cal-train* sz))) (y (copy-matrix (mhead *cal-label* sz))) (gbleast (make-instance 'gboost-regressor :loss :least :max-depth 3)) (gbleast5 (make-instance 'gboost-regressor :loss :least :max-depth 5)) (gblad (make-instance 'gboost-regressor :loss :lad :max-depth 3))) (fit gbleast X y) (assert-true (< (mean-squared-error y (predict gbleast X)) 0.27)) (fit gbleast5 X y) (assert-true (< (mean-squared-error y (predict gbleast5 X)) 0.16)) (fit gblad X y) (assert-true (< (mean-squared-error y (predict gblad X)) 0.3)) )) (define-test gradient-boost-classifier (let ((y (load-iris-label-binary 0)) ;; petal length, petal width (X (copy-matrix (make-matrix-view *iris-train* :col-view '(2 3)))) (gbclf (make-instance 'gboost-classifier :loss :deviance :max-depth 5))) (fit gbclf X y) (assert-true (> (accuracy-score y (predict gbclf X)) 0.98))))
1,163
Common Lisp
.lisp
25
41.04
80
0.640529
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
96fc3c5353e3258767faa157340a76bb6f07f5a6a96e96b5959c56ccb1da45aa
17,307
[ -1 ]
17,308
prepro-test.lisp
jnjcc_cl-ml/test/prepro-test.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; (in-package #:cl-ml/test) ;; Polynomial Features (defvar *poly-feature* (make-matrix 2 2 :initial-contents '((1 2) (3 4)))) ;; Standard scaler (define-test stand-scaler-iris (let ((train (make-matrix 3 3 :initial-contents '((1 2 3) (2 3 1) (3 1 2)))) (mean (make-row-vector 3 :initial-contents '(2 2 2))) (stand (make-instance 'standard-scaler))) (fit stand train) (assert-true (matrix-eq mean (cl-ml::mu-vector stand))) (transform stand train) (assert-eq 0 (mref train 0 1)) (assert-eq 0 (mref train 1 0)))) ;; Polynomial feature transformer (define-test poly-transformer-test (let ((poly (make-instance 'poly-transformer :degree 2 :expand-bias nil)) (x-poly (make-matrix 2 5 :initial-contents '((1 2 1 2 4) (3 4 9 12 16)))) (poly-bias (make-instance 'poly-transformer :degree 2)) (x-bias (make-matrix 2 6 :initial-contents '((1 1 2 1 2 4) (1 3 4 9 12 16))))) (assert-true (matrix-eq x-poly (fit-transform poly *poly-feature*))) (fit poly-bias *poly-feature*) (assert-true (matrix-eq x-bias (transform poly-bias *poly-feature*))))) ;; Label encoder from iris dataset (define-test label-encoder-iris (let ((lencoder (make-instance 'label-encoder)) ;; Iris-setosa, Iris-setosa, Iris-versicolor, Iris-setosa, Iris-virginica (y-label (copy-matrix (make-matrix-view *iris-label* :row-view '(0 10 50 20 100)))) (y-trans (make-matrix 5 1 :initial-contents '((0) (0) (1) (0) (2))))) (let ((y-copy (copy-matrix y-label))) (assert-true (matrix-eq y-trans (fit-transform lencoder y-label))) (assert-true (matrix-eq y-copy (inv-transform lencoder y-label) #'string=)))))
1,861
Common Lisp
.lisp
35
45.628571
91
0.617792
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
aa3cfc1d22d627f7822f29e611ab653b98272b3673b5f4931dd7542b041e347d
17,308
[ -1 ]
17,309
xgboost-test.lisp
jnjcc_cl-ml/test/xgboost-test.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; ;;;; XGBoost test (in-package #:cl-ml/test) (define-test xgboost-regressor-test (let* ((sz 1000) (X (copy-matrix (mhead *cal-train* sz))) (y (copy-matrix (mhead *cal-label* sz))) (xgreg (make-instance 'xgboost-regressor :max-depth 5))) (fit xgreg X y) (assert-true (< (mean-squared-error y (predict xgreg X)) 0.16))))
429
Common Lisp
.lisp
11
34.454545
69
0.627404
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
a438b3dd3c85e1bd8a6c7d783430c42d1e06a371f15abc599ab904beb84017e0
17,309
[ -1 ]
17,310
common.lisp
jnjcc_cl-ml/test/common.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; (in-package #:cl-ml/test) (defvar *test-stream* nil) ;;; utility functions (defun matrix-eq (ma mb &optional (eq #'=)) (when (and (= (nrow ma) (nrow mb)) (= (ncol ma) (ncol mb))) (do-matrix (i j ma) (unless (funcall eq (mref ma i j) (mref mb i j)) (return-from matrix-eq nil))) (return-from matrix-eq t))) (defun make-float-eq (epsilon) (lambda (a b) (cl-ml::float= a b epsilon))) ;;; TODO: unfortunately, this is not portable #+sbcl (defmacro with-random-seed ((seed) &body body) `(let ((*random-state* (sb-ext:seed-random-state ,seed))) ,@body)) #-sbcl (defmacro with-random-seed ((seed) &body body) (declare (ignore seed)) `(progn ,@body))
768
Common Lisp
.lisp
24
28.791667
66
0.63365
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
d0cf4b578e1a26f150a2e3a11af3c0addf1f6ad7e7b9ce7b507782705685b034
17,310
[ -1 ]
17,311
dtree-test.lisp
jnjcc_cl-ml/test/dtree-test.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; ;;;; Decision Tree test (in-package #:cl-ml/test) (define-test decision-tree-classifier "Binary tree of the form: [length <= 2.45] / \ Iris-setosa [width <= 1.75] / \ Iris-versicolor Iris-virginica " (let ((y (copy-matrix *iris-label*)) ;; 0: petal length, 1: petal width (X (copy-matrix (make-matrix-view *iris-train* :col-view '(2 3)))) (labenc (make-instance 'label-encoder)) (dtclf (make-instance 'dtree-classifier :max-depth 3)) (pred nil)) (fit-transform labenc y) (fit dtclf X y) (setf pred (predict dtclf X)) (format *test-stream* "~A~%" (confusion-matrix y pred)) (format *test-stream* "~A~%" (accuracy-score y pred)) (let* ((droot (slot-value dtclf 'cl-ml::droot)) (pdata (cl-ml::get-tree-data droot)) (ldata (cl-ml::get-tree-data (cl-ml::get-left-tree droot))) (rchild (cl-ml::get-right-tree droot)) (rdata (cl-ml::get-tree-data rchild)) (rldata (cl-ml::get-tree-data (cl-ml::get-left-tree rchild))) (rrdata (cl-ml::get-tree-data (cl-ml::get-right-tree rchild)))) ;; parent: petal length (assert-eq 0 (slot-value pdata 'cl-ml::feature)) (assert-true (cl-ml::float= 2.45 (slot-value pdata 'cl-ml::threshold) 0.01)) (assert-true (cl-ml::float= 0.667 (slot-value pdata 'cl-ml::impurity) 0.001)) ;; left child: Iris-setosa (assert-true (string= "Iris-setosa" (cl-ml::get-encoder-label labenc (slot-value ldata 'cl-ml::value)))) ;; right child: petal width (assert-eq 1 (slot-value rdata 'cl-ml::feature)) (assert-true (cl-ml::float= 1.75 (slot-value rdata 'cl-ml::threshold) 0.01)) (assert-eq 100 (slot-value rdata 'cl-ml::nsamples)) ;; left child of right child: Iris-versicolor (assert-true (string= "Iris-versicolor" (cl-ml::get-encoder-label labenc (slot-value rldata 'cl-ml::value)))) ;; right child of right child: Iris-virginica (assert-true (string= "Iris-virginica" (cl-ml::get-encoder-label labenc (slot-value rrdata 'cl-ml::value)))))))
2,505
Common Lisp
.lisp
50
38.3
85
0.54097
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
6ee0c1a0f7ad11ae54086899007b8cd8631c57ed0474c815f524aa536e57b9b9
17,311
[ -1 ]
17,312
metrics-test.lisp
jnjcc_cl-ml/test/metrics-test.lisp
;;;; Copyright (c) 2012-2015 jnjcc, Yste.org. All rights reserved. ;;;; (in-package #:cl-ml/test) (define-test clf-metrics-test (let ((ylabl (make-vector 10 :initial-contents '(1 0 0 0 1 0 1 0 0 0))) (ypred (make-vector 10 :initial-contents '(1 1 0 0 1 1 1 0 0 0)))) (assert-eq 0.6 (precision-score ylabl ypred)) (assert-eq 1.0 (recall-score ylabl ypred)) (assert-eq 0.8 (accuracy-score ylabl ypred)) (let ((float-eq (make-float-eq 0.01))) (assert-true (funcall float-eq 0.75 (f1-score ylabl ypred)))) (let ((expect (make-square-matrix 2 :initial-contents '((3 0) (2 5)))) (cmatrix (confusion-matrix ylabl ypred))) (assert-true (matrix-eq expect cmatrix)) (multiple-value-bind (tp fn fp tn) (cl-ml::binary-confusion ylabl ypred 1) (assert-eq tp (mref expect 0 0)) (assert-eq fn (mref expect 0 1)) (assert-eq fp (mref expect 1 0)) (assert-eq tn (mref expect 1 1)))))) (define-test clf-auc-test (let ((ylabl (make-vector 4 :initial-contents '(1 1 2 2))) (ypred (make-vector 4 :initial-contents '(0.1 0.4 0.35 0.8)))) (multiple-value-bind (fpr tpr thresh) (roc-curve ylabl ypred 2) (assert-true (equal '(0.0 0.0 0.5 0.5 1.0) fpr)) (assert-true (equal '(0.0 0.5 0.5 1.0 1.0) tpr)) (assert-true (equal '(1.0 0.8 0.4 0.35 0.1) thresh))) (assert-eq 0.75 (roc-auc-score ylabl ypred 2)))) (define-test reg-metrics-test (let ((ylabl (make-vector 4 :initial-contents '(3 -0.5 2 7))) (ypred (make-vector 4 :initial-contents '(2.5 0.0 2 8)))) (assert-eq 0.375 (mean-squared-error ylabl ypred))))
1,634
Common Lisp
.lisp
31
47.064516
80
0.623515
jnjcc/cl-ml
3
0
0
GPL-2.0
9/19/2024, 11:27:44 AM (Europe/Amsterdam)
4242ad9e97eaa52223ec46ce1909d57f69de8794885d3585ead086886fd48ce1
17,312
[ -1 ]