Re: Is it possible to implement break or return in Lisp?
On Tue, Nov 3, 2009 at 10:58 PM, rob r.p.l...@gmail.com wrote: I'm pretty sure there was an example of this using continuations in Dybvig's book on Scheme. I just flipped through it and didn't readily find it, but I think that is where I saw it. You can do something like this (PLT Scheme): #lang scheme (define (foo n) (define (foo-helper return) (if ( n 0) (return n) (* -1 n))) (call/cc foo-helper)) (display (foo 10)) -- Chris Wilson christopher.j.wil...@gmail.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Is it possible to implement break or return in Lisp?
On Nov 3, 12:03 am, CuppoJava patrickli_2...@hotmail.com wrote: But I'm writing a DSL for others to use. People that don't have experience with functional programming, and for them it's easier to have a break/return. That doesn't seem like a good idea to me. Clojure is a functional programming language; trying to treat it as an procedural language is a bad habit to get into, and there are many hidden gotchas with things like lazy evaluation of sequences. - James --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Is it possible to implement break or return in Lisp?
I'm pretty sure there was an example of this using continuations in Dybvig's book on Scheme. I just flipped through it and didn't readily find it, but I think that is where I saw it. On Nov 1, 8:04 pm, CuppoJava patrickli_2...@hotmail.com wrote: Hi, For the purposes of a DSL that I'm writing, it would be very convenient to have a break/return statement that early exits from a subroutine. Is it possible to implement this in Clojure? or any Lisp-like language? I've given it some thought and it seems it has to be a compiler level feature. Am I right? Thanks -Patrick --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Is it possible to implement break or return in Lisp?
Could you do this w/ a lazy seq cond? The seq terminates early if cond x is met. On Nov 1, 8:04 pm, CuppoJava patrickli_2...@hotmail.com wrote: Hi, For the purposes of a DSL that I'm writing, it would be very convenient to have a break/return statement that early exits from a subroutine. Is it possible to implement this in Clojure? or any Lisp-like language? I've given it some thought and it seems it has to be a compiler level feature. Am I right? Thanks -Patrick --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Is it possible to implement break or return in Lisp?
On Sun, Nov 1, 2009 at 8:04 PM, CuppoJava patrickli_2...@hotmail.comwrote: Hi, For the purposes of a DSL that I'm writing, it would be very convenient to have a break/return statement that early exits from a subroutine. I'm not sure why you need this. The body of a function in clojure isn't a series of statements, it's an expression. do is then just an operator that evaluates it's various arguments and returns the last one- like + is an operator that evaluates it's various arguments and returns the sum of them. So at any point you can stop and return a value, because it's an expression- you don't need a special statement for that. So, for example, you might write code like: (defn example [ x y ] (do (foo x) ; This is always done (if exit-early 3 ; if we want to stop early, we return this value (do (bar y) 4 A similar thing happens with loop. The default is exiting immediately- rather than having a special form to exit the loop, you have a special form to continue the loop (recur). So there is no value to having a break or return- just don't recur. Brian --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Is it possible to implement break or return in Lisp?
Thanks Brian. For my own purposes, yes I have no need for a break or return statement. But I'm writing a DSL for others to use. People that don't have experience with functional programming, and for them it's easier to have a break/return. And for me, it's easier to implement break/return using an exception than it is to transform the DSL into appropriately nested if's. -Patrick --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Is it possible to implement break or return in Lisp?
On Nov 3, 2:03 am, CuppoJava patrickli_2...@hotmail.com wrote: Thanks Brian. For my own purposes, yes I have no need for a break or return statement. But I'm writing a DSL for others to use. People that don't have experience with functional programming, and for them it's easier to have a break/return. And for me, it's easier to implement break/return using an exception than it is to transform the DSL into appropriately nested if's. You might find http://blogs.sun.com/jrose/entry/longjumps_considered_inexpensive interesting. It's about making exception-throwing quick. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Is it possible to implement break or return in Lisp?
Hi, For the purposes of a DSL that I'm writing, it would be very convenient to have a break/return statement that early exits from a subroutine. Is it possible to implement this in Clojure? or any Lisp-like language? I've given it some thought and it seems it has to be a compiler level feature. Am I right? Thanks -Patrick --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Is it possible to implement break or return in Lisp?
I've given it some thought and it seems it has to be a compiler level feature. Am I right? You could implement Common Lisp-style return-from with a custom exception class: public class BlockException extends Exception { String name; Object val; public BlockException(String name, Object val) { this.name = name; this.val = val; } public String name() { return this.name; } public Object value() { return this.val; } } (defmacro block [name body] `(try (do ,@body) (catch BlockException e (if (= (.name e) (name ,name)) (.value e) (throw e) (defmacro return-from [name expr] `(throw (BlockException. (name ,name) ,expr))) Then use it: (defn foo [] (block outside (do-something ;; ... (return-from outside 5) ;; ... ))) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Is it possible to implement break or return in Lisp?
On Sun, Nov 1, 2009 at 9:14 PM, Richard Newman holyg...@gmail.com wrote: I've given it some thought and it seems it has to be a compiler level feature. Am I right? You could implement Common Lisp-style return-from with a custom exception class: public class BlockException extends Exception { String name; Object val; public BlockException(String name, Object val) { this.name = name; this.val = val; } public String name() { return this.name; } public Object value() { return this.val; } } (defmacro block [name body] `(try (do ,@body) (catch BlockException e (if (= (.name e) (name ,name)) (.value e) (throw e) (defmacro return-from [name expr] `(throw (BlockException. (name ,name) ,expr))) Then use it: (defn foo [] (block outside (do-something ;; ... (return-from outside 5) ;; ... ))) For this to work, you'd first have to implement Common Lisp style unquoting. :) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Is it possible to implement break or return in Lisp?
Thanks for the pointer! I haven't considered using Exceptions to influence the control-flow. This will work perfectly. -Patrick PS: I've always found it a little strange for exception-handling to be treated as a special case in language design. It seems like it ought able to be expressed in terms of some simpler elements (eg. a function return). Is there a book or article that discusses this? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Is it possible to implement break or return in Lisp?
Maybe could be done using continuation passing style? (http:// en.wikipedia.org/wiki/Continuation-passing_style) On Nov 1, 7:04 pm, CuppoJava patrickli_2...@hotmail.com wrote: Hi, For the purposes of a DSL that I'm writing, it would be very convenient to have a break/return statement that early exits from a subroutine. Is it possible to implement this in Clojure? or any Lisp-like language? I've given it some thought and it seems it has to be a compiler level feature. Am I right? Thanks -Patrick --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Is it possible to implement break or return in Lisp?
CuppoJava patrickli_2...@hotmail.com writes: I've always found it a little strange for exception-handling to be treated as a special case in language design. It seems like it ought able to be expressed in terms of some simpler elements (eg. a function Note sure exactly what you want, but you could try Lisp in Small Pieces by Christian Queinnec. If I remember right he talks about exceptions in terms of continuations. Or you could just start reading up on continuations if you're not familiar with them. Wikipedia has a call/cc page at http://en.wikipedia.org/wiki/Call-with-current-continuation Cheers, Chris Dean --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Is it possible to implement break or return in Lisp?
For this to work, you'd first have to implement Common Lisp style unquoting. :) Heh, good catch :) I've been switching between the two too much recently! At least I didn't miss out the semicolons in the Java code ;) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Is it possible to implement break or return in Lisp?
On Mon, Nov 2, 2009 at 12:19 AM, Richard Newman holyg...@gmail.com wrote: For this to work, you'd first have to implement Common Lisp style unquoting. :) Heh, good catch :) I've been switching between the two too much recently! At least I didn't miss out the semicolons in the Java code ;) I'd be more worried about the parentheses, or switching the order of the object and method. When I need to drop down to Java it always seems like for ... is naked without an open parenthesis first. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---