(I'm talking to myself, a bad sign!)
> If I was serious about "improper = length 1" then it should be "(+ a .
> z)"instead.
Hmm, now that I've played with it, I think we should rule that the "improper"
end of a list has length zero, not length 1. After all, if the empty list is in
that final position, then we'd say it is length 0. So basically, wherever the
list ends at a not-pair, that's when we stop counting. This rule is simple,
and it generalizes the list processing in a few places to ask about "not pair?"
instead of "null?". That seems like the plausibly-correct semantics.
Remarkably, this *reduces* the implementation code by one line, because it no
longer has to include a special case test to reject improper lists.
Examples - given these:
{1 + 2}
{ . x}
{1 . x}
{1 + . x}
{1 + 2 . x}
{1 + 2 + . x}
{1 + 2 + 3 . x}
{1 + 2 + 3 + . x}
This interpretation yields these:
(+ 1 2)
(nfx . x)
(nfx 1 . x)
(nfx 1 + . x)
(+ 1 2 . x)
(nfx 1 + 2 + . x)
(+ 1 2 3 . x)
(nfx 1 + 2 + 3 + . x)
A Common Lisp implementation has to be careful, because empty lists *are* lists
in CL (it slightly different semantics than Scheme regarding empty lists), but
it'd be no harder to implement in Common Lisp.
Comments?
--- David A. Wheeler
--- srfi-neoteric.scm 2012-08-17 08:30:11.016314393 -0400
+++ ,2 2012-08-17 10:09:40.660663736 -0400
@@ -95,16 +95,15 @@
; Return true if lyst has an even # of parameters, and the (alternating)
; first parameters are "op". Used to determine if a longer lyst is infix.
; If passed empty list, returns true (so recursion works correctly).
(define (even-and-op-prefix? op lyst)
(cond
- ((null? lyst) #t)
- ((not (pair? lyst)) #f) ; Not a list.
+ ((not (pair? lyst)) #t) ; We're at end of list; improper lists allowed.
((not (eq? op (car lyst))) #f) ; fail - operators not the same
- ((null? (cdr lyst)) #f) ; fail - wrong # of parameters in lyst.
+ ((not (pair? (cdr lyst))) #f) ; fail - wrong # of parameters in lyst.
(#t (even-and-op-prefix? op (cddr lyst))))) ; recurse.
; Returns true if item is member of lyst, else false.
(define (ismember? item lyst)
(pair? (member item lyst)))
@@ -118,13 +117,13 @@
; this way for performance)
(symbol? (cadr lyst)) ; 2nd parameter must be a symbol.
(even-and-op-prefix? (cadr lyst) (cdr lyst)))) ; true if rest is simple
; Return alternating parameters in a lyst (1st, 3rd, 5th, etc.)
(define (alternating-parameters lyst)
- (if (or (null? lyst) (null? (cdr lyst)))
+ (if (or (not (pair? lyst)) (not (pair? (cdr lyst))))
lyst
(cons (car lyst) (alternating-parameters (cddr lyst)))))
; Transform a simple infix list - move 2nd parameter into first position,
; followed by all the odd parameters. Thus (3 + 4 + 5) => (+ 3 4 5).
(define (transform-simple-infix lyst)
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Readable-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/readable-discuss