[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

CLtL2, appendix B

William James

5/5/2015 4:50:00 AM

Barry Margolin wrote:

> The Generators and Collectors macros described in Appendix B
> of CLtL2 also provide this convenience and are much more
> Lisp-like. It's too bad they weren't around when LOOP was
> gaining popularity, and I think they're a better way to go.
> But LOOP is what I got used to, and its popularity is why it
> got elevated into the ANSI standard.

CLtL2, appendix B:

> (let ((x (generator (scan '(1 2 3 4)))))
> (with-output-to-string (s)
> (loop (prin1 (next-in x (return)) s)
> (prin1 (next-in x (return)) s)
> (princ "," s))))
> => "12,34,"

Gauche Scheme:

(use gauche.generator)

(let1 x (list->generator '(1 2 3 4))
(with-output-to-string (lambda ()
(do-generator (n x)
(display n)
(display (x))
(display ",")))))

===>
"12,34,"

--
In 1998 he was prosecuted, along with his publisher ... for "denying" the gas
chambers and the six million figure. In July 1998 a Swiss court sentenced him
to 15 months imprisonment, and to pay a large fine, because of his writings.
Rather than serve the sentence, in August 2000 Graf went into exile. In 2001 he
married a Russian historian in Moscow.
http://www.revisionists.com/revisionists...
2 Answers

William James

5/5/2015 8:37:00 AM

0

WJ wrote:

> Barry Margolin wrote:
>
> > The Generators and Collectors macros described in Appendix B
> > of CLtL2 also provide this convenience and are much more
> > Lisp-like. It's too bad they weren't around when LOOP was
> > gaining popularity, and I think they're a better way to go.
> > But LOOP is what I got used to, and its popularity is why it
> > got elevated into the ANSI standard.

CLtL2, appendix B:

> (let ((g (gatherer #'collect-sum)))
> (dolist (i '(1 2 3 4))
> (next-out g i)
> (if (evenp i) (next-out g (* 10 i))))
> (result-of g))
> => 70

Gauche Scheme:

(define (make-summer)
(let ((sum 0))
(case-lambda
(() sum)
((x) (set! sum (+ sum x)) sum)
((x . more) (set! sum (apply + sum x more)) sum))))

(let1 g (make-summer)
(dolist (i '(1 2 3 4))
(g i)
(when (even? i) (g (* 10 i))))
(g))

===>
70

Another way:

(let1 g (make-summer)
(dolist (i '(1 2 3 4))
(g i (if (even? i) (* 10 i) 0)))
(g))


> (defun examp (data)
> (gathering ((x collect) (y collect-sum))
> (iterate ((i (scan data)))
> (case (first i)
> (:slot (next-out x (second i)))
> (:part (dolist (j (second i)) (next-out x j))))
> (next-out y (third i)))))
>
> (examp '((:slot a 10) (:part (c d) 40))) => (a c d) and 50


(define (make-bagger)
(let ((bag '()))
(case-lambda
(() (reverse bag))
((x) (set! bag (cons x bag)) x)
((x what)
(case what
((append) (set! bag (append (reverse x) bag)) x)
((front cons) (set! bag (append bag (list x))) x)
((new) (if (member x bag)
#f
(begin (set! bag (cons x bag)) x)))
((sum) (set! bag (cons x bag)) (apply + bag))
(else (error "Bad args. to bagger:" x what)))))))

(define-syntax gathering
(syntax-rules ()
([_ ((name maker) ...) body ...]
(let ( (name (maker)) ... )
body ...
(values (name) ...)))))


(use srfi-1 :only (first second third))

(define (examp data)
(gathering ((x make-bagger) (y make-summer))
(dolist (i data)
(case (first i)
((:slot) (x (second i)))
((:part) (x (second i) 'append)))
(y (third i)))))

(examp '((:slot a 10) (:part (c d) 40)))
===>
(a c d)
50

--
I think that we have to remember that the real activists who are doing this are
motivated not by love for humanity, not by love for America or anything else;
they are motivated by hatred for the traditional people and culture of America.
They are motivated by hatred toward the traditional people and culture of the
European countries. --- www.redicecreations.com/radio/2014/09/RIR-140929.php

William James

12/1/2015 11:19:00 PM

0

WJ wrote:

> Barry Margolin wrote:
>
> > The Generators and Collectors macros described in Appendix B
> > of CLtL2 also provide this convenience and are much more
> > Lisp-like. It's too bad they weren't around when LOOP was
> > gaining popularity, and I think they're a better way to go.
> > But LOOP is what I got used to, and its popularity is why it
> > got elevated into the ANSI standard.
>
> CLtL2, appendix B:
>
> > (let ((x (generator (scan '(1 2 3 4)))))
> > (with-output-to-string (s)
> > (loop (prin1 (next-in x (return)) s)
> > (prin1 (next-in x (return)) s)
> > (princ "," s))))
> > => "12,34,"

Ocaml:

open Printf;;

let buf = Buffer.create 99 and
x = Stream.of_list [1;2;3;4;-5] in
(try
while true do
bprintf buf "%+d" (Stream.next x) ;
bprintf buf "%+d" (Stream.next x) ;
Buffer.add_string buf ", "
done
with Stream.Failure -> ()) ;
Buffer.contents buf ;;

"+1+2, +3+4, -5"

--
In the latter case, arrests are made, not so much for what has been done, as
for what probably would be done. The latter is more for the preventive, and
less for the vindictive, than the former. --- A. Lincoln, Letter to Erastus
Corning, June 12, 1863