newLISP Fan Club

Forum => Anything else we might add? => Topic started by: Dmi on October 01, 2005, 04:22:09 AM

Title: macro for (dolist)-like function
Post by: Dmi on October 01, 2005, 04:22:09 AM
I want to write something like:
(dofile (sym filespec opts) (some-code))
to simplify processing of large files with regular substructures (for example - csv or ldif) in record-by-record basis - without reading-in whole file.

where

 sym is symbol name like exactly such one in (dolist) - not predefined and existing locally to dofile contents.

 filespec can be value or expression that results in str-filename or int-fd.

 opts are optional arguments like record delimiter other than n, or buffer size for (read-buf) etc.



My trouble is about a way to handle sym in macro function. Only way I found is to compose lambda like
(let (sym dofile-current-record-value) (some-code))
and to call it in dofile internal loop.

Is this the best choice?
Title:
Post by: Lutz on October 01, 2005, 07:10:17 AM
That works. You also could use the 'expand' function to redefine symbols in expressions:





newLISP v.8.6.0 on OSX, execute 'newlisp -h' for more info.

> (set 'smbl 'idx)
idx

> (eval (expand '(dolist (smbl '(a b c d e)) (println smbl)) 'smbl))
a
b
c
d
e
e
>

Let me show this step by step

> (set 'prog '(dolist (smbl '(a b c d e)) (println smbl)))
(dolist (smbl '(a b c d e))
 (println smbl))

> (expand prog 'smbl)
(dolist (idx '(a b c d e))
 (println idx))

> (eval (expand prog 'smbl))
a
b
c
d
e
e
>

Lutz
Title:
Post by: Dmi on October 01, 2005, 08:23:14 AM
Thanks!  Cool!



As I understand, the way with (expand) will break dynamic scoping?

But I don't use it now (probably yet... ;-)
Title:
Post by: Lutz on October 01, 2005, 08:47:29 AM
'expand' does not relate to dynamic scoping in any way. It is just replacing a symbol in an expression with the content of that symbol's evaluation.



(set 'x 1 'y 2 'z 3)

(expand '(x (y z)) 'x 'y 'z) => (1 (2 3))


Its like a multilevel replace/expansion



Lutz
Title:
Post by: Dmi on October 01, 2005, 09:04:59 AM
Of course. :-)



But, hmm... I found another cool ways to apply (expand) to my task, than I've seen before!

Thanks again :-)