[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Debugger features

William James

3/12/2016 6:42:00 PM

Alan Crowe wrote:

> Macros let you abstract control structures. For example,
> with a list (a b c d) you might want to compute
> (f a b),(f b c),(f c d)
> Another popular pattern is
> (f a b),(f b c),(f c d)(f d a)
> It is natural to define macros
>
> * (dolinks (x y '(a b c d ) 'done)(print (cons x y)))
> (A . B)
> (B . C)
> (C . D)
> DONE
>
> * (docycle (x y '(a b c d ) 'done)(print (cons x y)))
> (A . B)
> (B . C)
> (C . D)
> (D . A)
> DONE
>
> The alternative is to come up with cliches and use them
> repeatedly in ones code. I've not done too well at coming up
> with cliches taught enough to bear repeated typing; best so
> far:
>
> * (loop with list = '(a b c d)
> for x = (car list) then y
> and y in (cdr list)
> do (print (cons x y)))
> (A . B)
> (B . C)
> (C . D)
> NIL


MatzLisp (Ruby):

[:a,:b,:c,:d].each_cons(2){|x| p x}
[:a, :b]
[:b, :c]
[:c, :d]
==>nil


>
> * (loop for (x . rest) on '(a b c d)
> for y = (car rest)
> when (null rest) do (setf y 'a)
> do (print (cons x y)))
> (A . B)
> (B . C)
> (C . D)
> (D . A)
> NIL
>
> Obviously, mistakes in the cliches result in bugs, and
> writing a macro eliminates the bugs by design. On the other
> hand my macros for dolinks and docycles are fairly chunky.
>
> (defmacro with-gensyms((&rest syms) &body code)
> `(let ,(mapcar (lambda(sym)(list sym '(gensym)))
> syms)
> ,@code))
>
> (defmacro dolinks ((a b list &optional value) &body code)
> (with-gensyms(evaluated-list tail)
> `(let ((,evaluated-list ,list))
> (do ((,a (car ,evaluated-list) ,b)
> (,b (cadr ,evaluated-list) (cadr ,tail))
> (,tail (cdr ,evaluated-list)
> (cdr ,tail)))
> ((endp ,tail) ,value)
> ,@code))))
>
> (defmacro docycle((a b list &optional value) &body code)
> (with-gensyms (evaluated-list counting-list working-list)
> `(let ((,evaluated-list ,list))
> (do ((,counting-list ,evaluated-list
> (cdr ,counting-list))
> (,working-list (cddr ,evaluated-list)
> (cdr ,working-list))
> (,a (car ,evaluated-list) ,b)
> (,b (if (cdr ,evaluated-list)
> (cadr ,evaluated-list)
> (car ,evaluated-list))
> (if ,working-list
> (car ,working-list)
> (car ,evaluated-list))))
> ((endp ,counting-list) ,value)
> ,@code))))

OCaml:

let rec do_links f = function
[] | [_] -> ()
| x::y::more -> f x y; do_links f (y::more) ;;

do_links (Printf.printf "%s %s\n") ["a";"b";"c";"d"];;
===>
a b
b c
c d

let do_cycle f list =
let rec loop = function
[] -> ()
| [z] -> f z (List.hd list)
| x::y::more -> f x y; loop (y::more)
in
loop list ;;

do_cycle (Printf.printf "%s %s\n") ["a";"b";"c";"d"];;
===>
a b
b c
c d
d a


--
Blacks are an estimated 39 times more likely to commit a violent crime
against a white than vice versa, and 136 times more likely to commit robbery.
www.colorofcrime.com/2005/10/the-color-of-crime-2005/