How to use unqualified symbols as 'parameters'

Notices and updates
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

How to use unqualified symbols as 'parameters'

Post 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 ...)

newBert
Posts: 156
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Post 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 ?
BertrandnewLISP v.10.7.6 64-bit on Linux (Linux Mint 20.1)

newBert
Posts: 156
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Post 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
BertrandnewLISP v.10.7.6 64-bit on Linux (Linux Mint 20.1)

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Re: How to use unqualified symbols as 'parameters'

Post 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
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

newBert
Posts: 156
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Re: How to use unqualified symbols as 'parameters'

Post 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))
BertrandnewLISP v.10.7.6 64-bit on Linux (Linux Mint 20.1)

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post 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...

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post 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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

a nice approach

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post 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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post 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!

Locked