Page 1 of 1

dolist on main-args problem

PostPosted: Wed Aug 23, 2017 9:18 pm
by dukester
I have this test code:
Code: Select all
#!c:\bin\newlisp\newlisp.exe
(println (main-args 2))   ;; works OK

(dolist (x (main-args))
(println $idx ":" x))
(exit)


The above code works fine! BUT - when I ask dolist to use a "break expression", I'm making it choke and I don't know why!

So if I do:
Code: Select all
(dolist (x (main-args 2))


No joy!! Help!! and I have read the manual!! TIA ....

BTW, this is happening on a Win10 box

Re: dolist on main-args problem

PostPosted: Wed Aug 23, 2017 10:51 pm
by ralph.ronnquist
Maybe you confuse it with the following?
Code: Select all
(dolist (x (2 (main-args)))

Re: dolist on main-args problem

PostPosted: Thu Aug 24, 2017 1:17 am
by dukester
Thanks for the reply Ron!

The manual says:
dolist
syntax: (dolist (sym list|array [exp-break]) body)

(dolist (x '(a b c d e f g) (= x 'e)) ; prints: abcd
(print x))


The optional "exp break" appeared to follow all the other arguments!

How am I interpreting the manual incorrectly?

Re: dolist on main-args problem

PostPosted: Thu Aug 24, 2017 4:00 am
by ralph.ronnquist
One could read it like this: that the first element of a dolist is a list with three elements:
- firstly a symbol,
- then a list or array,
- then, optionally, a "break expression".

Especially, that break expression is a third element of the list, and not an argument to the list construction expression. (count the parentheses)

Further it tells me, that the middle element, i.e., the list or array, is actually an expression that is evaluated once initially, to provide the actual sequence of values to assign to the symbol, i.e. the first element of the list. And it tells me, that the third element, i.e. the break expression, is a term that is evaluated and re-evaluated subsequent to each assignment, to determine whether or not to break, i.e. exit out of the dolist expression.

Re: dolist on main-args problem

PostPosted: Thu Aug 24, 2017 4:28 am
by dukester
So how does this reconcile with the code snippet

Code: Select all
(dolist (x (2 (main-args)))


that you kindly volunteered in your previous post - which works like a charm?

Re: dolist on main-args problem

PostPosted: Thu Aug 24, 2017 6:33 am
by ralph.ronnquist
Well, that's a repetition over the command line arguments, starting with the third. Specifically:

+ when invoked without arguments, the main-args function returns the command line arguments as a list of strings. When invoked with an argument, an integer, then it returns only that command line argument as a string (with 0 indicating the first argument).

+ the term syntax (2 list) returns the sub list of the given list starting with the third element (i.e., skip 2). That's the sequence of values provided successively to x in the dolist clause.

There is no "break expression" involved. A break expression is something that stops the succession of assignments (and the further execution of the dolist clause). It's not a way to pick which values to repeat for (that's done by the "list|array" element), or to skip individual assignments; it basically is just a way to end the repetition prematurely.

Re: dolist on main-args problem

PostPosted: Thu Aug 24, 2017 11:50 am
by dukester
Thx Ralph!! Much obliged ...

I now see how I confused the "break expression" with "sub list" expression!

Learning any flavour of LISP as been such a challenge for me! But at my age, the struggle might be an excellent way to keep the "leetle grey cells" flashing and sparking! LOL