unexpected logic

Started by Dmi, December 22, 2005, 09:08:27 AM

Previous topic - Next topic

Dmi

Recently I wrote the function ecase, which works like "case", but evaluates it's key-arguments before testing.
(define-macro (ecase _v)
  (eval (append (list 'case _v)
(map (fn (_i) (set-nth 0 _i (eval (_i 0))))
    (args)))))

But when I start using it, I got strange result:
> (setq a 1 b 2)
2
> (ecase 2 (a 7) (b 8) (true 9))
8
> (ecase 3 (a 7) (b 8) (true 9))
nil
> (eval '(case 3 (1 7) (2 8) (true 9)))
9

Debug investigations shows that my list transformations are correct, but the eval doesn't work:
newLISP v.8.7.1 on Win32 MinGW, execute 'newlisp -h' for more info.

> (setq a 1 b 2)
2
> (debug (ecase 3 (a 7) (b 8) (true 9)))

-----

(define-macro (ecase _v)
  #(let (_e (append (list 'case _v) (map (lambda (_i) (set-nth 0 _i
        (eval (_i 0))))
      (args))))
   (eval _e))#)

(global 'ecase)


[-> 3 ] s|tep n|ext c|ont q|uit > s

-----

(define-macro (ecase _v)
  (let (_e #(append (list 'case _v) (map (lambda (_i) (set-nth 0 _i
        (eval (_i 0))))
      (args)))#)
   (eval _e)))

(global 'ecase)


[-> 4 ] s|tep n|ext c|ont q|uit > n

-----

(define-macro (ecase _v)
  (let (_e #(append (list 'case _v) (map (lambda (_i) (set-nth 0 _i
        (eval (_i 0))))
      (args)))#)
   (eval _e)))

(global 'ecase)


RESULT: (case 3
 (1 7)
 (2 8)
 (true 9))

[<- 4 ] s|tep n|ext c|ont q|uit > s

-----

(define-macro (ecase _v)
  (let (_e (append (list 'case _v) (map (lambda (_i) (set-nth 0 _i
        (eval (_i 0))))
      (args))))
   #(eval _e)#))

(global 'ecase)


[-> 4 ] s|tep n|ext c|ont q|uit > _e
(case 3
 (1 7)
 (2 8)
 (true 9))
[-> 4 ] s|tep n|ext c|ont q|uit > (eval _e)
nil
[-> 4 ] s|tep n|ext c|ont q|uit > (save "e.lsp" '_e)
true
[-> 4 ] s|tep n|ext c|ont q|uit > s

-----

(define-macro (ecase _v)
  (let (_e (append (list 'case _v) (map (lambda (_i) (set-nth 0 _i
        (eval (_i 0))))
      (args))))
   #(eval _e)#))

(global 'ecase)


RESULT: nil

[<- 4 ] s|tep n|ext c|ont q|uit > c
nil
> (load "e.lsp")
(case 3
 (1 7)
 (2 8)
 (true 9))
> _e
(case 3
 (1 7)
 (2 8)
 (true 9))
> (eval _e)
9
>


Where am I wrong?
WBR, Dmi