newlisp --quite

Q&A's, tips, howto's

newlisp --quite

Postby hds1 » Sun Feb 15, 2015 8:29 pm

Hello,

is it possible to turn off the standard return output of newlisp ?
i.e: echo "(print 'willi')" | newlisp
willinil
nil
Here the "nil" value.

So my feature request would be something like:
echo "(println 'willi')" | newlisp -q
--> willi

Kubuntu 14.04, kernel 3.13.0-45-generic, newLISP v.10.6.2 64-bit on Linux IPv4/6 UTF-8 libffi

Regards
Heiko
hds1
 
Posts: 27
Joined: Thu Mar 20, 2014 5:02 pm

Re: newlisp --quite

Postby Lutz » Sun Feb 15, 2015 8:44 pm

Note, that newLISP does not use single quotes as string delimiters.

To suppress the return value of an expression in the console, use silent. Now you only see the side effect, printing the string "will" and a line feed, because println was used instead of print

Code: Select all
~> echo '(silent (println "willi"))' | newlisp
willi
~>
Lutz
 
Posts: 5276
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: newlisp --quite

Postby hds1 » Mon Feb 16, 2015 8:39 am

thanks for the hint with (silent).
But you need to wrap your whole proggi into it.
Code: Select all
(silent
  (long prog to follow))

Hm, it doesn't feel right to me.

Consider other scripting languages:
echo 'print "laura\n";' | perl
echo 'print "laura\n";' | ruby
echo 'print "laura\n";' | python (adds an extra \n because of default print)
No extra return value on the console.

echo '(println "laura")' | newlisp
laura -> printed
"laura" -> return value

Is there a special reason i miss (or don't understand) that NL needs to return a value to the console ?

Thanks and Regards
Heiko
hds1
 
Posts: 27
Joined: Thu Mar 20, 2014 5:02 pm

Re: newlisp --quite

Postby Lutz » Mon Feb 16, 2015 3:22 pm

In a functional language like Lisp, everything has a return value - consumed by an enclosing expression. There are no functions with only side effects, like in other programming languages. Often it is important to distinguish between the side effect and the return value of a function. You also wouldn’t have a longer prog in an echo statement. With a longer prog you would do:

Code: Select all
newlisp prog | otherprog

or with “#!/usr/bin/newlisp” in the first line of the script:
Code: Select all
#!usr/bin/newlisp

(define (foo x)
    (+ x x))

(println "(foo 123 ->)" (foo 123))
(exit)

... you can do in the shell:
Code: Select all
~> prog
(foo 123) -> 246
~>

prog must have executable permissions and in the executable path and you only see print action, no return values.

BTW, you also can do:

Code: Select all
echo '(silent) (println "hello")' | newlisp

the first expression after silent would than be silent. You don’t have to enclose the whole expression.

But in a one-liner you probably wouldn’t use print at all but just work with the return value:
Code: Select all
~> echo '(+ 3 4)' | newlisp
7
~> newlisp -e '(+ 3 4)'
7


A few links about the REPL (heavily relying on return values) and shell scripting:
http://www.newlisp.org/downloads/newlis ... .html#REPL
http://www.newlisp.org/downloads/newlis ... ml#options
http://www.newlisp.org/downloads/CodePa ... html#toc-2

the last link shows you how to consume output from another program in newLISP when newLISP is at the receiving end of a pipe.
Lutz
 
Posts: 5276
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 2 guests