How to partially evaluate elements in a hash-table

Q&A's, tips, howto's

How to partially evaluate elements in a hash-table

Postby lyl » Fri Jun 14, 2019 6:50 am

I construct a hash-table like this:
Code: Select all
(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?
lyl
 
Posts: 33
Joined: Sun Mar 25, 2018 5:00 am

Re: How to partially evaluate elements in a hash-table

Postby cameyo » Fri Jun 14, 2019 7:14 am

Try this:
Code: Select all
(map (fn (x) (eval (first x))) mytable)
;-> (97 98)
cameyo
 
Posts: 83
Joined: Sun Mar 27, 2011 3:07 pm

Re: How to partially evaluate elements in a hash-table

Postby lyl » Fri Jun 14, 2019 7:42 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
Code: Select all
'(((char "a") "a: choiceA" (+ 0 101))  ((char "b") "b: choiceB" (+ 0 102)))

to
Code: Select all
'((97 "a: choiceA" (+ 0 101)) (98 "b: choiceB" (+ 0 102)))
lyl
 
Posts: 33
Joined: Sun Mar 25, 2018 5:00 am

Re: How to partially evaluate elements in a hash-table

Postby cameyo » Fri Jun 14, 2019 5:16 pm

Maybe this:
Code: Select all
(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))
cameyo
 
Posts: 83
Joined: Sun Mar 27, 2011 3:07 pm

Re: How to partially evaluate elements in a hash-table

Postby lyl » Sun Jun 23, 2019 2:06 am

Yet, is there a better way to evaluate elements in hash-table when constructing hash-table, as like the comma expression in common lisp?
lyl
 
Posts: 33
Joined: Sun Mar 25, 2018 5:00 am

Re: How to partially evaluate elements in a hash-table

Postby al1ranger » Sat Jun 29, 2019 8:31 am

This can be one way of doing it. The quote function can also be used if the list is being built at runtime.
Code: Select all
(setq mytable (list (list (char "a") "a: choiceA" '(+ 0 101)) (list (char "b") "b: choiceB" '(+ 0 102))))
al1ranger
 
Posts: 1
Joined: Sat Jun 29, 2019 8:05 am


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 2 guests

cron