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

Q&A's, tips, howto's
Locked
jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

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

Post by jopython »

In Scheme

Code: Select all

> (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.
Last edited by jopython on Fri Dec 30, 2011 4:07 am, edited 1 time in total.

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: What is the direct equivalent of for-each(Scheme) in new

Post by 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
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Re: What is the direct equivalent of for-each(Scheme) in new

Post by HPW »

With help from Lutz I got this some years ago.

Code: Select all

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

jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: What is the direct equivalent of for-each(Scheme) in new

Post by jopython »

I am getting an error for the foreach macro.

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

Code: Select all

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
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Re: What is the direct equivalent of for-each(Scheme) in new

Post by Kazimir Majorinc »

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)))))
Last edited by Kazimir Majorinc on Fri Dec 30, 2011 9:33 pm, edited 1 time in total.

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

Re: What is the direct equivalent of for-each(Scheme) in new

Post by 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
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: What is the direct equivalent of for-each(Scheme) in new

Post by jopython »

Very informative.
Thank you Kazimir, Lutz.

Locked