How to use unqualified symbols as 'parameters'

Started by cormullion, October 22, 2008, 09:33:51 AM

Previous topic - Next topic

cormullion

Here's a simple bit of code:


(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

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

(f 1 2 'white)

I get:
-1
which seems correct. But can I obtain the same result from another context than MAIN ?
<r><I>>Bertrand<e></e></I> − <COLOR color=\"#808080\">><B>newLISP<e></e></B> v.10.7.6 64-bit <B>>on Linux<e></e></B> (<I>>Linux Mint 20.1<e></e></I>)<e></e></COLOR></r>

newBert

#2
Otherwise:
(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:
-1
3
<r><I>>Bertrand<e></e></I> − <COLOR color=\"#808080\">><B>newLISP<e></e></B> v.10.7.6 64-bit <B>>on Linux<e></e></B> (<I>>Linux Mint 20.1<e></e></I>)<e></e></COLOR></r>

xytroxon

#3
Using constant numbers with global scope should be fastest.



(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 filesnewlispnewlisp.exe" "D:newlisptest.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

#4
Quote from: "xytroxon"
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:
(constant
(global 'black) 0
(global 'white) 1
(global 'blue ) 2
)

or
(map constant (map global '(black white blue)) '(0 1 2))
<r><I>>Bertrand<e></e></I> − <COLOR color=\"#808080\">><B>newLISP<e></e></B> v.10.7.6 64-bit <B>>on Linux<e></e></B> (<I>>Linux Mint 20.1<e></e></I>)<e></e></COLOR></r>

cormullion

#5
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

#6
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

#7
a nice approach

m i c h a e l

#8
And six lines becomes four with the addition of context specifiers:


(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:


(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

#9
Quote from: "m i c h a e l"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!