newLISP Fan Club

Forum => newLISP newS => Topic started by: cormullion on October 22, 2008, 09:33:51 AM

Title: How to use unqualified symbols as 'parameters'
Post by: cormullion on October 22, 2008, 09:33:51 AM
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 ...)
Title:
Post by: newBert on October 23, 2008, 12:45:48 AM
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 ?
Title:
Post by: newBert on October 23, 2008, 12:56:42 AM
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
Title: Re: How to use unqualified symbols as 'parameters'
Post by: xytroxon on October 23, 2008, 01:00:11 AM
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
Title: Re: How to use unqualified symbols as 'parameters'
Post by: newBert on October 23, 2008, 01:58:34 AM
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))
Title:
Post by: cormullion on October 23, 2008, 09:24:23 AM
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...
Title:
Post by: m i c h a e l on October 23, 2008, 12:42:05 PM
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
Title:
Post by: cormullion on October 24, 2008, 11:50:54 AM
a nice approach
Title:
Post by: m i c h a e l on October 24, 2008, 01:54:58 PM
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
Title:
Post by: cormullion on October 25, 2008, 01:57:48 AM
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!