I'd like my Lisp (SBCL) to work friendly with type declarations, so
for example if I have:
(defun fib (n)
(declare (fixnum n))
(the fixnum
(if (< n 2)
1
(+ (fib (- n 1)) (fib (- n 2))))))
I would like something like:
(defun fb:fixnum (n:fixnum)
(if (< n 2)
1
(+ (fib (- n 1)) (fib (- n 2)))))
If ':' is not convenient (because it is used for packages), any other
single symbol would be nice, too (for example $ % &, etc).
Is this possible? How do I do it?
Javier wrote:
> I'd like my Lisp (SBCL) to work friendly with type declarations, so
> for example if I have:
>
> (defun fib (n)
> (declare (fixnum n))
> (the fixnum
> (if (< n 2)
> 1
> (+ (fib (- n 1)) (fib (- n 2))))))
>
> I would like something like:
>
> (defun fb:fixnum (n:fixnum)
> (if (< n 2)
> 1
> (+ (fib (- n 1)) (fib (- n 2)))))
>
> If ':' is not convenient (because it is used for packages), any other
> single symbol would be nice, too (for example $ % &, etc).
> Is this possible? How do I do it?
In the HyperSpec, there are examples how to express a
'local-type-declare' macro as part of the issue
'COMPILER-LET-CONFUSION'. (Since compiler-let is not part of ANSI CL,
that version of the macro doesn't work, of course.)
Pascal
--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
You have two issues in your code w.r.t. types
On Nov 18, 8:07 pm, Javier <·······@gmail.com> wrote:
> I'd like my Lisp (SBCL) to work friendly with type declarations, so
> for example if I have:
>
> (defun fib (n)
> (declare (fixnum n))
> (the fixnum
> (if (< n 2)
> 1
> (+ (fib (- n 1)) (fib (- n 2))))))
One thing is the DECLARE, the other is the THE form. The first is a
declaration, the second is a promise you make that the expression will
not be changed into a BIGNUM. These are different things.
>
> I would like something like:
>
> (defun fb:fixnum (n:fixnum)
> (if (< n 2)
> 1
> (+ (fib (- n 1)) (fib (- n 2)))))
>
> If ':' is not convenient (because it is used for packages), any other
> single symbol would be nice, too (for example $ % &, etc).
> Is this possible? How do I do it?
Yes, but probably not in the way you want.
One way is to have a syntax almost like defmethod
(defun (fib fixnum) ((n fixnum)) (if ...))
otherwise you can have something like
(defun fixnum fib ((n fixnum)) (if ...))
or
(defun fib => fixnum (n - fixnum) (if ...))
with a syntax gotten from McDermott's NISP and descendants.
You do all these things with macros: the DEFUN in the previous
examples is really NEW-DEFUN:DEFUN. One cute example about how to
change the syntax of surface is (shameless plug) http://common-lisp.net/project/definer
Cheers
Marco
On Nov 18, 11:07 am, Javier <·······@gmail.com> wrote:
> I would like something like:
>
> (defun fb:fixnum (n:fixnum)
> (if (< n 2)
> 1
> (+ (fib (- n 1)) (fib (- n 2)))))
You might want to have a look at the thread from November/December
2006 in this newsgroup, with the subject "A Better Syntax for Type
Declarations?"