unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Extending the numerical tower
@ 2013-12-07 20:06 Pieter Slabbert
  2013-12-07 23:37 ` Ludovic Courtès
  0 siblings, 1 reply; 2+ messages in thread
From: Pieter Slabbert @ 2013-12-07 20:06 UTC (permalink / raw)
  To: guile-user

Hi

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

Regards
Pieter



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Extending the numerical tower
  2013-12-07 20:06 Extending the numerical tower Pieter Slabbert
@ 2013-12-07 23:37 ` Ludovic Courtès
  0 siblings, 0 replies; 2+ messages in thread
From: Ludovic Courtès @ 2013-12-07 23:37 UTC (permalink / raw)
  To: guile-user

Pieter Slabbert <blob626@gmail.com> 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’.




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-12-07 23:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-07 20:06 Extending the numerical tower Pieter Slabbert
2013-12-07 23:37 ` Ludovic Courtès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).