Author Topic: A simple question (I hope!)  (Read 1648 times)

0 Members and 1 Guest are viewing this topic.

Jeremy

  • Guest
A simple question (I hope!)
« on: June 17, 2014, 03:52:19 PM »
Suppose I want to write a function MYSETQ. MYSETQ takes a list of items to input into a SETQ statement and sets them. For instance,

(MYSETQ (list a 2 b 5))  should act the same as (setq a 2 b 5).

At first I thought

(defun MYSETQ ( lst )(apply 'setq lst))

Nope! It works if you write (MYSETQ (list 'a 2 'b 5)). You have to quote the items. But I don't want to! I've tried quoting, using eval and combinations of functions within the function to no avail. Is there any way to quote the items after you have input them rather than before? Every time I try to access the elements of the list  the a and b are simply nil and I can't do a thing with them, what's a poor schmuck to do?

ymg

  • Guest
Re: A simple question (I hope!)
« Reply #1 on: June 17, 2014, 04:17:02 PM »
Jeremy

Look at eval to do such a thing

Here an example:

Code: [Select]
(setq lst '((a 2) (b 8) (c 10)))

(defun mysetq (l)
  (setq l (mapcar (function (lambda (a) (list 'setq (car a) (cadr a)))) l))
  (mapcar 'eval l)


If you run:

Quote
_$ (mysetq lst)
(2 8 10)
_$ a
2
_$ b
8
_$ c
10
_$

ymg
« Last Edit: June 17, 2014, 04:53:08 PM by ymg »

reltro

  • Guest
Re: A simple question (I hope!)
« Reply #2 on: June 17, 2014, 04:23:14 PM »
Hey Jeremy...

setq is a macro and can't be immitated in pure lisp... like defun, foreach and lot others...

I don't know how u build up the Input-list for MySetq but u may can give the symbols as strings?

Code: [Select]
(defun MySetq (lst / )
   (if (= (rem (length lst) 2) 0)
       (while lst
           (set (read (car lst)) (cadr lst))
           (setq lst (cddr lst))
       )
       (alert "Syntax error")
   )
)

Code: [Select]
(MySetq (list "a" 2 "b" 5))

greets reltro
« Last Edit: June 17, 2014, 04:47:14 PM by reltro »

PKENEWELL

  • Bull Frog
  • Posts: 308
Re: A simple question (I hope!)
« Reply #3 on: June 17, 2014, 04:24:14 PM »
Here is a pull from my library:

Code - Auto/Visual Lisp: [Select]
  1. ;|==============================================================================
  2.   Function Name: (pjk-dynsetvar) By Phil Kenewell
  3.   Arguments:
  4.              assoclst = List of cons; an assoc list on the format:
  5.                         (list (cons "<var name1>" <value1>) ... (cons "<Var namN>" <valueN>))
  6.  
  7.   Usage: (pjk-dynsetvar <Assoc. List>)
  8.          Ex: (pjk-dynsetvar (list (cons "Cenpt" (list 1.0 1.0 0.0)) (cons "Radius" 0.25)))
  9.   Returns:
  10.      The results of the read list. You can verify the variables with the ! prefix:
  11.  
  12.      Command line Example:
  13.  
  14.      Command: (pjk-dynsetvar (list (cons "Cenpt" (list 1.0 1.0 0.0)) (cons "Radius" 0.25)))
  15.      ((1.0 1.0 0.0) 0.25)
  16.  
  17.      Command: !Cenpt
  18.      (1.0 1.0 0.0)
  19.  
  20.      Command: !Radius
  21.      0.25
  22.  
  23.    Description:
  24.       This function dynamically sets variables based on an association list of strings
  25.       and values. The string keys represent the names of the variables that the values
  26.       will be assigned to.
  27. ================================================================================|;
  28. (defun pjk-dynsetvar (assoclst)
  29.    (mapcar '(lambda (x)(set (read (car x)) (cdr x))) assoclst)
  30. );; End Function (pjk-dynsetvar)
  31.  
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

Lee Mac

  • Seagull
  • Posts: 12892
  • London, England
Re: A simple question (I hope!)
« Reply #4 on: June 17, 2014, 05:04:00 PM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun mysetq ( l )
  2.     (and l (set (car l) (cadr l)) (mysetq (cddr l)))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (mysetq '(a 2 b 5))
  2. nil
  3. _$ a
  4. 2
  5. _$ b
  6. 5
However, note that any function which bounds values to symbols passed as arguments will yield unexpected results if a supplied symbol matches those used by arguments/variables of the function, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. _$ (mysetq '(l 2))
  2. ; error: bad argument type: consp 2

Alternatively:
Code - Auto/Visual Lisp: [Select]
  1. (defun mysetq ( l ) (eval (cons 'setq l)))

The supplied symbols (but not necessarily the values) will always need to be quoted in the supplied list, else the list function will evaluate the symbols to yield their values (null or otherwise) - see here for more.
« Last Edit: June 17, 2014, 05:10:08 PM by Lee Mac »