Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a defstruct database with remove-if-not
    text
    copied!<p>I'm trying to adapt this <a href="http://mypage.iu.edu/~colallen/lp/node56.html" rel="nofollow noreferrer">defstruct</a> example by adding the <strong>select-</strong> functions described in the book: <a href="http://www.gigamonkeys.com/book/practical-a-simple-database.html" rel="nofollow noreferrer">Practical Common Lisp</a>. I'm running the code in Emacs using the Common Lisp package. The <strong>select-by-first</strong> does not return anything. In the Lisp book, the author does not use defstruct so I must need to do something slightly different?</p> <pre> (defun <strong>select-by-first</strong> (first-name) (remove-if-not #'(lambda (employee) (equal (getf employee :first-name) first-name)) *emp-db*)) (select-by-first "steve") </pre> <p>The complete program:</p> <pre><code>(require 'cl) ;; http://mypage.iu.edu/~colallen/lp/node56.html ;; http://www.gigamonkeys.com/book/practical-a-simple-database.html ;; (defvar *emp-db* nil) (defun add-record (emp) (push emp *emp-db*)) (defstruct employee age first-name last-name sex children)</code> </pre> <pre> <code>(add-record (make-employee)) (add-record (make-employee :age 34 :last-name 'farquharson :first-name 'alice :sex 'female)) (add-record (make-employee :age 43 :last-name 'jobs :first-name 'steve :sex 'male)) (add-record (make-employee :age 53 :last-name 'ballmer :first-name 'steve :sex 'male))</code> </pre> <pre> <code>(defun select-by-first (first-name) (remove-if-not #'(lambda (employee) (equal (getf employee :first-name) first-name)) *emp-db*)) (select-by-first "steve")</code> </pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload