newLISP Fan Club

Forum => newLISP in the real world => Topic started by: lyl on June 13, 2019, 11:50:32 PM

Title: How to partially evaluate elements in a hash-table
Post by: lyl on June 13, 2019, 11:50:32 PM
I construct a hash-table like this:
(setq mytable '(
     ((char "a") "a: choiceA" (+ 0 101))
     ((char "b") "b: choiceB" (+ 0 102))
     ))

In this example, I want the first element of each sublist to be evaluated while other elements not. That is to say, (char "a") should be 97, (char "b") should be 98.

Or, in common, how to  partially evaluate elements in a hash-table? Is there an easy to achieve this?
Title: Re: How to partially evaluate elements in a hash-table
Post by: cameyo on June 14, 2019, 12:14:20 AM
Try this:
(map (fn (x) (eval (first x))) mytable)
;-> (97 98)
Title: Re: How to partially evaluate elements in a hash-table
Post by: lyl on June 14, 2019, 12:42:10 AM
Sorry, I didn't make my question clear.

What I want is not the result of each first element evaluation. I want to get a easy way to construct "mytable" from
'(((char "a") "a: choiceA" (+ 0 101))  ((char "b") "b: choiceB" (+ 0 102)))
to
'((97 "a: choiceA" (+ 0 101)) (98 "b: choiceB" (+ 0 102)))
Title: Re: How to partially evaluate elements in a hash-table
Post by: cameyo on June 14, 2019, 10:16:42 AM
Maybe this:
(setq a '(((+ 6 2) (a) 2) ((- 2 5) (b) 5)))
;-> (((+ 6 2) (a) 2) ((- 2 5) (b) 5))
(dolist (el a)
 (setf (first (a $idx)) (eval (first el)))
)
a
;-> ((8 (a) 2) (-3 (b) 5))
Title: Re: How to partially evaluate elements in a hash-table
Post by: lyl on June 22, 2019, 07:06:27 PM
Yet, is there a better way to evaluate elements in hash-table when constructing hash-table, as like the comma expression in common lisp?
Title: Re: How to partially evaluate elements in a hash-table
Post by: al1ranger on June 29, 2019, 01:31:42 AM
This can be one way of doing it. The quote (//http) function can also be used if the list is being built at runtime.
(setq mytable (list (list (char "a") "a: choiceA" '(+ 0 101)) (list (char "b") "b: choiceB" '(+ 0 102))))