Page 1 of 1

How to use unqualified symbols as 'parameters'

Posted: Wed Oct 22, 2008 4:33 pm
by cormullion
Here's a simple bit of code:

Code: Select all

(context 'C)

(define (f x y (mode 'black))
  (cond
    ((= mode 'blue)
        (+ x y))
    ((= mode 'white)
        (- x y))
    (true
        0)))

(context MAIN)

(C:f 1 2 'white)
which doesn't work, of course, since the 'white in the function call isn't the same as the 'white in the function definition. (I've made this mistake lots of times, so I'm familiar with it...:)

What's the best (fastest, safest, clearest) way to get this to work:

- declare the symbols to be global
- use strings instead of symbols
- use 'C:white rather than 'white in the function call
- in the context, refer to MAIN:white (but I don't always want to call it from the MAIN context ...)

Posted: Thu Oct 23, 2008 7:45 am
by newBert
With:

Code: Select all

(define (f:f x y (mode 'black))
  (cond
    ((= mode 'blue)
        (+ x y))
    ((= mode 'white)
        (- x y))
    (true
        0)))

(f 1 2 'white)
I get:

Code: Select all

-1
which seems correct. But can I obtain the same result from another context than MAIN ?

Posted: Thu Oct 23, 2008 7:56 am
by newBert
Otherwise:

Code: Select all

(context 'C)

(define (f x y (mode 'black))
  (cond
    ((= mode 'blue)
        (+ x y))
    ((= mode 'white)
        (- x y))
    (true
        0)))

(context MAIN)
(C:f 1 2 'C:white)

(context 'Z)
(C:f 1 2 'C:blue)
produce:

Code: Select all

-1
3

Re: How to use unqualified symbols as 'parameters'

Posted: Thu Oct 23, 2008 8:00 am
by xytroxon
Using constant numbers with global scope should be fastest.

Code: Select all

(setq black 0 white 1 blue 2)
(constant (global 'black 'white 'blue))

(context 'C)

(define (f x y (mode black))
	(print mode ": ") ; show mode
  (cond
    ((= mode blue)
        (+ x y))
    ((= mode white)
        (- x y))
    (true
        0)))

(context MAIN)

(println (C:f 1 2 black))
(println (C:f 1 2 white))
(println (C:f 1 2 blue))
(println (C:f 1 2))
(exit)

>"c:\program files\newlisp\newlisp.exe" "D:\newlisp\test.nl"
0: 0
1: -1
2: 3
0: 0
>Exit code: 0
For:

(setq black 0 white 1 blue 2)
(constant (global 'black 'white 'blue))

Unless I missed it ;) It might be nice to have an enumeration list function(s).

(enum 'black 0 'white 1 'blue 2)

And or:

(enum-global 'black 0 'white 1 'blue 2)

-- xytroxon

Re: How to use unqualified symbols as 'parameters'

Posted: Thu Oct 23, 2008 8:58 am
by newBert
xytroxon wrote: For:

(setq black 0 white 1 blue 2)
(constant (global 'black 'white 'blue))

Unless I missed it ;) It might be nice to have an enumeration list function(s).

(enum 'black 0 'white 1 'blue 2)

And or:

(enum-global 'black 0 'white 1 'blue 2)

-- xytroxon
You can do:

Code: Select all

(constant
	(global 'black) 0
	(global 'white) 1
	(global 'blue ) 2
)
or

Code: Select all

(map constant (map global '(black white blue)) '(0 1 2))

Posted: Thu Oct 23, 2008 4:24 pm
by cormullion
Thanks for the good ideas...!

Perhaps my reservations are because I'm reluctant to add symbols to the namespaces unnecessarily. Just some kind of indicator to pass a setting to a function, rather than requiring anything permanent...

Posted: Thu Oct 23, 2008 7:42 pm
by m i c h a e l

Code: Select all

newLISP v.9.9.9 on OSX IPv4 UTF-8, execute 'newlisp -h' for more info.

> (context 'C)
C
C> (define (black) 0)
(lambda () 0)
C> (define white -)
- <11290>
C> (define blue +)
+ <11280>
C> (define (f x y (mode black)) (apply mode (list x y)))
(lambda (x y (mode black)) (apply mode (list x y)))
C> (context MAIN)
MAIN
> (C:f 1 2 C:black)
0
> (C:f 1 2 C:white)
-1
> (C:f 1 2 C:blue)
3
> (C:f 1 2)
0
> _
;-)

m i c h a e l

Posted: Fri Oct 24, 2008 6:50 pm
by cormullion
a nice approach

Posted: Fri Oct 24, 2008 8:54 pm
by m i c h a e l
And six lines becomes four with the addition of context specifiers:

Code: Select all

(define (C:black) 0)
(define C:white -)
(define C:blue +)
(define (C:f x y (mode C:black)) (apply mode (list x y)))
I leave the parameters in the MAIN context, but they could also be placed in C:

Code: Select all

(define (C:f C:x C:y (C:mode C:black)) (apply C:mode (list C:x C:y)))
but I find that unnecessary.

BTW, cormullion, I was all set to post a comment on your Spaghetti post, but when talk turned to beer, it sort of spoiled my opening joke: "Some spaghetti to go with your beer, cormullion?" By which I meant your 99 bottles bottle code :-)

I like how your spaghetti program is based on code nesting and element type. It reminds me of some code I worked on to generate music based on newLISP code. It relied on code nesting, too. I may try to get that code together if I can ever successfully clone myself. Don't ask about the failures ;-)

m i c h a e l

Posted: Sat Oct 25, 2008 8:57 am
by cormullion
m i c h a e l wrote:I may try to get that code together if I can ever successfully clone myself. Don't ask about the failures ;-)
still waiting for that Font Viewer program you previewed us once... :)

Perhaps you should just lower your standards and post them anyway. That's what I do!