From 42c02f0e4750523a53e0e7ada3661100f051edde Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 7 Jul 2018 04:59:17 +0000 Subject: net: Add (net ricketyspace sicp two five) --- net/ricketyspace/sicp/two/five.scm | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 net/ricketyspace/sicp/two/five.scm (limited to 'net/ricketyspace') diff --git a/net/ricketyspace/sicp/two/five.scm b/net/ricketyspace/sicp/two/five.scm new file mode 100644 index 0000000..4af6768 --- /dev/null +++ b/net/ricketyspace/sicp/two/five.scm @@ -0,0 +1,45 @@ +;;;; Under Creative Commons Attribution-ShareAlike 4.0 +;;;; International. See +;;;; . + +(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 -- cgit v1.2.3