unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Simulating rational number addition/subtraction
@ 2016-02-24 22:27 Lawrence Bottorff
  2016-02-24 23:32 ` dsmich
  2016-02-25  6:52 ` Jan Wedekind
  0 siblings, 2 replies; 5+ messages in thread
From: Lawrence Bottorff @ 2016-02-24 22:27 UTC (permalink / raw
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 760 bytes --]

With guile (and a few other Schemes/Lisps) you can directly add fractions
or rational numbers:

(+ 1/2 1/12)
=> 7/12

It's like magic, no? Anyway, I'm trying -- for educational purposes -- to
reproduce this functionality. But right out of the blocks I'm stuck trying
to figure out how to handle incoming parameters. So far I've got

(define (myrat a b)
   . . .)

(define (myrat a b c d)
   . . .)

where numerator and denominator are given separately, as well as

(define (myrat . fracparams)
   . . .)

which still has my basic problem, namely, can one parameter hold both parts
of a rational number, then allow the numerator and denominator to be peeled
off and worked on, i.e., something like

(let ((num1 numepeeler(a))
       (denom1 denompeeler(a)


LB

[-- Attachment #2: Type: text/html, Size: 1097 bytes --]

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

* Re: Simulating rational number addition/subtraction
  2016-02-24 22:27 Simulating rational number addition/subtraction Lawrence Bottorff
@ 2016-02-24 23:32 ` dsmich
  2016-02-25  7:07   ` tomas
  2016-02-25  6:52 ` Jan Wedekind
  1 sibling, 1 reply; 5+ messages in thread
From: dsmich @ 2016-02-24 23:32 UTC (permalink / raw
  To: guile-user, Lawrence Bottorff


---- Lawrence Bottorff <borgauf@gmail.com> wrote: 
> With guile (and a few other Schemes/Lisps) you can directly add fractions
> or rational numbers:
> 
> (+ 1/2 1/12)
> => 7/12
> 
> It's like magic, no? Anyway, I'm trying -- for educational purposes -- to
> reproduce this functionality. But right out of the blocks I'm stuck trying
> to figure out how to handle incoming parameters. So far I've got
> 
> (define (myrat a b)
>    . . .)
> 
> (define (myrat a b c d)
>    . . .)
> 
> where numerator and denominator are given separately, as well as
> 
> (define (myrat . fracparams)
>    . . .)
> 
> which still has my basic problem, namely, can one parameter hold both parts
> of a rational number, then allow the numerator and denominator to be peeled
> off and worked on, i.e., something like
> 
> (let ((num1 numepeeler(a))
>        (denom1 denompeeler(a)
> 
> 
> LB

This https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-14.html might be enlightening....

-Dale





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

* Re: Simulating rational number addition/subtraction
  2016-02-24 22:27 Simulating rational number addition/subtraction Lawrence Bottorff
  2016-02-24 23:32 ` dsmich
@ 2016-02-25  6:52 ` Jan Wedekind
  1 sibling, 0 replies; 5+ messages in thread
From: Jan Wedekind @ 2016-02-25  6:52 UTC (permalink / raw
  To: Lawrence Bottorff, guile-user

On 24. Februar 2016 22:27:45 GMT+00:00, Lawrence Bottorff <borgauf@gmail.com> wrote:
>With guile (and a few other Schemes/Lisps) you can directly add
>fractions
>or rational numbers:
>
>(+ 1/2 1/12)
>=> 7/12
>
>It's like magic, no? Anyway, I'm trying -- for educational purposes --
>to
>reproduce this functionality. But right out of the blocks I'm stuck
>trying
>to figure out how to handle incoming parameters. So far I've got
>
>(define (myrat a b)
>   . . .)
>
>(define (myrat a b c d)
>   . . .)
>
>where numerator and denominator are given separately, as well as
>
>(define (myrat . fracparams)
>   . . .)
>
>which still has my basic problem, namely, can one parameter hold both
>parts
>of a rational number, then allow the numerator and denominator to be
>peeled
>off and worked on, i.e., something like
>
>(let ((num1 numepeeler(a))
>       (denom1 denompeeler(a)
>
>
>LB

You can use GOOPS (*) to represent the rational numbers.

(*) http://www.gnu.org/software/guile/manual/html_node/GOOPS.html
-- 
Jan Wedekind
http://www.wedesoft.de/



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

* Re: Simulating rational number addition/subtraction
  2016-02-24 23:32 ` dsmich
@ 2016-02-25  7:07   ` tomas
  2016-02-26 13:01     ` Alex Vong
  0 siblings, 1 reply; 5+ messages in thread
From: tomas @ 2016-02-25  7:07 UTC (permalink / raw
  To: guile-user

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, Feb 24, 2016 at 06:32:12PM -0500, dsmich@roadrunner.com wrote:

[representing rationals]

> This https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-14.html might be enlightening....

Yes. "Structure and Interpretation of Computer Programs" (here affectionately
known as "SICP") *very* *warmly* recommended.

- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlbOqB0ACgkQBcgs9XrR2kYIrQCfXjldGxHGVXiOQj362462imsr
AK0An0FK+sM4809IgXx/8luGVgGDBPIm
=243s
-----END PGP SIGNATURE-----



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

* Re: Simulating rational number addition/subtraction
  2016-02-25  7:07   ` tomas
@ 2016-02-26 13:01     ` Alex Vong
  0 siblings, 0 replies; 5+ messages in thread
From: Alex Vong @ 2016-02-26 13:01 UTC (permalink / raw
  To: tomas; +Cc: guile-user

<tomas@tuxteam.de> writes:

> On Wed, Feb 24, 2016 at 06:32:12PM -0500, dsmich@roadrunner.com wrote:
>
> [representing rationals]
>
>> This https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-14.html
>> might be enlightening....

There are also video lectures recorded in 1986 in addition to the book
<http://cynic.cc/blog/posts/2015-01-17_uploading_sicp_to_youtube/>.
Lecture 2B talks about implementing rational numbers using CONS, CAR and
CDR as an example of data abstraction. Then, to "terrify" you by showing
CONS, CAR and CDR can be built from "air". Watch the lecture or the book
(the link of enlightenment given by Dale) to find out what does it mean
:)

>
> Yes. "Structure and Interpretation of Computer Programs" (here affectionately
> known as "SICP") *very* *warmly* recommended.
>
> -- t



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

end of thread, other threads:[~2016-02-26 13:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-24 22:27 Simulating rational number addition/subtraction Lawrence Bottorff
2016-02-24 23:32 ` dsmich
2016-02-25  7:07   ` tomas
2016-02-26 13:01     ` Alex Vong
2016-02-25  6:52 ` Jan Wedekind

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).