cflect of a cons » to erratę a dottcd pair. the list nota non m dcancr and is generałły prp-
ir iwo aioms arc consed together. (be result is always wriitcn usmg dottcd pair noration. The cdr of a dottcd paii is thc sccond element in thc pair. rather than a hu conuining the sccoad atom. For cxamp!e:
> (eona 'a *b)
(a.b)
> (car (a b))
•
> (cdr'(a.b))
|
Dottcd pairs occur naturally in association lists when one atom i« uscd as a kcy far retrioing another atom. as wcil as in other applications that requirc thc formation and mampulation of pairs of atomie symbołs. Bccausc unifications often substitutc a single atom for a variablc. dottcd pairs appear oflen in the association list retumed by the unifica* tka lunet ion.
Along with assoc. Common LISP defines the function acons. which takes as argu-ments a kcy. a daium and an association list and retums a mew association list whosc fint element is the roult of oonsuig thc key ooto thc datura. For cxamplc
> (acons ‘a 1 nil)
Notc that when acons is givcn two aioms. it adds their cons to thc association list:
> (acons 'pata '(ammi Jack clydo)
'((namo . bill) (hobbios musie skiing movios) (Job . programmor)))
((pata smma Jack cfyda) (nama . bili) (hobbies musie skiing movles)
(Job. programmor))
Members on an association list may be either dottcd pairs or lists.
Association lists providc a convcnicnt way to implcmcnt a varicty of tablcs and other simplc data rctricval schcmcs. In implcmcnting thc unifteation algorithm, we usc association lists to represent sets of suhstitutkras: thc kcys arc thc variuhlcs. and the data arc thc valucs of their bmdings. The datum may be a simplc \ ariable or constant or a morc complicatcd structurc.
l/sśng association lists, thc substitution set functions arc defined: (dfffun got-blnding (var substitution-lisi)
(assoc var substitution-list :test ?'equal»
(dofun get binding-value (binding) (cdr binding)) (dafun add substłtutlon (ver paltem substitution-list) (acons var patiom substitution-list))
PART VI / LANGUAOES FOR Al PROBLEM SOLVING
This coniplctcs thc implcmcmation of the umfication algorithm. We wiD ue « ■ Scction 15.8 lo implcmcnt n simplc PROLOG interpreter,andioScction I5.l0tob«ld» e\pcn system shcil.
The top lcvcl of thc LISP interpreter is known as thc reai^ni-fńś loop. Thit describes ibc interpreter \ bcharór in reading, cvaluating. and pcinung thc sake of s-cxprcssioo* entered by the user. The eval function, defined in Scctwo 15.1.-. is thc beart of the LISP ■terpreter, using eval. it is possiblc to writc LISPs top-loci read-oval-prinl loop in USP itsclf. In thc ncxt cxamplc, we <Icvclop a siraplifiedvcrs»oo of this loop. This vcr>ion is simplificd chicfly in that it docs not ha\'c thc error-hmdlins abilibes of the built-in loop, although USP docs providc thc functionality nccded to iapkmtm such capabilitie*.
To writc thc read-eval-pńnt loop.we use t*o morc LISP functions, read and print. read is a function that takes no parametm; when it is csaluatcd. h relums the next s* opression entered at the keyboard, print is a funct»oa dni takes a single argument, cvalu-śa it. and then prints that result to standard outpot Another function that will prore use* r,! is terpri. a function of no argumems that causcs a newline chancter to standard oaput. terpri also retums a value of nil on complction. Using thcsc functions. thc read* eval*print loop is based on a nested s-exprcssion:
(print (eval (road)))
Wben this is cvaluated. thc innermost s*c\pression. (read). is oalofcd fint The sake retumed by thc read. thc ncxt s-exprcssion entered by thc user. b passed lo ®vaf shere it sesahoted. The result of this esahiation is passed to print wberc k is scal lo the dbpfay utea. To complctc thc loop we add a print e\prc*ioe «o otęp# the poapt a tarpri to cotpot a newline aft er the result has becn printed and a recarsńecaB to repeat tkacie. Tbus, thc finał read-eval-print loop is defincd:
; this function takes no argumonts ; output a prompt, ours is a V :read-«vakprint : outpot a neufnt : do ital agam
(defun my-read-eval-prfnt ()
(print ’:)
(print (eval (read)))
(larprl)
(my-read-eval-print))
Tk* may bc uscd “on top of" thc built-in interpreter
> (my*read-eval-print)
: noto tha naw prempt
3
As this cxamplc illustratcs. by making functions soch as quote and oval avaihbłe to fe user. LISP givcs thc programmer a high degrec of conuol mer the handliag of
721
CHAPTER15; AN IłTRODUCTlON TO USP