dolist on main-args problem

Started by dukester, August 23, 2017, 02:18:05 PM

Previous topic - Next topic

dukester

I have this test code:

#!c:binnewlispnewlisp.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:

(dolist (x (main-args 2))


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



BTW, this is happening on a Win10 box
duke

rrq

#1
Maybe you confuse it with the following?
(dolist (x (2 (main-args)))

dukester

#2
Thanks for the reply Ron!



The manual says:
Quote
dolist

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



(dolist (x '(a b c d e f g) (= x 'e))  ; prints]

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



How am I interpreting the manual incorrectly?
duke

rrq

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

dukester

#4
So how does this reconcile with the code snippet



(dolist (x (2 (main-args)))


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

rrq

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

dukester

#6
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
duke