delayed evaluation of arguments

This is a moderated forum that collects tutorials, guides, and references for creating Transcendence extensions and scripts.
Post Reply
User avatar
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

Wed Feb 17, 2010 10:46 pm

Suppose you make a lambda function with several arguments sometimes you don't want to use all those arguments. There is a way to make that code faster by not evaluating all of the arguments.

Here is a silly example.

Code: Select all

(setq oddEvenMulti (lambda (a %b %c)
	(if (eq 1 (modulo a 2))
		(multiply a (eval %b))
		(multiply a (eval %c))
As you can see I used % to stop the evaluation until I wanted it. You may ask how would this help?

Consider the case

Code: Select all

(oddEvenMulti (objGetData gplayership "numb") (objGetData (item (sysFindObject gPlayerShip "G") 0) "numb") (objGetData (item (sysFindObject gPlayerShip "G") 1) "numb"))
Now you will never use both stargates so are you using an extra sysFindObject (it is an expensive function), objGetData, and item if you did not use delayed evaluation. This could save you lots of time overall and would make your function more friendly to complex arguments.
Crying is not a proper retort!

User avatar
Fleet Officer
Fleet Officer
Posts: 1658
Joined: Wed Jan 14, 2009 3:08 am
Location: Previously enslaved by the Iocrym

Wed Feb 17, 2010 10:54 pm

Another case for this is for default values. I have this function I use sometimes:

Code: Select all

(setq extSelect (lambda (test val) (if test test val)))

;; I use something like this to determine some positions
(setq pos (extSelect (find somelist element) (count somelist)))
The only problem is that (count somelist) is always evaluated, making this slow in some cases. This can now be rewritten like this:

Code: Select all

(setq extSelect (lambda (test %val)
  (if test test (eval %val))
Much better :D

User avatar
Militia Captain
Militia Captain
Posts: 826
Joined: Tue Nov 18, 2008 6:09 pm

Sun May 30, 2010 2:32 pm

While reading some common Lisp tutorials online on found some information that is relevant.
The true power of the ' ! (single quote)

The ' character flips the interpreter into read only mode:
(add 1 1)
is normally a function call and the interpreter will treat the first parameter as a function.
'(add 1 1)
is now a list with a string and 2 integers.

So what does that mean?

You can store a function in a variable and use it, modify or whatever you want, whenever you want.

(setq myVar '(add 1 1))
currently myVar is a list with 3 elements,
(eval myVar) returns 2

(lnkAppend myVar 5))
returns the list (add 1 1 5)
(eval myVar) now returns 7
Coming soon: The Syrtian War adventure mod!
A Turret defense genre mod exploring the worst era in Earth's history.
Can you defend the Earth from the Syrtian invaders?
Stay tuned for updates!

Post Reply