Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to improve the readability of (any) LISP

17 views
Skip to first unread message

Robert L.

unread,
May 26, 2018, 6:02:18 PM5/26/18
to
> "Pascal J. Bourguignon" <p...@informatimago.com> writes:
>
> > Paul Rubin <no.em...@nospam.invalid> writes:
>
> >> m_mom...@yahoo.com (Mario S. Mommer) writes:
> >>> (defun xfilter (list &key (test #'eql) (key #'identity))
> >>> (let ((tsil (list (car list))))
> >>> (dolist (x (cdr list) (nreverse tsil)) ...
>
> >> Recursive implementation seems simplest if you don't mind the stack
> >> consumption. No fancy macros, just straightforward COND:
>
> > Which is a macro. Perhaps not fancy, but still a macro.
> > DEFUN too is a macro, by the way.
>
> He did say fancy macros. I don't think cond is all that fancy.
>
> >> (defun xfilter (xs)
> >> (cond
> >> ((null xs) nil)
> >> ((null (cdr xs)) xs)
> >> ((eq (car xs) (cadr xs)) (xfilter (cdr xs)))
> >> (t (cons (car xs) (xfilter (cdr xs))))))
>
> >> * (xfilter '(1 2 3 3 3 4 4 3))
> >> (1 2 3 4 3)
>
> > Macroless lisp is more like assembler.
> > Try it again with TAGBODY and GO.
>
> If you want recursion, you might as well make it tail recursive
> which at least some lisps can do effiently.
>
> (defun xfilter (list &optional (result nil))
> (if (null list)
> (nreverse result)
> (if (and (cdr list) (eql (car list) (cadr list)))
> (xfilter (cdr list) result)
> (xfilter (cdr list) (cons (car list) result)))))

(define (xfilter lst)
(map car (g->list (g.chunks = lst))))

(xfilter '(1 2 3 3 3 4 4 3))
===>
'(1 2 3 4 3)


Given:

(define g.eof-object (with-input-from-string "" (lambda () (read))))

(define (span-comparing compare key the-list)
(cond ((null? the-list) (list '() '()))
((null? (cdr the-list)) (list the-list '()))
(else
(let go ((prev (key (car the-list)))
(xs (cdr the-list))
(accum (list (car the-list))))
(if (null? xs)
(list (reverse accum) xs)
(let ((x (key (car xs))))
(if (not (compare x prev))
(list (reverse accum) xs)
(go x (cdr xs) (cons (car xs) accum)))))))))

(define (g.chunks-by compare key the-list)
(let ((the-list the-list))
(lambda ()
(if (null? the-list)
g.eof-object
(let ((tmp (span-comparing compare key the-list)))
(set! the-list (cadr tmp))
(car tmp))))))

(define (g.chunks compare the-list)
(g.chunks-by compare values the-list))

(define (g->list g)
(let go ((accum '()))
(let ((x (g)))
(if (eof-object? x)
(reverse accum)
(go (cons x accum))))))

--
The report card by the American Society of Civil Engineers showed the national
infrastructure a single grade above failure, a step from declining to the point
where everyday things simply stop working the way people expect them to.
http://archive.org/details/nolies
0 new messages