Page 1 of 1
newLISP 10.1.2 issues
Posted: Thu Aug 27, 2009 9:36 pm
by dvebc
Hi all,
I am new in this forum.
Installed newLISP in my winxp machine and I started learning it with the help of 'Introduction to newLISP'.
One of my problem is with the print function
(print "Hello World")
result comes like this -- Hello World"Hello World"
it's repeating.
Another issue is
(println(read-file "c:\path-file")) gives a nil eventhough the file exist.
I wonder anyone here can help me on this
Many Thanks
Posted: Thu Aug 27, 2009 10:09 pm
by cormullion
Hi! Every expression in newLISP returns a value. (print "Hello World") is an expression, and it returns the value "Hello World". You'll see this in the terminal, just as you'll see the result of an expression such as (+ 2 2). However, the print function has a side-effect: it prints the string to the current output device. This may also be the terminal. If it is, you'll see both the 'printing' action and the return value of the expression.
It isn't always true that printing goes to the screen, though:
Code: Select all
> (println "hello world")
hello world
"hello world"
> (device (open "myfile" "write"))
3
>
> (println "hello world")
"hello world"
> (close (device))
true
> (println "hello world")
hello world
"hello world"
>
If you look in myfile, you'll see it contains the printing, because (device ...) redirected it. You saw the return values of each expression as usual, though.
Also:
Code: Select all
> (read-file "myfile")
"hello world\n"
>
shows that (read-file) worked as expected. You don't need (println), unless sending output to a file... I think "C:\path-file" should work if that really is the exact name of the file.
Posted: Fri Aug 28, 2009 2:17 am
by Lutz
"C:\path-file" will not work, because the "\" is a special (escape) character. The best for file-path names is, to use the forward slash instead, like in "C:/path-file". The forward slash will work on both: Win32 an Unix operating systems. Or you could escape the "\" using "C:\\path-file".
Posted: Fri Aug 28, 2009 12:28 pm
by dvebc
cormullion wrote:Hi! Every expression in newLISP returns a value. (print "Hello World") is an expression, and it returns the value "Hello World". You'll see this in the terminal, just as you'll see the result of an expression such as (+ 2 2). However, the print function has a side-effect: it prints the string to the current output device. This may also be the terminal. If it is, you'll see both the 'printing' action and the return value of the expression.
It isn't always true that printing goes to the screen, though:
Code: Select all
> (println "hello world")
hello world
"hello world"
> (device (open "myfile" "write"))
3
>
> (println "hello world")
"hello world"
> (close (device))
true
> (println "hello world")
hello world
"hello world"
>
If you look in myfile, you'll see it contains the printing, because (device ...) redirected it. You saw the return values of each expression as usual, though.
Also:
Code: Select all
> (read-file "myfile")
"hello world\n"
>
shows that (read-file) worked as expected. You don't need (println), unless sending output to a file... I think "C:\path-file" should work if that really is the exact name of the file.
Thanks a mil cormullion ,
That helps a lot .Is there anyway that I can stop the return value getting printed.
And is there any IDEs or editors , which I can configure newLISP .
Regards
Posted: Fri Aug 28, 2009 12:29 pm
by dvebc
Lutz wrote:"C:\path-file" will not work, because the "" is a special (escape) character. The best for file-path names is, to use the forward slash instead, like in "C:/path-file". The forward slash will work on both: Win32 an Unix operating systems. Or you could escape the "" using "C:\\path-file".
Many thanks Lutz ,
I tried the foreward slash "/" and it doesnt work for me . I will try the "\" now.
Regards
Posted: Fri Aug 28, 2009 1:33 pm
by HPW
Is there anyway that I can stop the return value getting printed.
You can use: (silent(println "Hello World"))
Posted: Fri Aug 28, 2009 1:37 pm
by ale870
dvebc wrote:Lutz wrote:"C:\path-file" will not work, because the "" is a special (escape) character. The best for file-path names is, to use the forward slash instead, like in "C:/path-file". The forward slash will work on both: Win32 an Unix operating systems. Or you could escape the "" using "C:\\path-file".
Many thanks Lutz ,
I tried the foreward slash "/" and it doesnt work for me . I will try the "\" now.
Regards
Does not work? It's strange. Can you send me the code you are using?
Posted: Fri Aug 28, 2009 7:11 pm
by dvebc
ale870 wrote:dvebc wrote:Lutz wrote:"C:\path-file" will not work, because the "" is a special (escape) character. The best for file-path names is, to use the forward slash instead, like in "C:/path-file". The forward slash will work on both: Win32 an Unix operating systems. Or you could escape the "" using "C:\\path-file".
Many thanks Lutz ,
I tried the foreward slash "/" and it doesnt work for me . I will try the "\" now.
Regards
Does not work? It's strange. Can you send me the code you are using?
Sorry , I think I made some mistakes yesterday , "Late nights = No Brain".
Tried it forward slash / again and it's working . Double back slash \\ is also working .
Many Thanks
Posted: Fri Aug 28, 2009 7:16 pm
by dvebc
HPW wrote:Is there anyway that I can stop the return value getting printed.
You can use: (silent(println "Hello World"))
Magic , That is working . Can you please explain , what happens to the returned value now when you get a chance?
Does every function is newLISP return a value?
I am very new to LISP , that's why asking all these silly questions .
Thanks
Posted: Fri Aug 28, 2009 9:45 pm
by ale870
Well, every "command" in newLisp is a function. For example:
(println 100)
(println) is a function, and "100" is the argument for that function.
Some functions can have variable number of parameters. For example:
(push myVar "A") ;; Put the string "A" in the variable myVar, at the beginning. But...
(push myVar "A" -1) I added another parameter: "-1". It means I put "A" to the end of the variable.
Now don't care about (push) self. But look at that (push) function can accept 2 or 3 arguments.
Every function return a value. So you can do:
(+ 3 4) ; returns 7
(+ 2 (+ 3 4) 5) ; it is: 2 + (3+4) + 5 --> 14
The function (silent) will "eliminate" the last returning value.
Code: Select all
Try this (go in the console and type):
> (define (calc) (+ 3 2))
(lambda () (+ 3 2))
> (calc)
5
I defined a function, called "calc". As you can see, even i f I didn't used a RETURN value (as you make in C or Java for example), that function return the last value managed: my sum.
Try this:
Code: Select all
> (define (calc) 88)
(lambda () 88)
> (calc)
88
As you can see, now the function return the only value I inserted: 88.
I hope this help you to understand this concept :-)
Posted: Sun Aug 30, 2009 11:22 am
by dvebc
Thanks ale870 for the detailed reply.
I am bit confused on this code given in the tutorial .
define number-list ’(100 300 500 701 900 1100 1300 1500))
(dolist (n number-list (!= (mod n 2) 0)) ; escape if true
(println (/ n 2)))
In the previous section of the tutorial ,
(dolist (n number-list ))
it's said n is a loop variable and it takes a value from list for each iteration.
But in this case, I am wondering which function will execute first .
(dolist (n number-list (!= (mod n 2) 0))
If it is (!= (mod n 2) 0) , how is 'n' getting the value from the list .
Thanks
Posted: Mon Aug 31, 2009 2:22 pm
by ale870
In that example it seems there are some minor but relevant syntax errors.
I didn't see the original example, so you could cheeck if you copied it well.
Can you tell m exaclty where you found it?
Posted: Mon Aug 31, 2009 7:01 pm
by cormullion
The example is correct, although there is an opening parenthesis missing, and the quote is smart - it should be a simple single apostrophe. It's probably been copied from the PDF, which has faulty punctuation...
It's a good question. I don't know the precise answer. However, it's a general principle in newLISP that lists aren't always evaluated immediately. The language would be very hard to work in if there were no special forms such as :
or
Code: Select all
(if (= x 2) (println "continue") (exit))
So I would suggest that the built-in functions have to override the "rules", and user-written functions can do as well. Even though
dolist is probably not written in newLISP, it probably could be...
Posted: Mon Aug 31, 2009 7:24 pm
by dvebc
ale870 wrote:In that example it seems there are some minor but relevant syntax errors.
I didn't see the original example, so you could cheeck if you copied it well.
Can you tell m exaclty where you found it?
That example can be found in the 'Introduction to newLISP' tutorial , which is here
http://www.newlisp.org/index.cgi?Documentation .Please look in the section;
looping /an escape route is available
As curmullion mentioned , I have missed a opening paranthesis. The correct code would be ,
(define number-list ’(100 300 500 701 900 1100 1300 1500))
(dolist (n number-list (!= (mod n 2) 0))
(println (/ n 2)))
when this code is run in newLISP shell , an error is returned
>
nil
ERR: list expected in function dolist : number-list
>
Thanks
Posted: Mon Aug 31, 2009 7:51 pm
by Sammo
Work for me after replacing the ASCII 92 character you used as a single quote with a bonafide single quote character ASCII 27. Here is the working code:
(define number-list '(100 300 500 701 900 1100 1300 1500))
(dolist (n number-list (!= (mod n 2) 0))
(println (/ n 2)))
Posted: Mon Aug 31, 2009 8:11 pm
by dvebc
Sammo wrote:Work for me after replacing the ASCII 92 character you used as a single quote with a bonafide single quote character ASCII 27. Here is the working code:
(define number-list '(100 300 500 701 900 1100 1300 1500))
(dolist (n number-list (!= (mod n 2) 0))
(println (/ n 2)))
Thanks Sammo ,
That works now
still I am confused which function is executed first in this code,
(dolist (n number-list (!= (mod n 2) 0))
regards
Posted: Mon Aug 31, 2009 8:33 pm
by Sammo
In this code
(dolist (n number-list (!= (mod n 2) 0)) (println (/ n 2)))
symbol n is assigned the value of an element of number-list after which function (!= (mod n 2) 0) is evaluated. If number-list has no elements, the dolist function terminates without having evaluated the [exp-break] function or the [body] of the dolist expression.
Posted: Tue Sep 01, 2009 2:26 pm
by ale870
Thank you. I must admit that code was a small labyrinth even for me!