newLISP 10.1.2 issues

Started by dvebc, August 27, 2009, 02:36:12 PM

Previous topic - Next topic

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

cormullion

#1
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:


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


> (read-file "myfile")
"hello worldn"
>


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.

Lutz

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

dvebc

#3
Quote from: "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:


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


> (read-file "myfile")
"hello worldn"
>


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

dvebc

#4
Quote from: "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".


Many thanks Lutz ,



I tried the foreward slash "/" and it doesnt work for me . I will try the "" now.



Regards

HPW

#5
QuoteIs there anyway that I can stop the return value getting printed.


You can use: (silent(println "Hello World"))
Hans-Peter

ale870

#6
Quote from: "dvebc"
Quote from: "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".


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

dvebc

#7
Quote from: "ale870"
Quote from: "dvebc"
Quote from: "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".


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

dvebc

#8
Quote from: "HPW"
QuoteIs 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

ale870

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


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:


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

dvebc

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

ale870

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

cormullion

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


(for (x 1 2) (println x))

or


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

dvebc

#13
Quote from: "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?




That example can be found in the 'Introduction to newLISP' tutorial , which is here http://www.newlisp.org/index.cgi?Documentation">//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]




Thanks

Sammo

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