[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

the code of Edi book is (free) available online at http://weitz.de/cl-recipes/code.zip

TwirlwT

1/2/2016 1:24:00 PM


Thanks to Edi (Edmuntz Weitz) for providing the code of his book: Common Lisp Recipes. In the webpage he welcomes people reviewing his book or blogging about it.
The code is available at http://weitz.de/cl-recipe...


23 Answers

William James

1/2/2016 8:21:00 PM

0

TwirlwT wrote:

> Thanks to Edi (Edmuntz Weitz)
> for providing the code of his
> book: Common Lisp Recipes. In
> the webpage he welcomes people
> reviewing his book or blogging
> about it.
>
> The code is available at
> http://weitz.de/cl-recipe...

I broke your lines for you.

Anyone who knows anything whatsoever about Usenet knows
that he must limit the length of his lines.


Chapter 1.

(defmacro swap (var-1 var-2)
(let ((temp-var (gensym))) ;; <- added
`(let ((,temp-var ,var-1)) ;; <- changed
(setf ,var-1 ,var-2
,var-2 ,temp-var) ;; <- changed
(values))))


MatzLisp (Ruby):

a,b = b,a

-----

(let (result)
(do-all-symbols (s)
(when (member s '("VECTOR-ADD" "VECTOR-MULT") :test 'string=)
(pushnew s result)))
result)


MatzLisp (Ruby):

%w(a foo b bar baz) & %w(m bar n foo zoo)
==>["foo", "bar"]


-----

(defpackage :vec
(:use :cl)
(:shadow :vector :+))
(in-package :vec)
(defclass vector ()
((x :initarg :x :reader x)
(y :initarg :y :reader y)))
(defgeneric + (arg &rest other-args)
(:method ((arg number) &rest other-args)
(apply 'cl:+ arg other-args)))
(defmethod + ((arg vector) &rest other-args)
(make-instance 'vector
:x (apply 'cl:+ (x arg) (mapcar 'x other-args))
:y (apply 'cl:+ (y arg) (mapcar 'y other-args))))

(+ (make-instance 'vector :x 3 :y 4)
(make-instance 'vector :x 5 :y 6)
(make-instance 'vector :x 7 :y 8))


MatzLisp (Ruby):

class Array
def add( *rest )
self.zip(*rest).map{|xs| xs.reduce(:+)}
end
end

[2,3,4,5].add
==>[2, 3, 4, 5]

[2,3,4,5].add [3,4,5,6]
==>[5, 7, 9, 11]

[2,3,4,5].add([3,4,5,6], [4,5,6,7])
==>[9, 12, 15, 18]

--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunk...
https://www.youtube.com/watch?v=E...

Polos Ruetz

1/2/2016 10:12:00 PM

0

> The code is available at http://weitz.de/cl-recipe...

You can actually just download the code (each chapter seems to have a file) and work through the examples in Slime.

It seems enough for a poor man's learning experience (like the commenter), lacking all of: time, money and a book shelf.

Jon Atack

1/2/2016 10:25:00 PM

0

On principle alone, I reckon anyone who writes a Lisp book, not to mention a real book of this size and effort, considering the size of the community, shows dedication that is deserving of support.

As well, the author has written several good open source libraries that help everyone. If you use to want to support Lisp, please buy the book if you can.

The same with QuickLisp. If you use it, set up a small monthly donation with PayPal. Support the people who support Lisp :)

Jon Atack

1/2/2016 10:29:00 PM

0

On principle alone, I reckon anyone who writes a Lisp book, not to mention a
real book of this size and effort, considering the size of the community, shows
dedication that is deserving of support.

As well, the author has written several good open source libraries that help
everyone. If you use Lisp or would like to contribute/help support the community, please buy the book if you can.

The same with QuickLisp. If you use it, set up a small monthly donation with
PayPal. Support the people who support Lisp :)

William James

1/2/2016 11:20:00 PM

0

TwirlwT wrote:

> Thanks to Edi (Edmuntz Weitz)
> for providing the code of his
> book: Common Lisp Recipes. In
> the webpage he welcomes people
> reviewing his book or blogging
> about it.
>
> The code is available at
> http://weitz.de/cl-recipe...

I broke your lines for you.

Anyone who knows anything whatsoever about Usenet knows
that he must limit the length of his lines.


Chapter 2.

(defun integer-to-bit-list (x)
(check-type x (integer 0 *))
(let (result)
(loop (when (zerop x)
(return (nreverse result)))
(push (logand x 1) result)
(setf x (ash x -1)))))

MatzLisp (Ruby):

def int_to_bit_list n
result = []
until n.zero?
result << (n & 1)
n >>= 1
end
result
end

int_to_bit_list 7
==>[1, 1, 1]
int_to_bit_list 6
==>[0, 1, 1]
int_to_bit_list 5
==>[1, 0, 1]
int_to_bit_list 0
==>[]


-----

(defparameter *m*
'((11 12 13 14)
(21 22 23 24)
(31 32 33 34)))
(let ((*print-right-margin* 20))
(pprint (apply 'mapcar 'list *m*)))

[ The output is: ]

((11 21 31)
(12 22 32)
(13 23 33)
(14 24 34))

MatzLisp (Ruby):

m = [[11,12,13,14],
[21,22,23,24],
[31,32,33,34]]

m.transpose
===>
[[11, 21, 31], [12, 22, 32], [13, 23, 33], [14, 24, 34]]


-----

(let ((b (list 23 42)))
`(a ,@b c))

[ The output is: ]

(A 23 42 C)

MatzLisp (Ruby):

b = [23,42]
[:a, *b, :c]
===>
[:a, 23, 42, :c]


-----

(flet ((foo (x)
`(,x b c)))
(let ((a (foo 23))
(b (foo 42)))
(list a b (eq a b) (eq (cdr a) (cdr b)))))

[ The output is: ]

((23 B C) (42 B C) NIL T)

MatzLisp (Ruby):

foo = lambda{|x| [x, :b, :c]}
a = foo[23]
b = foo[42]
[a, b, a==b, a.drop(1) == b.drop(1)]
===>
[[23, :b, :c], [42, :b, :c], false, true]


-----

(loop for i below 10 collect (* i i))

[ Output: ]

(0 1 4 9 16 25 36 49 64 81)

MatzLisp (Ruby):

Array.new(10){|i| i*i}
===>
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Another way:

(0..9).map{|i| i*i}

-----

(defun splice (list &key (start 0) (end (length list)) new)
(setf list (cons nil list)) ;; add dummy cell
(let ((reroute-start (nthcdr start list)))
(setf (cdr reroute-start)
(nconc (make-list (length new)) ;; empty cons cells
(nthcdr (- end start) ;; tail of old list
(cdr reroute-start)))
list (cdr list))) ;; remove dummy cell
(replace list new :start1 start) ;; fill empty cells
list)

(defparameter *list* (list 'a 'b 'c 'd 'e))
(defparameter *new* (list 'x 'y 'z))
(setf *list* (splice *list* :start 1 :end 3 :new *new*))

[ Output: ]

(A X Y Z D E)


MatzLisp (Ruby):

list = %w(a b c d e)
new = %w(x y z)
list[1,2] = new
list
===>
["a", "x", "y", "z", "d", "e"]

list[1,3] = []
list
===>["a", "d", "e"]

list[1,0] = 42
list
===>["a", 42, "d", "e"]

list << 88
===>["a", 42, "d", "e", 88]


-----

(destructuring-bind (a (b &rest c) (d (e . f)))
'("A" (:b 2 3) (#\D (1.0 . 3.0)))
(list a b c d e f))

[ Output: ]

("A" :B (2 3) #\D 1.0 3.0)


MatzLisp (Ruby):

a,(b,*c),(d,(e,f)) = ["A", [:b,2,3], ['D',[1.0, 3.0]]]

[a,b,c,d,e,f]
===>
["A", :b, [2, 3], "D", 1.0, 3.0]


-----

(destructuring-bind (&key a (b :not-found) c &allow-other-keys)
'(:c 23 :d "D" :a #\A :foo :whatever)
(list a b c))

[ Output: ]
(#\A :NOT-FOUND 23)


MatzLisp (Ruby):

[:a,:b,:c].map{|k|
Hash[:c,23,:d,"D",:a,'A',:foo,:whatever].fetch(k,:not_found)}
===>
["A", :not_found, 23]

--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunk...
https://www.youtube.com/watch?v=E...

TwirlwT

1/3/2016 12:30:00 AM

0

El sábado, 2 de enero de 2016, 14:24:32 (UTC+1), TwirlwT escribió:
> Thanks to Edi (Edmuntz Weitz) for providing the code of his book: Common Lisp Recipes. In the webpage he welcomes people reviewing his book or blogging about it.
> The code is available at http://weitz.de/cl-recipe...

Obviously, the code in the book is not the shorter possible.
The goal of the author is to teach the language.

For example, this code

(defun integer-to-bit-list (x)
(check-type x (integer 0 *))
(let (result)
(loop (when (zerop x)
(return (nreverse result)))
(push (logand x 1) result)
(setf x (ash x -1)))))

could be shortened like this:

(defun integer-to-bit-list(x)
(check-type x (integer 0 *))
(map 'list #'digit-char-p (format nil "~B" x)))

Also the swap macro of chapter one be replaced by

(defmacro swap(a b)
`(psetq ,a ,b ,b ,a))

And many other examples. (trying not to feed the tail recursive troll).

Pascal Costanza

1/3/2016 3:29:00 AM

0

On 03/01/2016 01:29, TwirlwT wrote:
> El sábado, 2 de enero de 2016, 14:24:32 (UTC+1), TwirlwT escribió:
>> Thanks to Edi (Edmuntz Weitz) for providing the code of his book: Common Lisp Recipes. In the webpage he welcomes people reviewing his book or blogging about it.
>> The code is available at http://weitz.de/cl-recipe...
>
> Obviously, the code in the book is not the shorter possible.
> The goal of the author is to teach the language.
>
> For example, this code
>
> (defun integer-to-bit-list (x)
> (check-type x (integer 0 *))
> (let (result)
> (loop (when (zerop x)
> (return (nreverse result)))
> (push (logand x 1) result)
> (setf x (ash x -1)))))
>
> could be shortened like this:
>
> (defun integer-to-bit-list(x)
> (check-type x (integer 0 *))
> (map 'list #'digit-char-p (format nil "~B" x)))

Could be, but would be horribly inefficient.

> Also the swap macro of chapter one be replaced by
>
> (defmacro swap(a b)
> `(psetq ,a ,b ,b ,a))

Common Lisp already has a macro built in that can do this, without the
need to define one yourself. I don't know the book yet, but this
suggests to me that defining a swap macro was not the primary goal here.


Pascal

--
My website: http:/...
Common Lisp Document Repository: http://cdr.eu...
Closer to MOP & ContextL: http://common-lisp.net/proje...
The views expressed are my own, and not those of my employer.

William James

1/3/2016 9:09:00 AM

0

TwirlwT wrote:

> Thanks to Edi (Edmuntz Weitz)
> for providing the code of his
> book: Common Lisp Recipes. In
> the webpage he welcomes people
> reviewing his book or blogging
> about it.
>
> The code is available at
> http://weitz.de/cl-recipe...

Chapter 3.

;; assumes something like ASCII - see footnote
;; see https://en.wikipedia.org/...
(defun rot13-char (char)
(cond ((char<= #\a char #\z)
(code-char (+ (mod (+ (- (char-code char) (char-code #\a))
13)
26)
(char-code #\a))))
((char<= #\A char #\Z)
(code-char (+ (mod (+ (- (char-code char) (char-code #\A))
13)
26)
(char-code #\A))))))

(defun n-rot13-string (string)
(loop for i below (length string)
do (setf (char string i)
(rot13-char (char string i)))))
(defparameter *string* (copy-seq "foobar"))
(n-rot13-string *string*)
*string*



[ ----- Testing: ----- ]

(defparameter *string* (copy-seq "Foobar"))
(n-rot13-string *string*)
*string*

===>
"Sbbone"

(defparameter *string* (copy-seq "Foo bar."))
(n-rot13-string *string*)

Debugger invoked on condition of type TYPE-ERROR:
The value NIL is not of type CHARACTER.

[ ----- It failed the test. ----- ]


MatzLisp (Ruby):

def rot13 s
table = Hash[*['a'..'z', 'A'..'Z'].map{|r|
a = r.to_a; a.zip(a.rotate 13)}.flatten]
s.each_char.map{|c| table.fetch(c, c)}.join
end

rot13 "PY vf gur zbfg cvgvshy bs ynathntrf."
===>
"CL is the most pitiful of languages."

--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunk...
https://www.youtube.com/watch?v=E...

Pascal J. Bourguignon

1/3/2016 9:35:00 AM

0

TwirlwT <danieltorridoverde@gmail.com> writes:

> Also the swap macro of chapter one be replaced by
>
> (defmacro swap(a b)
> `(psetq ,a ,b ,b ,a))

This swap is very defective.

I don't have the macro of chapter one, but it would have to be at least
this complex:

(defmacro swap (a b &environment env)
(multiple-value-bind (avars avals astore-vars asetter agetter)
(get-setf-expansion a env)
(when (cdr astore-vars)
(error "Can't expand ~S" a))
(multiple-value-bind (bvars bvals bstore-vars bsetter bgetter)
(get-setf-expansion b env)
(when (cdr astore-vars)
(error "Can't expand ~S" b))
(let ((atemp (car astore-vars))
(btemp (car bstore-vars))
(temp (gensym)))
;; ensure a and b subepressions (vals) are evaluated in order, once:
`(let* (,@(mapcar #'list avars avals)
(,btemp ,agetter)
,@(mapcar #'list bvars bvals)
(,atemp ,bgetter))
,asetter
,bsetter)))))

(pprint (macroexpand-1 '(swap (aref v (incf i)) (aref v (incf i)))))
--> (let* ((#1=#:g53144 v)
(#2=#:g53145 (incf i))
(#6=#:g53146 (aref #1# #2#))
(#3=#:g53147 v)
(#4=#:g53148 (incf i))
(#5=#:g53143 (aref #3# #4#)))
(ccl::aset #1# #2# #5#)
(ccl::aset #3# #4# #6#))

(let ((v (vector 1 2 3 4 5))
(i 0))
(swap (aref v (incf i)) (aref v (incf i)))
(values v i))
--> #(1 3 2 4 5)
2

(pprint (macroexpand-1 '(swap a b)))
--> (let* ((#2=#:g53131 a)
(#1=#:g53130 b))
(setq a #1#)
(setq b #2#))


(of course, a CL-isper would just use ROTATEF).

--
__Pascal Bourguignon__

Croire en l'histoire officielle, c'est croire des criminels sur parole.
-- Simone WEIL (1909-1943) philosophe Française.

William James

1/4/2016 12:38:00 AM

0

TwirlwT wrote:

> Thanks to Edi (Edmuntz Weitz)
> for providing the code of his
> book: Common Lisp Recipes. In
> the webpage he welcomes people
> reviewing his book or blogging
> about it.
>
> The code is available at
> http://weitz.de/cl-recipe...

Chapter 4.

;; see <http://en.wikipedia.org/wiki/Fermat_...
(defun fermat (n) (1+ (expt 2 (expt 2 n))))
(fermat 7)
(fermat 8)


MatzLisp (Ruby):

def fermat n
2**2**n + 1
end

fermat 7
===>
340282366920938463463374607431768211457

fermat 8
===>
115792089237316195423570985008687907853269984665640564039457584007913129639937


-----

(defun parse-integers (string)
(let ((start 0)
(end (length string))
(result '()))
(loop
(when (>= start end)
(return (nreverse result)))
(multiple-value-bind (number pos)
(parse-integer string :start start :junk-allowed t)
(cond (number
(push number result)
(setf start pos))
(t (setf start end)))))))

[ Testing: ]

CL-USER(2): (parse-integers " 22 88 -9 ")
(22 88 -9)
CL-USER(3): (parse-integers " 22 88- -9 ")
(22 88)
CL-USER(4): (parse-integers " 22 88.3 -9 ")
(22 88)
CL-USER(4): (parse-integers " 22bar88 -9 ")
(22)
CL-USER(5): (parse-integers "xxx 22bar88 -9 ")
NIL


MatzLisp (Ruby):

def parse_integers s
s.scan( /[-+]?\d+/ ).map(&:to_i)
end

parse_integers " 22 88 -9 "
==>[22, 88, -9]
parse_integers " 22 88- -9 "
==>[22, 88, -9]
parse_integers " 22 88.3 -9 "
==>[22, 88, 3, -9]
parse_integers " +22 88 -9 "
==>[22, 88, -9]
parse_integers "xxx +22bar88foo-9 "
==>[22, 88, -9]


-----

(loop for radix in '(2 8 10 16)
collect (parse-integer "111" :radix radix))


[ Result: ]

(7 73 111 273)


If he had had any affinity whatsoever for Lispy programming, he would
have done it this way:

(mapcar (lambda (radix) (parse-integer "111" :radix radix))
'(2 8 10 16))

Worshippers of CL (COBOL-Like) and LOOP never have any affinity
whatsoever for Lispy programming.


MatzLisp (Ruby):

[2,8,10,16].map{|radix| "111".to_i(radix)}
==>[7, 73, 111, 273]

--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunk...
https://www.youtube.com/watch?v=E...