25 1024x768

25 1024x768



12 3    4    12    3    4

(1234)    (02J34)

Figura \SJ2 Mapplnglistsontotrees.showlng slruclural dfferences.

> (fiJter-negatives '(1 -12 -2 3 -4))

(123)

This cumpłc u typical of tbe way cons is oftcn used in rccur$ive functioos on lim car and Cdr icar lists apari and “dme“ ihc rccursion: cons sclcctivcly construcis ihc rcsult as the rccursion “urmiods" Anocber cxamp!c of ihis usc of cons is in rcdcfining tbe buift-in funciion append:

(dofun my-append (llstl lls(2)

(cónd ((nuli llstl) Iist2)

(t (cons (car llstl) (my-append (cdr llstl) Iist2))))) wluch yiclds thc bchavior:

>    (my-append'(t 2 3)'(4 5 6))

(123456)

Noce thai the same rccursńe schemc is used in the dcfinitions of my-append my-length. and my-member. Each dcfinition uses thc cdr funciion to rcmovc an cleniem from thc list, allowing a recurśiyc cali on thc shortened list; thc rccursion "bottomsouf oo thc empty lisi. As thc rccursion unwinds, thc cons funciion rcasscmblcs thc solution.Thit panicular schcmc is known as cdr rccursion or somelimes lali rccursion. bccausc it uses the cdr funciion to lmcariy scan thc element* of a list.

15.1.7 Ncstcd Lisis, Structurc, and car/cdr Rccursion

Although both cons and append may bc used to combinc smallcr lists into a single lift a is importu* to noto thc diftcrcncc between thesc t\vo functions. If cons is called with lep lists as arguments, it makes thc first of thesc a ncw first element of thc sccond lisi. whereu append wwmś a list whosc clcmcnts arc thc clcmcnts of thc two nrgumcnis:

>    (eon* '(12)’(34))

((12)34)

>    (append (12)‘(3 4))

(1234)

PART VI / LAMGUAGES FOR Al PROBLEM S0LVING

thc li*l* <*1 2 3 4) and «*f 2) 3 -4) ho\c fundomcncally dśffcrcol %tnicturc». This dificreocc owy bc noicd graphically by cxploiting thc isomorphivm bctx«ccn lists and trccs. Tbc simplcst way to map lists onto trccs is to crcalc an unlabdcd nodc for cach li%«. with dcsccndants cqual to Ihc clcmcnts of" that list. This ruEc is applicd recursivcly to «Kc clcmcnts of thc list thai aro thcmsclvcs lists; clcmcnts that aro aloms aro msppud onto Ical nodcs of ihc trcc. Tltus. thc two lists mcntioned sbovc geocratc thc difTcrcnt Irce structurcs illussratod in Figurc I 3.2.

This cxamplc illustratcs thc reprcscntational power of lists. particularly os a mcons of icprcscnting any trcc structurc such as a scarch troć or a parsc trcc (Fijprrc 15.1). In •ddition. ncstcd lists providc a way ot* hicrarchically structuring c<wnplcx dala. In ihc cmploycc rccords cxamplc ot"Scction 13.1 .4. thc nanic field sas iisclf a list consistmg of"a lim namc and a last namc. *THis list could bc treated as a single cniity or its indiv»dual componcnls could bc acccsscd.

The simplc cdr-rccursivc schcmc di seussed in thc prcvious scction is not sufłicicnt to implcmcnt mlI manipulations on ncstcd lists. bccausc it docs not disdnguish befseen items ihat aro lists and thasc that aro simplc aloms. Suppose. for cxamplc. that thc leogtti funciion defined an Scction I 3. 1 .b is apg>Iicd to a ncstcd list structurc:

> <hnfllh ‘((1 2> 3 <1 (4 <5»>»

la il>c abovc examplc. length retums 3 bccausc tftio list has 3 clcmcnts. < T 2>_ 3. and (1 (4 (5))). This is. of coursc. thc correct and desired bchavior for a length funciion.

On thc other hand. if* wv wnnl thc funciion co couni thc number of"ofowu in thc list. sto aeed a cli florent rccursivc schcmc. one that. in addilion to scanning along thc element* of ehe list. “opens up" non -atom ic list clcmcnts and rccumiwly upplacs irsdf to thc taslc of countingg thoir atoms. We dcfinc this funciion. callcd count-atoms. which bdmes:

(dofun count-atoms (my-list)

(eond ((nuli my-list) O)

((atom my-list) 1 >

: opon up on domami ; ocan down tho list


<t <*•* (count-atoms (car my-list))

(count-atoms (cdr my-list»)»)

> (count-atoms #«T 2> 3 (((4 5 <6))»»

6

The abovc definition is ara cxamplc of car-cdr rccursion. Irtstead of just rccurring o«* theedrofthe list. count-atoms also rccurs on thc carofiis argument, with thc functioo combining thc two components anto on ansucr. Rccursion halts when it cncotmiers on atom or cmply list. One way of" thinking or this schcmc is that it adds a sccond dimension to simplc Odr rccursion. that ofMgoing dow n into" cach or thc list clcmcnts. Compatc thc dogranis of calls to length and count-atoms in Figurę 13.3. Notc thc stmiUriiy of car-ctir rccursion and thc rccursivc definition or s-cxpressaoau givcn in Scction I 3. 1.1.

Anoiłicr cxnmplc or thc usc or car-cdr rccursion is in the definition of thc funciion ftatten. fiat ton tnkes as argument a list ofarbimtr>' stmciure and retums a list that consisis ®f the same aloms in thc same order* but with all thc atoms at thc same lewi. Notc thc


Wyszukiwarka

Podobne podstrony:
img009 (69) 172 Percepcja i waloryzacja przestrzeni miasta ... rynek 25 targowisko 12 Studnia Trz
IMG?25 59,12,2,DZIAŁALNOŚĆ POSTPRODUKCYJNA ZWIĄZANA Z FILMAMI NAGRANIAMI WIDEO I PROGRAMAMI
2015 01 25 07 12?3 (1) £ol.oS - ** M    fJŚO&MS °tt.    pĄĄ*J>
28 luty 09 (42) 43 43 = A/_,+A/_ A _ 5.16 + 0.6 105.06 = 0.055 m em t 0,055 0.25= 0.22 12.0 Kff _ 3
Skanowanie 14 03 25 23 (12) WŁAŚCIWOŚCI FIZYCZNE KRUSZYW DROBNYCH PODLEGAJACYCH OCENIE W RAMACH
Skanowanie 14 03 25 23 (12) WŁAŚCIWOŚCI FIZYCZNE KRUSZYW DROBNYCH PODLEGAJACYCH OCENIE W RAMACH
298 (25) Kampfgruppe z 12 dywizji SS przejeżdża przez Caen. „A co znajdowało się za nami? - dymiące
300 (25) Ryc. 12.6. Obra/ nukjwkopowo<kktrooowy DNA bakteriofaga Vi III zmikmikopu elektrono*ego
318 (25) Zależność (12.16) wykorzystuje się do ogólnej charakterystyki związków optycznie czynnych (
17748 OILPOL61 WEST AFRICA 25 N.M. ZONĘ 12 N.M. ZONĘ 3 N.M. ZONĘ
8-93 b 32-miej acowy Al anunoryczno 49 k l jiyiszy 25.U x 13 x 2.5 12 2 lub 32 K znaki Alf
201312275217 (1024x768) 12.    Widzenie nocą (pręcikowie, Sltolopowlc) 13.  &nb

więcej podobnych podstron