Author Topic: ByRef in lisp?  (Read 2679 times)

0 Members and 1 Guest are viewing this topic.

mkweaver

  • Bull Frog
  • Posts: 352
ByRef in lisp?
« on: October 03, 2007, 08:47:40 AM »
I know I must be missing something easy here:

Given two lists of data:
(setq List1 '(1 2 3 4 5))
(1 2 3 4 5)
(setq list2 '(a b c d e))
(A B C D E)

And a defun:
(defun test(Data)
  (subst (car datalist) "12" datalist)
 )

How can I change the defun, or specify the argument when calling the function, so that the function modifies the original(global) datalist rather than the local copy?  I know I could leave the argument list global, but I want to be able to work on either datalist with the same defun.

I've tried the following to no avail:

(test 'list1)
nil
list1
(1 2 3 4 5)

and

(test list1)
nil
list1
(1 2 3 4 5)

I think I must be missing something simple/basic here.

Suggestions are most welcome.

Mike Weaver
« Last Edit: October 03, 2007, 08:51:15 AM by mkweaver »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ByRef in lisp?
« Reply #1 on: October 03, 2007, 08:56:41 AM »
I just woke up, so common sense is not preventing me from posting this quick and dirty --

Code: [Select]
(defun ByRef ( quotedVar / value )

    ;;  minimalist coding, no error checking (read "not good for production code")

    (setq value (eval quotedVar))

    (set quotedVar
   
        (subst "woot" (car value) value)       
   
    )

)

Example:

Code: [Select]
(setq lst '( "phee" "phi" "pho"))

(ByRef 'lst)

(princ lst)

Having posted it let me say there are far better strategies for achieving what you want but I don't have the time to detail right now. I'm sure the thread will flourish in my absence. Cheers.

:)
« Last Edit: October 03, 2007, 09:23:50 AM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

mkweaver

  • Bull Frog
  • Posts: 352
Re: ByRef in lisp?
« Reply #2 on: October 03, 2007, 09:12:38 AM »
I just woke up, so common sense is preventing me from posting this quick and dirty --

...Snip...

Having posted it let me say there are better strategies for achieving what you want but I don't have the time to detail right now. I'm sure the thread will flourish in my absence. Cheers.

:)

Thanks for the quick reply.  I will try to get my head wrapped around this and watch for additional replies.

Thanks,
Mike Weaver


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: ByRef in lisp?
« Reply #3 on: October 04, 2007, 08:50:51 AM »
Mike,
Using your example routine it would work like this:
Code: [Select]
(setq List1 '(1 2 3 4 5))
(setq list2 '(a b c d e))


(defun test(Data / value)
  (setq value (eval data))
  ;;  subst the first item in the list with "12"
  (set Data (subst "12" (car value) value))
  (princ)
)

Command:
Command: (test 'list1)

Command: !list1
("12" 2 3 4 5)

Command: (test 'list2)

Command: !list2
("12" B C D E)


Although I don't understand why you want to do this?
« Last Edit: October 04, 2007, 09:17:36 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: ByRef in lisp?
« Reply #4 on: October 04, 2007, 09:04:22 AM »
Hi perhaps I don't understand, but, why do you (MP & CAB) store the data value in a local variable ?

The result returned by (subst ...) won't change the original Data and the (eval ...) expression needs a quoted variable and don't allows to directly do :
(test '(1 2 3 4 5))

Code: [Select]
(defun test (data)
  (subst "12" (car data) data)
)

$ (setq list1 '(1 2 3 4 5))
(1 2 3 4 5)
_$ (test list1)
("12" 2 3 4 5)
_$ (test '(1 2 3 4 5))
("12" 2 3 4 5)
_$ list1
(1 2 3 4 5)

« Last Edit: October 04, 2007, 09:13:35 AM by gile »
Speaking English as a French Frog

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: ByRef in lisp?
« Reply #5 on: October 04, 2007, 09:18:21 AM »
Sorry, my example was in error, so I replaced the code.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

mkweaver

  • Bull Frog
  • Posts: 352
Re: ByRef in lisp?
« Reply #6 on: October 04, 2007, 09:23:34 AM »
Hi perhaps I don't understand, but, why do you (MP & CAB) store the data value in a local variable ?

the result returned by (subst ...) won't change the original Data

MP's code will change the original list, and Cab's will with the addition of the Set function shown by MP, thus:

Code: [Select]
(setq List1 '(1 2 3 4 5))
(setq list2 '(a b c d e))

(defun test(Data / value)
  (setq value (eval data))
  ;;  subst the first item in the list with "12"
  ([color=red]set[/color] data (subst "12" (car value) value))
)
(1 2 3 4 5)
(A B C D E)
TEST
(test 'list1)
("12" 2 3 4 5)
list1
("12" 2 3 4 5)
(test 'list2)
("12" B C D E)
list2
("12" B C D E)


This does exactly what I wanted, though not quite as easiliy as I had hoped.

I guess the other option would be this:

Code: [Select]
(setq List1 '(1 2 3 4 5))
(setq list2 '(a b c d e))

(defun test(Data / value)
  ;;  subst the first item in the list with "12"
 (subst "12" (car value) value)
)

Then calling the routine thus:
(setq List1 (test list1))
(setq list2 (test list2))


This is being driven by multiple lists that need the same functions performed on them.  Since these tend to be fairly large lists, I'm hoping the first approach will improve performance and/or decrease memory usage.

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: ByRef in lisp?
« Reply #7 on: October 04, 2007, 09:37:49 AM »
Sorry, I completely misunderstood. I thaught you didn't want to change original list :oops:
Speaking English as a French Frog

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: ByRef in lisp?
« Reply #8 on: October 04, 2007, 09:40:00 AM »
Perhaps if you posted the function someone could speed it up for you.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

mkweaver

  • Bull Frog
  • Posts: 352
Re: ByRef in lisp?
« Reply #9 on: October 04, 2007, 02:34:03 PM »
Sorry, I completely misunderstood. I thaught you didn't want to change original list :oops:

No need to feel sorry.  Any and all comments are appreciated.