[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Macros for seriously interested people

William James

11/15/2015 3:44:00 PM

Joe Marshall wrote:

> I gave two examples of interesting macro expansion. One from series:
>
> (defun example ()
> (let ((elements '(3 -1 4 -1 5 -9 2 -6 5 -3)))
> (/ (reduce #'max (map 'list #'abs elements))
> (reduce #'+ elements))))
>
> This works, but it is inefficient because MAP and the second REDUCE
> both traverse the list, and the intermediate list generated by MAP is
> immediately discarded (consumed) by the first REDUCE.
>
> The series version appears quite similar:
>
> (defun series-example ()
> (let ((elements (scan '(3 -1 4 -1 5 -9 2 -6 5 -3))))
> (/ (collect-max (#m abs elements))
> (collect-sum elements))))
>
> but if you macroexpand the body, you get this:

MatzLisp (Ruby):

m=s=0; [3,-1,4,-1,5,-9,2,-6,5,-3].each{|n| m=[m,n.abs].max; s+=n}; m.fdiv s
==>-9.0


> This example is from Screamer:
> (and seems relevant to a different thread on this list!)
>
> (defun pythagorean-triples (n)
> (all-values
> (let ((a (an-integer-between 1 n))
> (b (an-integer-between 1 n))
> (c (an-integer-between 1 n)))
> (unless (= (+ (* a a) (* b b)) (* c c)) (fail))
> (list a b c))))

def pyth_triples n
result = []
r = 1..n
r.each{|a|
r.each{|b|
r.each{|c|
result << [a,b,c] if c*c == a*a + b*b}}}
result
end

pyth_triples 22
==>[[3, 4, 5], [4, 3, 5], [5, 12, 13], [6, 8, 10], [8, 6, 10],
[8, 15, 17], [9, 12, 15], [12, 5, 13], [12, 9, 15], [12, 16, 20],
[15, 8, 17], [16, 12, 20]]

--
The pattern is the same. The gangs are almost always operated by Dutch-born
Moroccan and Turkish men. The men give their victims alcohol and drugs, use
lies to separate them from their families, gang rape them (often taking photos
for blackmail purposes), and then place the girls in prostitution.... Some of
these girls are as young as 11 or 12 years old.
http://www.liveleak.com/view?i=807_...