Thomas Baruchel <········@127.0.0.1> writes:
> http://baruchel.thomas.free.fr/code/ogf.lsp
Don't use ';' for stand alone comments.
Lisp Editors follow this convention:
;;;; section comment
;;; left-margin comment
(progn
;; block comment
(some code) ; code comment
;; some more block comment
(some more)) ; some more code comment
SETQ cannot define variables. Use DEFVAR or DEFPARAMETER
The convention for constants is to use '+': +polynomial-null+
Use DEFCONSTANT for constants.
That said, for anything but characters or numebers, using DEFCONSTANT
is cumbersome because you must ensure the constancy between
compilation, load and run times. For this reason, I usually write
things like:
(defparameter +polynomial-null+ '((0 0)))
The right indentation for IF is:
(IF condition
then
else)
For small and simple branches: (IF condition then else)
Never: (IF condition then
else)
this is confusing!
Instead of writing:
(if (null p) nil
...)
write:
(when p
...)
Or if you really want to distinguish lists from boolean:
(unless (null p)
...)
Also, in a case such as:
(if (null f) nil
(polynomial-fraction-normalize f))
I'd write:
(and f (polynomial-fraction-normalize f))
If you want the last, use (car (last p)) instead of (reverse p).
So write: (second (car (last p))), or (cadar (last p))
instead of: (cadar (reverse p))
> (apply 'lcm (mapcar (lambda (x) (denominator (car x))) a))
There's a difference between (quote fun) and (function fun):
(defun fun () (print :glob-fun))
(flet ((fun () (print :local-fun)))
(funcall (function fun))
(funcall (quote fun))
(values))
prints:
:LOCAL-FUN
:GLOB-FUN
In general, we use (function fun).
When you have lists of variable size, if you cannot prove they're less
than 50 in length, you must use REDUCE.
(reduce (function lcm) (mapcar (lambda (x) (denominator (car x))) a))
If you expect to get a performace improvment passing several arguments
at once, you could write:
(let ((args (mapcar (lambda (x) (denominator (car x))) a)))
(if (< (length args) call-arguments-limit)
(apply (function lcm) args)
(reduce (function lcm) args)))
In addition, when using REDUCE, you can collapse the MAPCAR (avoiding
consing the result of mapcar):
(reduce (lambda (r x) (lcm r (denomiator (car x)))) a :initial-value 1)
> (lambda (x) (cadr x))
This is equivalent to: (function cadr)
or: (function second)
--
__Pascal Bourguignon__ http://www.informatimago.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d? s++:++ a+ C+++ UL++++ P--- L+++ E+++ W++ N+++ o-- K- w---
O- M++ V PS PE++ Y++ PGP t+ 5+ X++ R !tv b+++ DI++++ D++
G e+++ h+ r-- z?
------END GEEK CODE BLOCK------
On Sun, 09 Oct 2005 02:20:49 +0200, <····@mouse-potato.com> wrote:
>
> Don't use ';' for stand alone comments.
>
> Lisp Editors follow this convention:
>
> ;;;; section comment
>
> ;;; left-margin comment
>
> (progn
> ;; block comment
> (some code) ; code comment
> ;; some more block comment
> (some more)) ; some more code comment
Pascal, I just looked over cliki for a style reference similar to your
post. I found nothing, perhaps you could add these suggestions
(rules) to cliki?
--
If you don't like LOOP, how do you feel about DOLIST ?
From: Surendra Singhi
Subject: Re: Could you have a look at my code (arithmetical stuff) ?
Date:
Message-ID: <achjfbvc.fsf@netscape.net>
GP lisper <········@CloudDancer.com> writes:
> On Sun, 09 Oct 2005 02:20:49 +0200, <····@mouse-potato.com> wrote:
>>
>> Don't use ';' for stand alone comments.
>>
>> Lisp Editors follow this convention:
>>
>> ;;;; section comment
>>
>> ;;; left-margin comment
>>
>> (progn
>> ;; block comment
>> (some code) ; code comment
>> ;; some more block comment
>> (some more)) ; some more code comment
>
> Pascal, I just looked over cliki for a style reference similar to your
> post. I found nothing, perhaps you could add these suggestions
> (rules) to cliki?
>
Check the lisp style at the link given below:
http://www.lisp.org/table/style.htm
--
Surendra Singhi
http://www.public.asu.edu/~sksinghi/index.html
,----
| "O thou my friend! The prosperity of Crime is like unto the lightning,
| whose traitorous brilliancies embellish the atmosphere but for an
| instant, in order to hurl into death's very depths the luckless one
| they have dazzled." -- Marquis de Sade
`----
Surendra Singhi <·········@netscape.net> wrote:
+---------------
| GP lisper <········@CloudDancer.com> writes:
| > Pascal, I just looked over cliki for a style reference similar to your
| > post. I found nothing, perhaps you could add these suggestions
| > (rules) to cliki?
|
| Check the lisp style at the link given below:
| http://www.lisp.org/table/style.htm
+---------------
Or even the CLHS itself, 2.4.4.2 "Notes about Style for Semicolon":
http://www.lispworks.com/documentation/HyperSpec/Body/02_ddb.htm
-Rob
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607
On Sun, 09 Oct 2005 04:33:25 -0500, <····@rpw3.org> wrote:
> Surendra Singhi <·········@netscape.net> wrote:
> +---------------
>| GP lisper <········@CloudDancer.com> writes:
>| > Pascal, I just looked over cliki for a style reference similar to your
>| > post. I found nothing, perhaps you could add these suggestions
>| > (rules) to cliki?
>|
>| Check the lisp style at the link given below:
>| http://www.lisp.org/table/style.htm
> +---------------
>
> Or even the CLHS itself, 2.4.4.2 "Notes about Style for Semicolon":
>
> http://www.lispworks.com/documentation/HyperSpec/Body/02_ddb.htm
I knew that Common Lisp was the ultimate language, but an ANSI
specification for comments? Is there a regression test for it? Which
CL source is compliant? ;-)
But Pascals other comments about style really didn't show up in the
suggested links.
--
If you don't like LOOP, how do you feel about DOLIST ?
On Sun, 09 Oct 2005 02:20:49 +0200, <····@mouse-potato.com> wrote:
>
>
> Thomas Baruchel <········@127.0.0.1> writes:
>> http://baruchel.thomas.free.fr/code/ogf.lsp
>
> Don't use ';' for stand alone comments.
>
> Lisp Editors follow this convention:
>
> ;;;; section comment
>
> ;;; left-margin comment
>
> (progn
> ;; block comment
> (some code) ; code comment
> ;; some more block comment
> (some more)) ; some more code comment
>
>
> SETQ cannot define variables. Use DEFVAR or DEFPARAMETER
> The convention for constants is to use '+': +polynomial-null+
> Use DEFCONSTANT for constants.
>
> That said, for anything but characters or numebers, using DEFCONSTANT
> is cumbersome because you must ensure the constancy between
> compilation, load and run times. For this reason, I usually write
> things like:
>
> (defparameter +polynomial-null+ '((0 0)))
>
>
> The right indentation for IF is:
>
> (IF condition
> then
> else)
>
>
> For small and simple branches: (IF condition then else)
>
> Never: (IF condition then
> else)
> this is confusing!
>
>
> Instead of writing:
> (if (null p) nil
> ...)
> write:
> (when p
> ...)
> Or if you really want to distinguish lists from boolean:
> (unless (null p)
> ...)
>
> Also, in a case such as:
> (if (null f) nil
> (polynomial-fraction-normalize f))
> I'd write:
> (and f (polynomial-fraction-normalize f))
>
>
> If you want the last, use (car (last p)) instead of (reverse p).
> So write: (second (car (last p))), or (cadar (last p))
> instead of: (cadar (reverse p))
>
>
>> (apply 'lcm (mapcar (lambda (x) (denominator (car x))) a))
>
> There's a difference between (quote fun) and (function fun):
>
> (defun fun () (print :glob-fun))
> (flet ((fun () (print :local-fun)))
> (funcall (function fun))
> (funcall (quote fun))
> (values))
>
> prints:
>
>:LOCAL-FUN
>:GLOB-FUN
>
> In general, we use (function fun).
>
>
>
> When you have lists of variable size, if you cannot prove they're less
> than 50 in length, you must use REDUCE.
>
> (reduce (function lcm) (mapcar (lambda (x) (denominator (car x))) a))
>
> If you expect to get a performace improvment passing several arguments
> at once, you could write:
>
> (let ((args (mapcar (lambda (x) (denominator (car x))) a)))
> (if (< (length args) call-arguments-limit)
> (apply (function lcm) args)
> (reduce (function lcm) args)))
>
>
> In addition, when using REDUCE, you can collapse the MAPCAR (avoiding
> consing the result of mapcar):
>
> (reduce (lambda (r x) (lcm r (denomiator (car x)))) a :initial-value 1)
>
>
>> (lambda (x) (cadr x))
>
> This is equivalent to: (function cadr)
> or: (function second)
>
>
>
--
If you don't like LOOP, how do you feel about DOLIST ?
Besides Pascal's comments, there's also the following.
A consistent pattern in the code is
(defun foo-bar0 ....)
(defun foo-bar
(.... (foo-bar0....)))
You might want to use LABELS here to show that these foo-bar0
functions are really only used as part of the foo-bar functions.
(defun polynomial-make (l)
(labels ((polynomial-make0 (l n)
(and l
(let ((a (car l)))
(if (zerop a)
(polynomial-make0 (cdr l) (+ n 1))
(cons (list a n)
(polynomial-make0 (cdr l) (+ n 1))))))))
(or (polynomial-make0 l 0) polynomial-null)))
Note also that along the lines of Pascal's comment about using AND
instead of IF, I replaced code like
(let ((d (polynomial-diff0 p)))
(if (null d) polynomial-null d)))
with simply
(or (polynomial-make0 l 0) polynomial-null)
--
Fred Gilham ······@csl.sri.com
In Nashville there ain't no money above the third fret. -- Jay Carlson
Le 09-10-2005, Pascal Bourguignon <····@mouse-potato.com> a �crit :
> Instead of writing:
> (if (null p) nil
> ...)
> write:
> (when p
> ...)
>
> Also, in a case such as:
> (if (null f) nil
> (polynomial-fraction-normalize f))
> I'd write:
> (and f (polynomial-fraction-normalize f))
Nice, but in this case, why not also use the lase form
(and f ...) for replacing all the (if (null f) nil ...)
instead of using (when f ...) for all other cases
(except the one you distinguish) ?
--
Thomas Baruchel
write to baruchel at the host called bluebottle dot com
�crire � baruchel chez l'h�te nomm� bluebottle point com
http://baruchel.thomas.free.fr/