Something has come up a second or third time now, so maybe it's time to ask the group. What is/will be the newLISP way to distinguish between a file simply being loaded and one being loaded into an interactive shell? Ruby's idiom is:
if __FILE__ == $0
# what you want to do iff this is loaded into the shell
end
__FILE__ is set to the name of the source file. $0 is the name of the program being executed (in the top-level only). By comparing these two variables, you're able to determine whether the file is being loaded on the command line. I've always hated this idiom ;-)
I thought that main-args might be a way to determine this, but I got sidetracked and forgot. I do this a lot. I'm used to it now.
Here is a rough sketch of a make-believe function named interactive?:
#!/newlisp
(define (hyphenate s)
(print
(join
(parse s { })
{-})))
(unless (interactive?) ; <- here is our imagined function
(print (hyphenate (current-text)))
(exit))
This feature seems to be part of most higher-profile scripting languages, though it always seems to be implemented so badly :-)
m i c h a e l
Do you mean something like this?
(define (interactive?)
(not (main-args 1)))
Lutz
Quote from: "Lutz"
Do you mean something like this?
Code:
(define (interactive?)
(not (main-args 1)))
Yes, exactly! Thank you. Man, that was easy.
This subject of newLISP being easy to program in brings to mind one of Ruby's guiding principles called "Least Surprise." newLISP's should be "Surprisingly Easy" ;-)
m i c h a e l
Quote from: "m i c h a e l"
newLISP's should be "Surprisingly Easy" ;-)
"newLISP: Easier than you thought it was going to be, given that it has "Lisp" in its name!"
"newLISP: not as surprising as you thought!"
"newLISP: easier that it looks, even though it looks easier than you thought it looked!"
"newLISP: prepare not to be astonished"
"newLISP: we have three weapons: easiness and surprise!"
No, I think yours is still the best! :-)
Lutz has demonstrated the power of newLISP many times in this forum. I almost fell off my chair at seeing his improvement on an idea for a 'Quine', for example.
The thing with newLISP is that it seems to confront you with your own mind. Many times I think complicated about a problem, and I start using newLISP to solve the issue in a stupid and in-effective way. However, in newLISP there always seems to be room for simplicity and easiness. In fact, the whole process of programming with newLISP resembles the art of meditation! Meditation can be described as the process of going from complicated thoughts towards simplicity and plain insights. ;-)
If I compare the practice of programming using a regular sequential language, it is me who tries to solve an issue, using the sequential language as a tool; but with newLISP, it is newLISP which solves the problem, using me as coder.
Just stop thinking and let newLISP do the talking. That would be my manual for newLISP. :-)
Peter
Quote from: "cormullion"
"newLISP: we have three weapons: easiness and surprise!"
This reminds me of the joke: "There are 10 kinds of people in the world. Those who can count in binary, and those who can't."
Okay, seriously. Here is my favorite newLISP motto: "The power of LISP, the fun of Ruby." Maybe newLISP can be famous for having the most mottos? Naw, Java already has a big head start on us ;-)
m i c h a e l
P.S. I haven't given up on your puzzle quite yet, cormullion. A little bitty hint, perhaps?
Quote from: "Peter"
Many times I think complicated about a problem, and I start using newLISP to solve the issue in a stupid and in-effective way.
Is there some other way I'm unaware of? That's the only way I've ever written programs ;-)
Quote from: "Peter"
In fact, the whole process of programming with newLISP resembles the art of meditation! Meditation can be described as the process of going from complicated thoughts towards simplicity and plain insights. ;-)
Does this mean we are going to start practicing transcendental newLISPitation? ;-)
I know exactly what you mean, Peter. Sometimes, while in the coding/thinking cycle, I do find a certain meditative quality to the experience. Whole swatches of time seem to disappear into a few beautiful lines of code.
Quote from: "Peter"
. . . it is newLISP which solves the problem, using me as coder.
That sneaky newLISP. Tricking us into solving its problems for it. What is it going to do next? Convince us to spend all of our extra time doing things for it, like writing about it and making up mottos for . . . wait a minute!
Quote from: "Peter"
Just stop thinking and let newLISP do the talking.
Beware! Peter is already under newLISP's hypnotic power. Everything he says from this point on -- especially things like "maybe newLISP would be happier if -- I mean, better if we spent more time talking to her -- I mean, it. She is not telling me to say this. I mean, newLISP doesn't control me. Hail newLISP!" -- should be taken with a grain of salt ;-)
m i c h a e l
Quote from: "pjot"
In fact, the whole process of programming with newLISP resembles the art of meditation! Meditation can be described as the process of going from complicated thoughts towards simplicity and plain insights. ;-)
I remember you saying something similar in your reply to one of my first posts, Peter //http://www.alh.net/newlisp/phpbb/viewtopic.php?t=874. I was quite impressed at the time! Although I confess I'm still struggling more than I'm flowing. But I've noticed that I'm struggling more with the problem at hand rather than the language, so there's some progress there...
Well, as one of my teachers used to say: repetition is the secret to learning. ;-)
So let's define meditation:
(define (meditation) (while true 0))
Have a beer, it helps. Really!
Peter
(PS Michael I hope you still take this seriously ;-) )
Quote from: "Peter"
(PS Michael I hope you still take this seriously ;-))
Take it seriously? You bet I'm taking it seriously! Why, I've already talked Lutz into adding three new chapters to the manual! "Higher States of Consness," "How to Run Your Dreams Through the newLISP Interpreter," and "LISP Signs: Which One Are You?"
m i c h a e l
*LOL*! Those would be interesting additions. Surely I am looking forward to read these chapters! :-)
Peter
less is more:
(define meditation until)
Lutz
ps: of course you could just say (until) which is the same as (while true), the above definition is just an assignment of until to meditation. You also could say (set 'meditation unil) which would have the same effect.
In Ruby, the main purpose of
if __FILE__ == $0
is to enable a library file to determine if it is being loaded by another file
or if it is being run as the main program.
File odd-sort.rb contains:
class Array
def odd_sort
sort_by{|x| x.reverse }
end
end
# Some test code that should not be run
# if this file is loaded by another file.
if __FILE__ == $0
p %w(testing the strange sort).odd_sort
end
C:>ruby odd-sort.rb
["strange", "the", "testing", "sort"]
File try.rb contains:
require 'odd-sort'
p %w(foo and bar and so on).odd_sort
C:>ruby try.rb
["and", "and", "on", "foo", "so", "bar"]
Note that when odd-sort.rb was loaded from another file, the
test code didn't run.
Let's do the same thing in newLisp. File odd-sort.lsp:
(define (odd-sort lst)
(sort lst (fn (a b) (< (reverse a) (reverse b)))))
;; Some test code.
(println (odd-sort '("quick" "red" "fox")))
File try.lsp:
(load "odd-sort.lsp")
(println (odd-sort '("stuff" "junk" "trash")))
(exit)
C:>newlisp try.lsp
("red" "quick" "fox")
("stuff" "trash" "junk")
How do we keep the test code from running?
(define (odd-sort lst)
(sort lst (fn (a b) (< (reverse a) (reverse b)))))
(define (test-odd-srt)
;; Some test code.
(println (odd-sort '("quick" "red" "fox"))))
I noticed that some newLISP modules (eg SQLite) define test routines but don't have them lying around loose, like your example. So the tests can be in the source file, but they don't run unless you specifically want them to.
But there might be a way...
So there seems to be nothing equivalent to
if __FILE__ == $0
in newLISP.
Another Ruby feature that would be nice is sort-by:
(define (odd-sort lst)
(sort lst (fn (a b) (< (reverse a) (reverse b)))))
could become something like
(define (odd-sort lst)
(sort-by lst (reverse $0)))
Not only would the coding be easier, but the sort would run faster since the "Schwartzian Transform" would be used internally. Using a simple sort, every time word x is compared to another word, it must be reversed. When using the Schwartzian Transform, each word is reversed only once.
Quote from: "William"
So there seems to be nothing equivalent to
if __FILE__ == $0
in newLISP.
Actually, I think Lutz's response to my question is in keeping with the spirit of newLISP, which is the answer, "Yes!" to the question, "Can newLISP do my favorite thing in language x?" You just need to figure out how to program it ;-) Of course, Lutz may take pity and bequeath the solution on you, and like an answered wish, there it is! And then something you thought was a special part of your former language becomes a few lines of newLisp, and you begin to wonder what the fuss was all about in the first place.
May the Schwartz be with you,
m i c h a e l
Quote
In Ruby, the main purpose of if __FILE__ == $0 is to enable a library file to determine if it is being loaded by another file or if it is being run as the main program.
In newLISP I wouldn't know about a situation where this is important. I guess this addresses a Ruby specific situation with multiple loading of libraries?
But you could have some kind of flag at the top-level of you module
(context 'MyLib)
(set 'loaded true)
...
...
The main program then could test for the 'MyLib:loaded' flag to find out if it has already been loaded:
(if MyLib:loaded
(do-this)
(do-that))
In newLISP when loading a module multiple times, all the top-level define's and assignments in the module will simply be overwritten again to their initial state and without wasting any additional memory.
If this should be prevented then some thing like the above load-flag or a load counter could prevent this. Another method would be to let the loaded module itself detect if already loaded and then perhaps exit.
Lutz