Is there an equivalent to flet or labels?
-
- Posts: 7
- Joined: Wed Nov 07, 2012 4:22 pm
Is there an equivalent to flet or labels?
Hi all,
Wondering if there's an equivalent to flet or labels in newLISP. I don't see any in the docs. There might be a way to hack it together with letex and lambda, but I'm not sure.
Any help?
Thanks.
Wondering if there's an equivalent to flet or labels in newLISP. I don't see any in the docs. There might be a way to hack it together with letex and lambda, but I'm not sure.
Any help?
Thanks.
Re: Is there an equivalent to flet or labels?
For example:
Then:
Code: Select all
(context 'MAIN:labels)
(define-macro (labels:labels)
(letex (_labels (append '(let)
(list (map (fn (x) (list (x 0) (append '(fn) (list (x 1)) (2 x)))) (args 0)))
(1 (args))))
_labels))
(context MAIN)
Code: Select all
D:\>newlisp -n
newLISP v.10.4.5 on Win32 IPv4/6 libffi, execute 'newlisp -h' for more info.
> labels
nil
> [cmd]
(context 'MAIN:labels)
(define-macro (labels:labels)
(letex (_labels (append '(let)
(list (map (fn (x) (list (x 0) (append '(fn) (list (x
1)) (2 x)))) (args 0)))
(1 (args))))
_labels))
(context MAIN)
(define (test-a x)
(labels ((in-test (x y) (println "test-a " x " " y)))
(in-test x "a")))
(define (test-b x)
(labels ((in-test (x y) (println "test-b " x " " y)))
(in-test "b" x)))
(define (in-test x)
(println "test-out " x))
[/cmd]
labels
(lambda-macro ()
(letex (_labels (append '(let) (list (map (lambda (x) (list (x 0) (append '(lam
bda )
(list (x 1))
(2 x))))
(args 0)))
(1 (args)))) _labels))
MAIN
(lambda (x) (labels ((in-test (x y) (println "test-a " x " " y))) (in-test x "a"
)))
(lambda (x) (labels ((in-test (x y) (println "test-b " x " " y))) (in-test "b" x
)))
(lambda (x) (println "test-out " x))
> (in-test "in-test")
test-out in-test
"in-test"
> (test-a "test-a")
test-a test-a a
"a"
> (test-b "test-b")
test-b b test-b
"test-b"
> (in-test "in-test")
test-out in-test
"in-test"
> [cmd]
(define (test-2 x y)
(labels ((test-x (x) (println "test1 x=" x))
(test-y (x) (println "test2 y=" x)))
(test-x x)
(test-y y)))
[/cmd]
(lambda (x y) (labels ((test-x (x) (println "test1 x=" x)) (test-y (x) (println
"test2 y="
x)))
(test-x x)
(test-y y)))
> (test-2 "a" "b")
test1 x=a
test2 y=b
"b"
>
-
- Posts: 7
- Joined: Wed Nov 07, 2012 4:22 pm
Re: Is there an equivalent to flet or labels?
That's some nice code.
However, I'm failing when trying to use it to write another macro based on it:
I'm getting these errors:
I'd like to be able to access "self" from a macro definition, so that I can define an anaphoric lambda macro to provide easy recursion.
:( My search continues.
However, I'm failing when trying to use it to write another macro based on it:
Code: Select all
(define-macro (alambda)
(labels ((self (args 0) (1 args)))
self))
(setf 'a (alambda (n) (if (= n 0) 1 (* n (self (- n 1))))))
(println (a 5))
Code: Select all
ERR: symbol is protected in function let : self
called from user defined function labels
called from user defined function alambda
:( My search continues.
Re: Is there an equivalent to flet or labels?
newLISP is not Common Lisp or Scheme and any intent to program in it, like you would in Common Lisp or Scheme, will end in frustration ;)
Try to solve your application problems with the functions built in to newLISP first. Not until you know newLISP's API well, write your own helpers to extend the language.
If you want to enjoy newLISP solving real world application problems, you have to program with newLISP on its own terms. A good start is to read the documentation and introductions available online:
The standard online documentation:
http://www.newlisp.org/downloads/newlisp_manual.html
http://www.newlisp.org/downloads/CodePatterns.html
Some explanation about differences between newLISP and other Lisps:
http://www.newlisp.org/index.cgi?page=D ... ther_LISPs
An introduction for beginners:
http://en.wikibooks.org/wiki/Introduction_to_newLISP
All links are from this page:
http://www.newlisp.org/index.cgi?Documentation
... which also has a list of very nice video tutorials. Some of them for advanced topics like FOOP.
Many small applications and snippets:
http://www.newlisp.org/index.cgi?Tips_and_Tricks
In any case welcome to newLISP, I hope you stay with us a bit longer :)
Try to solve your application problems with the functions built in to newLISP first. Not until you know newLISP's API well, write your own helpers to extend the language.
If you want to enjoy newLISP solving real world application problems, you have to program with newLISP on its own terms. A good start is to read the documentation and introductions available online:
The standard online documentation:
http://www.newlisp.org/downloads/newlisp_manual.html
http://www.newlisp.org/downloads/CodePatterns.html
Some explanation about differences between newLISP and other Lisps:
http://www.newlisp.org/index.cgi?page=D ... ther_LISPs
An introduction for beginners:
http://en.wikibooks.org/wiki/Introduction_to_newLISP
All links are from this page:
http://www.newlisp.org/index.cgi?Documentation
... which also has a list of very nice video tutorials. Some of them for advanced topics like FOOP.
Many small applications and snippets:
http://www.newlisp.org/index.cgi?Tips_and_Tricks
In any case welcome to newLISP, I hope you stay with us a bit longer :)
-
- Posts: 7
- Joined: Wed Nov 07, 2012 4:22 pm
Re: Is there an equivalent to flet or labels?
Thanks for all of those links, but I've read them already! :)
I'm aware that it's not CLISP or Scheme. That's why I'm attracted to it. I can't stand CLISP's bloat.
I was able to define an anaphoric if like this:
And an example:
So it's a start. I'm just exploring the idea of anaphora, and figured newLISP would be a good language to try it in.
I'm aware that it's not CLISP or Scheme. That's why I'm attracted to it. I can't stand CLISP's bloat.
I was able to define an anaphoric if like this:
Code: Select all
(define-macro (aif)
(let ((it (eval (args 0))))
(if it
(eval (args 1))
(eval (args 2)))))
Code: Select all
(aif (+ 23 42)
(println it)
(println "it's nil, bub."))
Re: Is there an equivalent to flet or labels?
self is a primitive function, then the variable name of self can't be used.
And previous labels's example cound not be used with closure.
And alambda is
Then,
By the way
I rewrote codes of 'On Lisp' to newLISP.
If you are interested,
labels
http://johu02.wordpress.com/2010/06/19/
anaphoric macro
http://johu02.wordpress.com/2010/07/29/
But, Japanese, sorry.
And previous labels's example cound not be used with closure.
Code: Select all
(context 'MAIN:labels)
(define-macro (labels:labels)
(letex (_labels (append '(begin)
(map (fn (x)
(list 'setq (x 0) (append '(fn) (list (x 1)) (2 x))))
(args 0))
(1 (args))))
_labels))
(context MAIN)
Code: Select all
(define-macro (alambda)
(letex (_funcbody (append '(_self) (list (args 0)) (1 (args))))
(labels (_funcbody)
_self)))
Code: Select all
D:\>newlisp -n
newLISP v.10.4.5 on Win32 IPv4/6 libffi, execute 'newlisp -h' for more info.
> self
self@40147C
> [cmd]
(context 'MAIN:labels)
(define-macro (labels:labels)
(letex (_labels (append '(begin)
(map (fn (x)
(list 'setq (x 0) (append '(fn) (list (x 1)) (2
x))))
(args 0))
(1 (args))))
_labels))
(context MAIN)
(define-macro (alambda)
(letex (_funcbody (append '(_self) (list (args 0)) (1 (args))))
(labels (_funcbody)
_self)))
[/cmd]
labels
(lambda-macro ()
(letex (_labels (append '(begin) (map (lambda (x) (list 'setq (x 0) (append '(l
ambda )
(list (x 1))
(2 x))))
(args 0))
(1 (args)))) _labels))
MAIN
(lambda-macro ()
(letex (_funcbody (append '(_self) (list (args 0)) (1 (args)))) (labels (_funcb
ody)
_self)))
> (set 'a (alambda (n) (if (= n 0) 1 (* n (_self (- n 1))))))
(lambda (n)
(if (= n 0)
1
(* n (_self (- n 1)))))
> (a 3)
6
>
I rewrote codes of 'On Lisp' to newLISP.
If you are interested,
labels
http://johu02.wordpress.com/2010/06/19/
anaphoric macro
http://johu02.wordpress.com/2010/07/29/
But, Japanese, sorry.
Last edited by johu on Sun Dec 23, 2012 10:39 pm, edited 1 time in total.
-
- Posts: 58
- Joined: Sat Jun 10, 2006 5:34 am
Re: Is there an equivalent to flet or labels?
Code: Select all
> (set 'a (fn (n) (if (zero? n) 1 (* n (a (- n 1))))))
(lambda (n)
(if (zero? n)
1
(* n (a (- n 1)))))
> (a 5)
120
Re: Is there an equivalent to flet or labels?
What Lutz is telling you and what William James is showing you is that you don't need constructs like flet or labels in newLISP, which only make sense (in fact, are necessary) in languages like Common Lisp where function names and variable names live in different namespaces.
This is not true in so-called Lisp-1 languages like newLISP and Scheme, so flet and labels are completely unnecessary in those languages.
Asking the question "is there something like flet or labels in newLISP?" is like walking around for years with your pockets full of coins so that you could use the public pay phones any time you needed them; and then one day you got a cell phone and asked the question, "Now that I have this cell phone -- an extra thing to put in my pockets - - what is a good way for me to continue to carry these coins?"
This is not true in so-called Lisp-1 languages like newLISP and Scheme, so flet and labels are completely unnecessary in those languages.
Asking the question "is there something like flet or labels in newLISP?" is like walking around for years with your pockets full of coins so that you could use the public pay phones any time you needed them; and then one day you got a cell phone and asked the question, "Now that I have this cell phone -- an extra thing to put in my pockets - - what is a good way for me to continue to carry these coins?"
(λx. x x) (λx. x x)