[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Taoufik Dachraoui

2/7/2016 6:41:00 PM

Hi

I would like to find a way to instruct CCL to convert ratios automatically as follows

if I define a variable x to be of type (ratio 1/n n/1) and set x to y than I want x to
equal to a/b such that 1<= a <= n, 1 <= b <= n and a/b is the closest to y

how to implement this in ClozureCL?

One way is to when the new value of x is not of the correct type then call a callback
functions that do the conversion; but is it possible to define a callback function
that CCL calls whenever there a type exception?

Kind regards
Taoufik
2 Answers

Pascal J. Bourguignon

2/7/2016 8:13:00 PM

0

Taoufik Dachraoui <dachraoui.taoufik@gmail.com> writes:

> I would like to find a way to instruct CCL to convert ratios automatically as follows
>
> if I define a variable x to be of type (ratio 1/n n/1) and set x to y than I want x to
> equal to a/b such that 1<= a <= n, 1 <= b <= n and a/b is the closest to y
>
> how to implement this in ClozureCL?
>
> One way is to when the new value of x is not of the correct type then call a callback
> functions that do the conversion; but is it possible to define a callback function
> that CCL calls whenever there a type exception?

It's rather easy to do that in Common Lisp, you don't need any
implementation specific features.

For this, use symbol macros. I will assume you already solved the
mathematical part of the problem (there's a trivial solution in
O(n²logn) that I'm too ashamed to publish; but it's Sunday evening):

(solve-taoufik-ratio 42 pi)
--> 22 ;
7

So:

(defstruct taoufik-variable
type
value)

(defmacro define-ratio-variable (name n)
(let ((variable (make-taoufik-variable :type n :value nil)))
`(define-symbol-macro ,name (converting-taoufik-ratio ,variable))))

(defun converting-taoufik-ratio (variable)
(taoufik-variable-value variable))

(defun (setf converting-taoufik-ratio) (new-value variable)
(multiple-value-bind (a b)
(solve-taoufik-ratio (taoufik-variable-type variable) new-value)
(setf (taoufik-variable-value variable) (/ a b))))


(define-ratio-variable x 42)
x --> nil
(setf x pi)
x --> 22/7

(define-ratio-variable y 10)
(setf y x) --> 3
y --> 3

(define-ratio-variable z 1000)
(setf z pi) --> 355/113
(setf x z) --> 22/7


Note that by your specifications:

(setf x 0) --> 1/42

and

(setf x -12) --> 1/42

since:

(solve-taoufik-ratio 42 0) --> 1 ; 42
(solve-taoufik-ratio 42 -12) --> 1 ; 42

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Taoufik Dachraoui

2/7/2016 8:35:00 PM

0

On Sunday, February 7, 2016 at 9:12:57 PM UTC+1, informatimago wrote:
> Taoufik Dachraoui <dachraoui.taoufik@gmail.com> writes:
>
> > I would like to find a way to instruct CCL to convert ratios automatically as follows
> >
> > if I define a variable x to be of type (ratio 1/n n/1) and set x to y than I want x to
> > equal to a/b such that 1<= a <= n, 1 <= b <= n and a/b is the closest to y
> >
> > how to implement this in ClozureCL?
> >
> > One way is to when the new value of x is not of the correct type then call a callback
> > functions that do the conversion; but is it possible to define a callback function
> > that CCL calls whenever there a type exception?
>
> It's rather easy to do that in Common Lisp, you don't need any
> implementation specific features.
>
> For this, use symbol macros. I will assume you already solved the
> mathematical part of the problem (there's a trivial solution in
> O(n²logn) that I'm too ashamed to publish; but it's Sunday evening):
>
> (solve-taoufik-ratio 42 pi)
> --> 22 ;
> 7
>
> So:
>
> (defstruct taoufik-variable
> type
> value)
>
> (defmacro define-ratio-variable (name n)
> (let ((variable (make-taoufik-variable :type n :value nil)))
> `(define-symbol-macro ,name (converting-taoufik-ratio ,variable))))
>
> (defun converting-taoufik-ratio (variable)
> (taoufik-variable-value variable))
>
> (defun (setf converting-taoufik-ratio) (new-value variable)
> (multiple-value-bind (a b)
> (solve-taoufik-ratio (taoufik-variable-type variable) new-value)
> (setf (taoufik-variable-value variable) (/ a b))))
>
>
> (define-ratio-variable x 42)
> x --> nil
> (setf x pi)
> x --> 22/7
>
> (define-ratio-variable y 10)
> (setf y x) --> 3
> y --> 3
>
> (define-ratio-variable z 1000)
> (setf z pi) --> 355/113
> (setf x z) --> 22/7
>
>
> Note that by your specifications:
>
> (setf x 0) --> 1/42
>
> and
>
> (setf x -12) --> 1/42
>
> since:
>
> (solve-taoufik-ratio 42 0) --> 1 ; 42
> (solve-taoufik-ratio 42 -12) --> 1 ; 42
>
> --
> __Pascal Bourguignon__ http://www.informat...
> "The factory of the future will have only two employees, a man and a
> dog. The man will be there to feed the dog. The dog will be there to
> keep the man from touching the equipment." -- Carl Bass CEO Autodesk

Of course the solution you are proposing is easy to implement but not acceptable for obvious reason.

I would like a solution that does not modify my code (or very little) and that is not silly;
so I was thinking of using handle-bind and hoping that by using appropriate type declarations
the compiler as soon as it finds a type exception calls the condition handler ;

But, my understanding, with handler-bind I need to call check-type for each variable, and this seemed
to me not acceptable (the code will be full of check-type), so I am now wondering if there is a way to
do it without calling my self check-type.

I would like to define a handler for type-error and whenever there is a type error raised by the compiler
it calls the ratio-handler I write to convert the number as appropriate.

Kind regards
Taoufik