Interactive or Loading?

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

Interactive or Loading?

Post by m i c h a e l »

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:

Code: Select all

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

Code: Select all

#!/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
Last edited by m i c h a e l on Mon Jun 12, 2006 2:50 am, edited 1 time in total.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Do you mean something like this?

Code: Select all

(define (interactive?) 
    (not (main-args 1)))
Lutz

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 »

Lutz wrote: 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

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: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! :-)

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

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

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 »

cormullion wrote:"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?

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 »

Peter wrote: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 ;-)
Peter wrote: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.
Peter wrote:. . . 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!
Peter wrote: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

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

Post by cormullion »

pjot wrote: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...

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Well, as one of my teachers used to say: repetition is the secret to learning. ;-)

So let's define meditation:

Code: Select all

(define (meditation) (while true 0))
Have a beer, it helps. Really!

Peter

(PS Michael I hope you still take this seriously ;-) )

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 »

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

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

*LOL*! Those would be interesting additions. Surely I am looking forward to read these chapters! :-)

Peter

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

less is more:

Code: Select all

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

William James
Posts: 58
Joined: Sat Jun 10, 2006 5:34 am

Post by William James »

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:

Code: Select all

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

Code: Select all

C:\>ruby odd-sort.rb
["strange", "the", "testing", "sort"]
File try.rb contains:

Code: Select all

require 'odd-sort'
p %w(foo and bar and so on).odd_sort

Code: Select all

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:

Code: Select all

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

Code: Select all

(load "odd-sort.lsp")
(println (odd-sort '("stuff" "junk" "trash")))
(exit)

Code: Select all

C:\>newlisp try.lsp
("red" "quick" "fox")
("stuff" "trash" "junk")
How do we keep the test code from running?

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

Post by cormullion »

Code: Select all

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

William James
Posts: 58
Joined: Sat Jun 10, 2006 5:34 am

Post by William James »

So there seems to be nothing equivalent to
if __FILE__ == $0
in newLISP.

Another Ruby feature that would be nice is sort-by:

Code: Select all

(define (odd-sort lst)
  (sort lst (fn (a b) (< (reverse a) (reverse b)))))
could become something like

Code: Select all

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

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 »

William wrote: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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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

Code: Select all

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

Code: Select all

(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

Locked