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

ACL chpater 3, exercise 5

68 views
Skip to first unread message

arnuld

unread,
Jan 9, 2012, 6:16:51 AM1/9/12
to
I don't understand whats wrong. Error looks cryptic to me :-\

;; ACL Paul Graham, Chpater 3 Exercise 5
;; Supppose the funtion pos+ takes a list and returns a list of
;; "each element + its position"
;; (pos+ '(7 5 1 4)) ==> (7 6 3 7)
;; define this function using (a) recursion (b) iteration (c) mapcar


(defun pos+ (x)
(mapcar #'(lambda (let ((idx 0))
(+ x idx)
(+ idx 1))) x))


================== OUTPUT ==========================

[34]> (load "ACL-ch-3-exr-5.lisp")
;; Loading file ACL-ch-3-exr-5.lisp ...
;; Loaded file ACL-ch-3-exr-5.lisp
T
[35]> (pos+ '(1 2))

*** - APPLY: argument list given to SYSTEM::ERROR-OF-TYPE is dotted
(terminated by
"Invalid specialized parameter in method lambda list (LET ((IDX 0))
(+ X IDX) (+ IDX 1)): ((IDX 0))")
The following restarts are available:
ABORT :R1 Abort main loop
Break 1 [36]>

--
arnuld
http://LispMachine.Wordpress.com

Espen Vestre

unread,
Jan 9, 2012, 6:38:54 AM1/9/12
to
arnuld <sun...@invalid.address> writes:

> I don't understand whats wrong. Error looks cryptic to me :-\

The error message is cryptic because your error is so unexpected :-)

Your mapcar lacks a list parameter, but the most confusing (to your
lisp) error is that your lambda expression lacks a parameter list:

> (defun pos+ (x)
> (mapcar #'(lambda (let ((idx 0))
> (+ x idx)
> (+ idx 1))) x))

Your solution reveals that you may not be quite up to this level of
exercise yet, I think you need to first understand that + is a function
that doesn't have side effects, i.e. it doesn't change the value of x
(or idx).
--
(espen)

Tamas Papp

unread,
Jan 9, 2012, 6:38:58 AM1/9/12
to
On Mon, 09 Jan 2012 11:16:51 +0000, arnuld wrote:

> I don't understand whats wrong. Error looks cryptic to me :-\
>
> ;; ACL Paul Graham, Chpater 3 Exercise 5 ;; Supppose the funtion pos+
> takes a list and returns a list of ;; "each element + its position"
> ;; (pos+ '(7 5 1 4)) ==> (7 6 3 7)
> ;; define this function using (a) recursion (b) iteration (c) mapcar
>
>
> (defun pos+ (x)
> (mapcar #'(lambda (let ((idx 0))
> (+ x idx)
> (+ idx 1))) x))

That lambda form is invalid -- (let ((idx 0)) ...) is not a valid
lambda list. If you are trying to use a let over lambda, so something
like

(defun pos+ (x)
(mapcar (let ((idx 0))
(lambda (x)
(prog1 (+ x idx)
(incf idx))))
x))

Notice that you also need to increment idx, simply adding 1 to it is
not enough.

Best,

Tamas

WJ

unread,
Feb 16, 2012, 10:10:11 PM2/16/12
to
MatzLisp:

add_pos = lambda{|x| x.each_with_index.map{|n,i| n+i}}

add_pos[ [4,5,6,7] ]

==>[4, 6, 8, 10]

Tim Bradshaw

unread,
Feb 17, 2012, 3:26:35 AM2/17/12
to
On 2012-02-17 03:10:11 +0000, WJ said:

> MatzLisp:

Could we have FORTRAN as well?

Kaz Kylheku

unread,
Feb 17, 2012, 3:44:08 AM2/17/12
to
On 2012-02-17, WJ <w_a_...@yahoo.com> wrote:
> MatzLisp:
>
> add_pos = lambda{|x| x.each_with_index.map{|n,i| n+i}}
>
> add_pos[ [4,5,6,7] ]
>
> ==>[4, 6, 8, 10]

each with index?

What an imbecillic turd of a kludge. R.O.M.F.A.O.

TXR Lisp:

[mapcar + '(4 5 6 7) (range 0)]

WJ

unread,
Mar 6, 2012, 3:55:53 AM3/6/12
to
NewLisp:

> (define (add-pos lst) (map (fn (n) (+ n $idx)) lst))
(lambda (lst) (map (lambda (n) (+ n $idx)) lst))
> (add-pos '(4 5 6 7))
(4 6 8 10)
0 new messages