What is the direct equivalent of for-each(Scheme) in newlisp

Started by jopython, December 29, 2011, 08:03:25 PM

Previous topic - Next topic

jopython

In Scheme

> (for-each (lambda (x) (display (* x x))) '(1 2 3 4))
14916


I didn't see a foreach function in the manual.



Note : I am NOT looking for 'dolist' which is different from foreach.

TedWalther

Try map, filter, clean, and dolist.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

HPW

With help from Lutz I got this some years ago.

(define-macro (foreach _foreachx _foreachlst)(eval (list 'dolist (list _foreachx _foreachlst) (append (list 'begin) (args)))))
 (constant (global 'foreach))
Hans-Peter

jopython

I am getting an error for the foreach macro.



(foreach (fn (x) (println (* x x))) '(1 2 3 4))



ERR: symbol expected in function dolist : (lambda (x) (println (* x x)))
called from user defined function foreach


Is there a macroexpand-1 facility in newlisp to see what is going on underneath?

Kazimir Majorinc

#4
Yes, that macro is intended to be used on different way.



(foreach i '(1 2 3 4) (println (* i i)))



In the meantime you can use following (as Ted suggested):



(setf foreach map)

(foreach (fn(x)(println(* x x))) '(1 2 3 4))




In Scheme, map and for-each are the same, just 'return value' of for-each isn't specified. It can be the same as result of map (at least according to R6RS).



You cannot macroexpand in Newlisp, since Newlisp macros are actually fexprs, they do not expand. In many cases you can replace external eval with println; in that case macro call will print what was intended to be evaluated.



(define-macro (foreach _foreachx _foreachlst)(println (list 'dolist (list _foreachx _foreachlst) (append (list 'begin) (args)))))
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

Lutz

Some general thoughts to learning programming in newLISP:



There is no "underneath" in newLISP, no hidden states, (almost) everything is visible and can be inspected. You can use the 'debug' function to step through 'define-macro's. These are not compiled expansion macros as in Common LISP but rather fexprs - functions without automatic evaluation of their arguments. Kazimir discusses fexprs in more detail on his site.



I don't think it is a good idea, trying to implement a 'foreach' in newLISP. There are other built-in functions doing the same or similar and without the overhead of a function/fexpr definition. Like 'map'. Kazimir simply defined 'foreach' as 'map'.



Don't try to program like Common Lisp or Scheme in newLISP. These are different languages. Try to learn newLISP on it's own terms. The "new" in newLISP means a new mindset to programming: Lisp approached with an applications oriented scripting mindset, not with a specific computer science mindset as developed in Scheme or a compiled language mindset as in Common Lisp.



Cormullion's WikiBooks Introduction is an excellent way to learn newLISP. If you want to get serious work done in newLISP you should also read at least once through the "Users Manual and Reference". It has many examples. There is also a section grouping all functions into different problem areas. This will help you to find out, what functions are built into newLISP already.

jopython

Very informative.

Thank you Kazimir, Lutz.