Skip to content

Commit

Permalink
Merge pull request #77 from pronoiac/copypasta-lisp
Browse files Browse the repository at this point in the history
Copypasta lisp
  • Loading branch information
norvig committed Dec 9, 2018
2 parents 2f6240f + 9851980 commit be493c0
Show file tree
Hide file tree
Showing 49 changed files with 320 additions and 319 deletions.
9 changes: 4 additions & 5 deletions docs/chapter1.md
Expand Up @@ -534,8 +534,8 @@ Although all non-nil values are considered true, by convention the constant `t`
(defun first-name (name)
"Select the first name from a name represented as a list."
(if (member (first name) *titles*)
(first-name (rest name))
(first name)))
(first-name (rest name))
(first name)))
```

When we map the new `first-name` over the list of names, the results are more encouraging.
Expand Down Expand Up @@ -688,7 +688,7 @@ This problem can be solved very easily using `mappend` as a component:
```lisp
(defun numbers-and-negations (input)
"Given a list, return only the numbers and their negations."
(mappend #' number-and-negation input))
(mappend #'number-and-negation input))
(defun number-and-negation (x)
"If x is a number, return a list of x and -x."
Expand Down Expand Up @@ -1145,7 +1145,6 @@ Example:
(cond ((= n 0) 1)
((evenp n) (expt (power x (/ n 2)) 2))
(t (* x (power x (- n 1))))))
```

### Answer 1.3
Expand All @@ -1159,7 +1158,7 @@ Example:
(count-atoms (rest exp))))))
(defun count-all-atoms (exp &optional (if-null 1))
"Return the total number of atoms in the expression,
"Return the total number of atoms in the expression,
counting nil as an atom only in non-tail position."
(cond ((null exp) if-null)
((atom exp) 1)
Expand Down
18 changes: 10 additions & 8 deletions docs/chapter2.md
Expand Up @@ -156,12 +156,14 @@ For example:
```lisp
(defun Adj* ()
(if (= (random 2) 0)
nil
(append (Adj) (Adj*))))
nil
(append (Adj) (Adj*))))
(defun PP* ()
(if (random-elt '(t nil))
(append (PP) (PP*))
nil))
(append (PP) (PP*))
nil))
(defun noun-phrase () (append (Article) (Adj*) (Noun) (PP*)))
(defun PP () (append (Prep) (noun-phrase)))
(defun Adj () (one-of '(big little blue green adiabatic)))
Expand Down Expand Up @@ -214,7 +216,7 @@ The list of rules can then be represented as follows:
"A grammar for a trivial subset of English.")
(defvar *grammar* *simple-grammar*
"The grammar used by generate. Initially, this is
"The grammar used by generate. Initially, this is
*simple-grammar*, but we can switch to other grammars.")
```

Expand All @@ -240,11 +242,11 @@ We will need three functions: one to get the right-hand side of a rule, one for

```lisp
(defun rule-lhs (rule)
"The left-hand side of a rule."
"The left hand side of a rule."
(first rule))
(defun rule-rhs (rule)
"The right-hand side of a rule."
"The right hand side of a rule."
(rest (rest rule)))
(defun rewrites (category)
Expand Down Expand Up @@ -460,7 +462,7 @@ Still, the complete program is quite simple:
E.g., (combine-all '((a) (b)) '((1) (2)))
-> ((A 1) (B 1) (A 2) (B 2))."
(mappend #'(lambda (y)
(mapcar #'(lambda (x) (append . y)) xlist))
(mapcar #'(lambda (x) (append x y)) xlist))
ylist))
```

Expand Down
42 changes: 21 additions & 21 deletions lisp/auxfns.lisp
Expand Up @@ -18,7 +18,7 @@
#+Lispworks
(setq *PACKAGES-FOR-WARN-ON-REDEFINITION* nil)

#+LCL
#+LCL
(compiler-options :warnings nil)
#+sbcl
(progn
Expand All @@ -36,13 +36,13 @@
(defvar *paip-modules* '())

(defvar *paip-files*
`("auxfns" "tutor" "examples"
"intro" "simple" "overview" "gps1" "gps" "eliza1" "eliza" "patmatch"
"eliza-pm" "search" "gps-srch" "student" "macsyma" "macsymar" "unify"
"prolog1" "prolog" "prologc1" "prologc2" "prologc" "prologcp"
"clos" "krep1" "krep2" "krep" "cmacsyma" "mycin" "mycin-r" "waltz"
"othello" "othello2" "syntax1" "syntax2" "syntax3" "unifgram"
"grammar" "lexicon" "interp1" "interp2" "interp3"
`("auxfns" "tutor" "examples"
"intro" "simple" "overview" "gps1" "gps" "eliza1" "eliza" "patmatch"
"eliza-pm" "search" "gps-srch" "student" "macsyma" "macsymar" "unify"
"prolog1" "prolog" "prologc1" "prologc2" "prologc" "prologcp"
"clos" "krep1" "krep2" "krep" "cmacsyma" "mycin" "mycin-r" "waltz"
"othello" "othello2" "syntax1" "syntax2" "syntax3" "unifgram"
"grammar" "lexicon" "interp1" "interp2" "interp3"
"compile1" "compile2" "compile3" "compopt"))

(defun requires (&rest files)
Expand All @@ -62,9 +62,9 @@
"The location of the source files for this book. If things don't work,
change it to reflect the location of the files on your computer.")

(defparameter *paip-source*
(defparameter *paip-source*
(make-pathname :name nil :type "lisp" ;;??? Maybe Change this
:defaults *paip-directory*))
:defaults *paip-directory*))

(defparameter *paip-binary*
(make-pathname
Expand All @@ -81,7 +81,7 @@
:defaults *paip-directory*))

(defun paip-pathname (name &optional (type :lisp))
(make-pathname :name name
(make-pathname :name name
:defaults (ecase type
((:lisp :source) *paip-source*)
((:binary :bin) *paip-binary*))))
Expand Down Expand Up @@ -169,7 +169,7 @@
"Find all those elements of sequence that match item,
according to the keywords. Doesn't alter sequence."
(if test-not
(apply #'remove item sequence
(apply #'remove item sequence
:test-not (complement test-not) keyword-args)
(apply #'remove item sequence
:test (complement test) keyword-args)))
Expand Down Expand Up @@ -208,15 +208,15 @@
new-length, if that is longer than the current length."
(if (and (arrayp array)
(array-has-fill-pointer-p array))
(setf (fill-pointer array)
(setf (fill-pointer array)
(max (fill-pointer array) new-length))))

;;; ==============================

;;; NOTE: In ANSI Common Lisp, the effects of adding a definition (or most
;;; anything else) to a symbol in the common-lisp package is undefined.
;;; Therefore, it would be best to rename the function SYMBOL to something
;;; else. This has not been done (for compatibility with the book).
;;; Therefore, it would be best to rename the function SYMBOL to something
;;; else. This has not been done (for compatibility with the book).

(defun symbol (&rest args)
"Concatenate symbols or strings to form an interned symbol"
Expand All @@ -237,15 +237,15 @@
Like mapcon, but uses append instead of nconc."
(apply #'append (mapcar fn list)))

(defun mklist (x)
(defun mklist (x)
"If x is a list return it, otherwise return the list of x"
(if (listp x) x (list x)))

(defun flatten (exp)
"Get rid of imbedded lists (to one level only)."
(mappend #'mklist exp))

(defun random-elt (seq)
(defun random-elt (seq)
"Pick a random element out of a sequence."
(elt seq (random (length seq))))

Expand Down Expand Up @@ -405,7 +405,7 @@
"Place a no-longer-needed element back in the pool."
(vector-push-extend ,name ,resource))
,(if (> initial-copies 0)
`(mapc #',deallocate (loop repeat ,initial-copies
`(mapc #',deallocate (loop repeat ,initial-copies
collect (,allocate))))
',name)))

Expand Down Expand Up @@ -456,7 +456,7 @@

;;;; Other:

(defun sort* (seq pred &key key)
(defun sort* (seq pred &key key)
"Sort without altering the sequence"
(sort (copy-seq seq) pred :key key))

Expand All @@ -468,7 +468,7 @@

;;; ==============================

(defun length=1 (x)
(defun length=1 (x)
"Is x a list of length 1?"
(and (consp x) (null (cdr x))))

Expand Down Expand Up @@ -561,7 +561,7 @@
(do-result (i)
(if (and (vectorp result-sequence)
(array-has-fill-pointer-p result-sequence))
(setf (fill-pointer result-sequence)
(setf (fill-pointer result-sequence)
(max i (fill-pointer result-sequence))))))
(declare (inline do-one-call))
;; Decide if the result is a list or vector,
Expand Down
4 changes: 2 additions & 2 deletions lisp/clos.lisp
Expand Up @@ -4,7 +4,7 @@

;;;; File clos.lisp: Object-oriented programming examples

(defstruct account
(defstruct account
(name "") (balance 0.00) (interest-rate .06))

(defun account-withdraw (account amt)
Expand Down Expand Up @@ -85,7 +85,7 @@

(defun generic-fn-p (fn-name)
"Is this a generic function?"
(and (fboundp fn-name)
(and (fboundp fn-name)
(eq (get fn-name 'generic-fn) (symbol-function fn-name))))

;;; ==============================
Expand Down
6 changes: 3 additions & 3 deletions lisp/cmacsyma.lisp
Expand Up @@ -26,7 +26,7 @@

(deftype polynomial () 'simple-vector)

(defsetf main-var (p) (val)
(defsetf main-var (p) (val)
`(setf (svref (the polynomial ,p) 0) ,val))

(defsetf coef (p i) (val)
Expand All @@ -37,7 +37,7 @@
(defun degree (p) (- (length (the polynomial p)) 2))

(defun poly (x &rest coefs)
"Make a polynomial with main variable x
"Make a polynomial with main variable x
and coefficients in increasing order."
(apply #'vector x coefs))

Expand All @@ -58,7 +58,7 @@
(mapcar #'prefix->canon (exp-args x))))
(t (error "Not a polynomial: ~a" x))))

(dolist (item '((+ poly+) (- poly-) (* poly*poly)
(dolist (item '((+ poly+) (- poly-) (* poly*poly)
(^ poly^n) (D deriv-poly)))
(setf (get (first item) 'prefix->canon) (second item)))

Expand Down
6 changes: 3 additions & 3 deletions lisp/compile1.lisp
Expand Up @@ -54,7 +54,7 @@
"Compile a lambda form into a closure with compiled code."
(assert (and (listp args) (every #'symbolp args)) ()
"Lambda arglist must be a list of symbols, not ~a" args)
;; For now, no &rest parameters.
;; For now, no &rest parameters.
;; The next version will support Scheme's version of &rest
(make-fn
:env env :args args
Expand Down Expand Up @@ -112,7 +112,7 @@
(if (atom name)
`(name! (set! ,name . ,body) ',name)
(scheme-macro-expand
`(define ,(first name)
`(define ,(first name)
(lambda ,(rest name) . ,body)))))

(defun name! (fn name)
Expand All @@ -130,7 +130,7 @@

(defun show-fn (fn &optional (stream *standard-output*) (depth 0))
"Print all the instructions in a function.
If the argument is not a function, just princ it,
If the argument is not a function, just princ it,
but in a column at least 8 spaces wide."
(if (not (fn-p fn))
(format stream "~8a" fn)
Expand Down
10 changes: 5 additions & 5 deletions lisp/compile2.lisp
Expand Up @@ -40,7 +40,7 @@
"Report an error if form has wrong number of args."
(let ((n-args (length (rest form))))
(assert (<= min n-args max) (form)
"Wrong number of arguments for ~a in ~a:
"Wrong number of arguments for ~a in ~a:
~d supplied, ~d~@[ to ~d~] expected"
(first form) form n-args min (if (/= min max) max))))

Expand Down Expand Up @@ -97,7 +97,7 @@
(let ((L2 (gen-label)))
(seq pcode (gen 'TJUMP L2) ecode (list L2)
(unless more? (gen 'RETURN)))))
((null ecode) ; (if p x) ==> p (FJUMP L1) x L1:
((null ecode) ; (if p x) ==> p (FJUMP L1) x L1:
(let ((L1 (gen-label)))
(seq pcode (gen 'FJUMP L1) tcode (list L1)
(unless more? (gen 'RETURN)))))
Expand Down Expand Up @@ -143,7 +143,7 @@

;;; ==============================

(defstruct (prim (:type list))
(defstruct (prim (:type list))
symbol n-args opcode always side-effects)

;;; Note change from book: some of the following primitive fns have had
Expand All @@ -157,10 +157,10 @@
(/= 2 /= nil nil) (= 2 = nil nil)
(eq? 2 eq nil nil) (equal? 2 equal nil nil) (eqv? 2 eql nil nil)
(not 1 not nil nil) (null? 1 not nil nil) (cons 2 cons true nil)
(car 1 car nil nil) (cdr 1 cdr nil nil) (cadr 1 cadr nil nil)
(car 1 car nil nil) (cdr 1 cdr nil nil) (cadr 1 cadr nil nil)
(list 1 list1 true nil) (list 2 list2 true nil) (list 3 list3 true nil)
(read 0 read nil t) (write 1 write nil t) (display 1 display nil t)
(newline 0 newline nil t) (compiler 1 compiler t nil)
(newline 0 newline nil t) (compiler 1 compiler t nil)
(name! 2 name! true t) (random 1 random true nil)))

(defun primitive-p (f env n-args)
Expand Down

0 comments on commit be493c0

Please sign in to comment.