racket - How to use symbols and lists in scheme to process data? -
i newbie in scheme, , in process of writing function checks pairwise disjointess of rules (for time being incomplete), used symbols , lists in order represent rues of grammar. uppercase symbol non-terminal in grammar, , lowercase terminal. trying check if rule passes pairwise disjointness test.
i check if rule has 1 unique terminal in it. if case, rule passes pairwise disjointness test. in scheme, thinking realize representing terminal symbol in lower case. example of rule be:
'(a <= (a b c))i check case of rule contains or. like:
'(a <= (a (or (a b) (a c))))finally, check recursively non terminals. rule case be
'(a <= (b b c))however, keeping me stuck how use symbols data in order processed , recurse upon it. thought converting symbols strings, did not in case of having list example
'(a b c)how can it? here reached far:#lang racket (define grammar '(a <= (a b)) ) (define (pairwise-disjoint lst) (print(symbol->string (car lst))) (print( cddr lst)) )
pairwise disjoint
as far know, way check if set pairwise disjoint enumerate every possible pair , check matches. note not follow racket syntax, meaning should still pretty clear.
(define (contains-match? x lst) (cond ((null? x) #f) ; nothing ((null? lst) #f) ; finished walking full list ((eq? x (car lst)) #t) ; found match, no need go further (else (contains-match? x (cdr lst))))) ; recursive call keep walking (define (pairwise-disjoint? lst) (if (null? lst) #f (let ((x (car lst)) ; let inner vars readability (tail (cdr lst))) (not ;; each element, check against later elements in list (or (contains-match? x tail) (contains-match? (car tail) (cdr tail))))))) it's not clear me else you're trying do, going general method. depending on data, may need use different (or custom-made) check equality, works normal symbols:
]=> (pairwise-disjoint? '(a b c d e)) ;value: #t ]=> (pairwise-disjoint? '(a b c d e a)) ;value: #f symbols & data
this section based on perceive pretty fundamental misunderstanding of scheme basics op, , speculation actual goal is. please clarify question if next bit doesn't you!
however, keeping me stuck how use symbols data...
in scheme, can associate symbol whatever want. in fact, define keyword tells interpreter "whenever contains-match? (which symbol) i'm referring big set of instructions on there, remember that." interpreter remembers storing symbol , thing refers in big table can found later.
whenever interpreter runs symbol, in table see if knows means , substitute real value, in case function.
]=> pairwise-disjoint? ;value 2: #[compound-procedure 2 pairwise-disjoint?] we tell interpreter keep symbol in place rather substituting using quote operator, ' or (quote ...):
]=> 'pairwise-disjoint? ;value: pairwise-disjoint? all said, using define purposes poor decision of same reasons global variables bad.
to hold definitions of particular symbols important grammar, you're looking hash table each symbol know key , particulars associated value.
and, if want pass around symbols, need understand quote , quasiquote.
once have definitions somewhere can find them, work that's left writing did above maybe little more tailored particular situation.
Comments
Post a Comment