Interactive or Loading?

Started by m i c h a e l, June 07, 2006, 08:05:42 PM

Previous topic - Next topic

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:


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

Lutz

#1
Do you mean something like this?



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


Lutz

m i c h a e l

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

cormullion

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

pjot

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

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

m i c h a e l

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

cormullion

#7
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">//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

#8
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 ;-)  )

m i c h a e l

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

pjot

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



Peter

Lutz

#11
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.

William James

#12
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?

cormullion

#13
(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

#14
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.