summaryrefslogtreecommitdiffstats
path: root/one
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2016-09-03 03:15:03 +0000
committerrsiddharth <s@ricketyspace.net>2016-09-03 03:15:03 +0000
commit4de900d8769fa687787464ba2cf2e050f0dfb399 (patch)
tree230d48fb1c9fb20a0b083638d1cd9778c60dfc17 /one
parent11ea72350f581ea316d5a45b39958a030b40dd7e (diff)
add one/twelve.scm
made some progress on ex. 1.12.
Diffstat (limited to 'one')
-rw-r--r--one/twelve.scm30
1 files changed, 30 insertions, 0 deletions
diff --git a/one/twelve.scm b/one/twelve.scm
new file mode 100644
index 0000000..39c3d8b
--- /dev/null
+++ b/one/twelve.scm
@@ -0,0 +1,30 @@
+;;;; Under Creative Commons Attribution-ShareAlike 4.0
+;;;; International. See
+;;;; <https://creativecommons.org/licenses/by-sa/4.0/>.
+
+;;; pascal's fucking triangle.
+
+(define-module (one twelve)
+ #:export (compute-triangle
+ compute-line
+ display-line
+ value-at))
+
+(define (compute-triangle n)
+ (cond ((= n 1) (list (compute-line n 1)))
+ (else (cons (compute-line n 1)
+ (compute-triangle (- n 1))))))
+
+(define (compute-line n pos)
+ (cond ((> pos n) '())
+ (else
+ (cons (value-at n pos)
+ (compute-line n (+ pos 1))))))
+
+(define (value-at line pos)
+ (cond ((= line 1) 1)
+ ((= pos 1) 1)
+ ((= line pos) 1)
+ (else (+
+ (value-at (- line 1) (- pos 1))
+ (value-at (- line 1) pos)))))