;;;2.3 lisp ;;;Adam nelson ;;;Chet tobrey (defun mappend (fn list) "Append the results of calling fn on each element of the list. Like mancon, but uses append instead of nconc." (apply #'append (mapcar fn list))) (defparameter *simple-grammar* '((sentence -> (noun-phrase verb-phrase)) (noun-phrase -> (Article Noun)) (verb-phrase -> (Verb noun-phrase)) (Article -> the a) (Noun -> man ball woman table) (Verb -> hit took saw liked)) "A grammar for a trivial subset of English.") (defvar *grammar* *simple-grammar*) (defun rule-lhs (rule) "left hand side of a rule" (first rule)) (defun rule-rhs(rule) "right hand side of a rule" (rest (rest rule))) (defun rewrites (category) "return a list of possible rewrites for this category" (rule-rhs (assoc category *grammar*))) (defun generate (phrase) "Generate a random sentence or phrase" (cond ((listp phrase) (mappend #'generate phrase)) ((rewrites phrase) (generate (random-elt(rewrites phrase)))) (t (list phrase)))) (egs :2.3 (eg '(assoc 'noun *grammar*) :of "using assoc to take a key and a list of lists to return the first element of the list of lists that starts with the key") (eg '(generate 'sentence) :of "generating a sentence") (eg '(generate 'sentence) :of "generating a sentence") (eg '(generate 'noun-phrase) :of "generating a noun-phrase") (eg '(generate 'verb-phrase) :of "generating a verb-phrase"))