guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Extending the numerical tower


From: Ludovic Courtès
Subject: Re: Extending the numerical tower
Date: Sun, 08 Dec 2013 00:37:39 +0100
User-agent: Gnus/5.130007 (Ma Gnus v0.7) Emacs/24.3 (gnu/linux)

Pieter Slabbert <address@hidden> skribis:

> I was wondering if it is possible to extend guile's numerical tower.
> Basically I want to try and get numbers with units such that
> (+ 100g 900g) => 1kg
> (= 1N  1kg*m/s^2) => #t
> (/ 1m 1s) => 1m/s

The syntax wouldn’t be this nice (unless you have a reader of your own),
but rather:

  (+ (g 100) (kg 1))

But then, yes, it’s possible to extend +, =, etc. with GOOPS (info
"(guile) GOOPS"), like:

  (define-class <weight> (<number>)
    (grams #:init-form #:grams))

  (define (g value)
    (make <weight> #:value g))
  (define (kg value)
    (make <weight> #:value (* 1000 g)))

  (define-method (+ (x <weight>) (y <weight>))
    (make <weight> #:value (+ (slot-ref x 'value) (slot-ref y 'value))))

Or you could avoid GOOPS altogether and use a specific ‘weight+’
procedure instead of the ‘+’ generic function.

Thanks,
Ludo’.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]