3 ms·
The code compiles and runs just fine on any CL compiler. Try SBCL or CLozure CL. Actually the loop macro in CL can be a very contentious topic. I'd hardly refe
by jgrant27 18y ago
The code compiles and runs just fine on any CL compiler. Try SBCL or CLozure CL.
Actually the loop macro in CL can be a very contentious topic. I'd hardly refer to the loop macro as idiomatic.
Imperative and functional styles in CL can both be idiomatic.
Anyway who said CL was Scheme ?
- jrockway 18y agoAnyway who said CL was Scheme ? Your style strongly hints at a Scheme background. Nothing you do is wrong, but if you read a bunch of Lisp code, you wouldn't see many people doing things the way you did. OTOH, if you read a bunch of Scheme code, it would look exactly like yours. Also, I don't think the use of loop is a point of contention. LOOP is in the CL spec, tail recursion isn't. (The ITERATE package is a nicer loop, but the idea is the same.)
- jgrant27 18y agoInteresting opinion regarding my style. Thanks for explaining. There are many things in the CL spec that are very contentious. Arguably too many. Dan Weinreb pointed this out earlier in the week(http://ilc2009.scheming.org/node/7 http://ilc2009.scheming.org/node/7) which inspired a response from me(http://jng.imagine27.com/articles/2009-04-07-135246_cl_hard_to_compile_efficiently.html http://jng.imagine27.com/articles/2009-04-07-135246_cl_hard_...) although not directly related to the "too many concepts, irregular" argument but then again this may effect how per formant/stable code can be written in a language. Yes tail-recursion is not part of the spec along with many other implementation aspects that probably should be.
- mahmud 18y agoGood on you taking criticism so well jgrant27, testament to a mature mind. Also, absence of tail-call elimination in the spec doesn't have to stop you from using it, if your compiler supports it. We all love Common Lisp, but usually not "warts and all"; we all end up using our personally "attractive" subset. I personally code in Dylan with s-exp notation that happens to compile under SBCL :-)
- jgrant27 18y agoThere's recursion and then there's tail recursion. Recursion is idiomatic of all functional languages. Using the loop macro as in your example is iterative and hence some of the contention about whether that is idiomatic for a functional language such as CL. Some would say that this is a a purist argument but some things are actually easier to write recursively.
- tc 18y agoI like the tail recursion, but it really would be more idiomatic in CL to write: (defun integrate (f a b &aux (dx *small-dx*)) (labels ((rec (x sum) (if (> x b) sum (rec (+ x dx) (+ sum (* (funcall f x) dx)))))) (rec a 0)))
- jgrant27 18y agoAbsolutely not. Nested functions are LEXICALLY scoped in CL. Why the aversion ?
- jrockway 18y agoThere is nothing wrong with your code, it's just that nobody else does it that way. Personally, I find code easier to read when it looks something like what I expect... but OTOH, it's very obvious what your code does too.
- sketerpot 18y agoYou know what? Screw the loop debate. The loop version of the integrate function is shorter and easier to understand. So what's wrong with it?
- jgrant27 18y agoNothing at all. The loop macro version is iterative and the original is recursive. However some things are easier to represent recursively than iteratively. That is if the programmer clearly understands recursion.