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

Re: Lisp for ANN programming

75 views
Skip to first unread message

WJ

unread,
Nov 15, 2012, 12:29:37 AM11/15/12
to
Coby Beck wrote:

> CL-USER 2 > (defvar *a* (make-array 10 :initial-element 1))
> *A*
>
> CL-USER 3 > (defun increment-every-third-elt (arr)
> (loop for elt across arr
> for i upfrom 1 do
> (when (= 0 (mod i 3))
> (incf (aref arr (1- i))))))
> INCREMENT-EVERY-THIRD-ELT
>
> CL-USER 4 > (increment-every-third-elt *a*)
> NIL
>
> CL-USER 5 > *a*
> #(1 1 2 1 1 2 1 1 2 1)

Clojure:

(def zeros (take 10 (repeat 0)))
(map-indexed #(if (zero? (mod (inc %1) 3)) (inc %2) %2) zeros)
--> (0 0 1 0 0 1 0 0 1 0)

Daniel Rupis

unread,
Nov 15, 2012, 5:25:33 AM11/15/12
to
WJ said


> (def zeros (take 10 (repeat 0)))
>
> (map-indexed #(if (zero? (mod (inc %1) 3)) (inc %2) %2) zeros)
>
> --> (0 0 1 0 0 1 0 0 1 0)

A trivial translation follows. Since it seems that you have plenty of time available for playing with clojure and common-lisp, what about developing a library that implements clojure in common lisp?

(map-indexed ($(i x) (if (zerop (mod (1+ i) 3)) (1+ x) x)) zeros)

given the following definitions:

(defun map-indexed (fun list)
(let ((i 0))
(mapcar (lambda(x) (prog1 (funcall fun i x) (incf i))) list)))

(defun repeat (times val)
(loop repeat times collect val))

(defparameter zeros (repeat 10 0))

(defmacro $ (vars &rest body)
`(lambda ,vars ,@body))

WJ

unread,
Jan 3, 2015, 1:15:48 AM1/3/15
to
WJ wrote:

> Coby Beck wrote:
>
> > CL-USER 2 > (defvar a (make-array 10 :initial-element 1))
> > A
> >
> > CL-USER 3 > (defun increment-every-third-elt (arr)
> > (loop for elt across arr
> > for i upfrom 1 do
> > (when (= 0 (mod i 3))
> > (incf (aref arr (1- i))))))
> > INCREMENT-EVERY-THIRD-ELT
> >
> > CL-USER 4 > (increment-every-third-elt a)
> > NIL
> >
> > CL-USER 5 > a
> > #(1 1 2 1 1 2 1 1 2 1)


Gauche Scheme:

(define a (make-list 10 2))

(map (^(x i) (if (zero? (mod i 3)) (+ x 1) x)) a (lrange 1))

===>
(2 2 3 2 2 3 2 2 3 2)

WJ

unread,
Dec 13, 2015, 12:06:01 PM12/13/15
to
MatzLisp (Ruby):

([500]*10).zip([0,0,1].cycle).map{|a,b| a+b}
==>[500, 500, 501, 500, 500, 501, 500, 500, 501, 500]

--
Elie [Wiesel] thus could have remained at Birkenau to await the Russians.
Although his father had permission to stay with him as a hospital patient or
orderly, father and son talked it over and decided to move out with the
Germans. --- Robert Faurisson
0 new messages