sicp

sicp sandbox.
git clone git://git.ricketyspace.net/sicp.git
Log | Files | Refs

commit 42c02f0e4750523a53e0e7ada3661100f051edde
parent 28d92f8a32885afc0cc11967bbbbe813288fd505
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat,  7 Jul 2018 04:59:17 +0000

net: Add (net ricketyspace sicp two five)

Diffstat:
net/ricketyspace/sicp/two/five.scm | 45+++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+), 0 deletions(-)

diff --git a/net/ricketyspace/sicp/two/five.scm b/net/ricketyspace/sicp/two/five.scm @@ -0,0 +1,45 @@ +;;;; Under Creative Commons Attribution-ShareAlike 4.0 +;;;; International. See +;;;; <https://creativecommons.org/licenses/by-sa/4.0/>. + +(define-module (net ricketyspace sicp two five) + #:export (npair-cons npair-car npair-cdr)) + +(define (npair-cons a b) + (* (expt 2 a) (expt 3 b))) + +(define (npair-cdr n) + (cond ((not (= (remainder n 3) 0)) 0) + (else (1+ (npair-cdr (quotient n 3)))))) + +(define (npair-car n) + (cond ((not (= (remainder n 2) 0)) 0) + (else (1+ (npair-car (quotient n 2)))))) + +;;; Guile REPL +;;; +;;; scheme@(guile-user)> ,use (net ricketyspace sicp two five) +;;; scheme@(guile-user)> (npair-car (npair-cons 1 1)) +;;; $21 = 1 +;;; scheme@(guile-user)> (npair-cdr (npair-cons 1 1)) +;;; $22 = 1 +;;; scheme@(guile-user)> (npair-car (npair-cons 2 1)) +;;; $23 = 2 +;;; scheme@(guile-user)> (npair-cdr (npair-cons 2 1)) +;;; $24 = 1 +;;; scheme@(guile-user)> (npair-car (npair-cons 2 5)) +;;; $25 = 2 +;;; scheme@(guile-user)> (npair-cdr (npair-cons 2 5)) +;;; $26 = 5 +;;; scheme@(guile-user)> (npair-car (npair-cons 272 583)) +;;; $27 = 272 +;;; scheme@(guile-user)> (npair-cdr (npair-cons 272 583)) +;;; $28 = 583 +;;; scheme@(guile-user)> (npair-car (npair-cons 2732 5883)) +;;; $29 = 2732 +;;; scheme@(guile-user)> (npair-cdr (npair-cons 2732 5883)) +;;; $30 = 5883 +;;; scheme@(guile-user)> (npair-car (npair-cons 233 839)) +;;; $31 = 233 +;;; scheme@(guile-user)> (npair-cdr (npair-cons 233 839)) +;;; $32 = 839