newLISP 10.1.2 issues

Notices and updates
Locked
dvebc
Posts: 8
Joined: Thu Aug 27, 2009 8:54 pm

newLISP 10.1.2 issues

Post 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

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

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

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

Post 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".

dvebc
Posts: 8
Joined: Thu Aug 27, 2009 8:54 pm

Post 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

dvebc
Posts: 8
Joined: Thu Aug 27, 2009 8:54 pm

Post 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

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

Is there anyway that I can stop the return value getting printed.
You can use: (silent(println "Hello World"))
Hans-Peter

ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

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

dvebc
Posts: 8
Joined: Thu Aug 27, 2009 8:54 pm

Post 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

dvebc
Posts: 8
Joined: Thu Aug 27, 2009 8:54 pm

Post 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

ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

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

dvebc
Posts: 8
Joined: Thu Aug 27, 2009 8:54 pm

Post 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

ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

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

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

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

Code: Select all

(for (x 1 2) (println x))
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...

dvebc
Posts: 8
Joined: Thu Aug 27, 2009 8:54 pm

Post 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

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post 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)))

dvebc
Posts: 8
Joined: Thu Aug 27, 2009 8:54 pm

Post 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

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

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

ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

Post by ale870 »

Thank you. I must admit that code was a small labyrinth even for me!
--

Locked