-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexample.scm
70 lines (54 loc) · 2.44 KB
/
example.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
; Created by xrlin
; Usage: (load "example") or (load "example.scm")
; This example file contains the procedures to calculate Fibonacci number, calculate and display pascal triangle number.
(define (fib n)
(if (<= n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))
(define (fib2 n)
(begin (define (fib-iter a b n)
(if (= n 0) b (fib-iter b (+ a b) (- n 1))))
(fib-iter 0 1 (- n 1))))
(define (display-pascal n)
(begin
(define lst '())))
(define (add-num a b)
(cond
((null? a) b)
((null? b) a)
(else (+ a b))))
(define (calc-pascal lst k)
(if (<= k 0) lst
(begin (list-set! lst k (add-num (list-ref lst (- k 1)) (list-ref lst k)))
(calc-pascal lst (- k 1)))))
(define (generate-pascal-helper sequence k n)
(cond ((= k n) sequence)
(else (generate-pascal-helper (append (calc-pascal sequence (- k 1)) 1) (+ k 1) n))))
(define (generate-pascal n)
(generate-pascal-helper '() 0 n))
(define (display-pascal-helper sequence current-level max-level width)
(begin
(display-pascal-indents (- max-level current-level) width)
(display-pascal-sequence sequence width)))
(define (display-blank n)
(if (> n 0) (display " ")))
(define (display-tab n)
(if (> n 0) (begin (display "\t") (display-tab (- n 1)))))
(define (display-pascal-indents indent-count width)
(if (> indent-count 0) (begin (display-tab 1) (display-pascal-indents (- indent-count 1) width))))
(define (display-pascal-sequence sequence width)
(if (null? sequence) (displayln "") (begin (display-pascal-num (car sequence) width) (display-pascal-sequence (cdr sequence) width))))
(define (max-pascal-number level)
(reduce (lambda (x y) (if (> x y) x y)) (generate-pascal level)))
(define (number-char-width num)
(begin
(define (number-width num)
(if (< num 10) 1 (+ (number-width (/ num 10)) 1)))
(number-width num)))
(define (display-pascal-num num width)
(if (<= (number-char-width num) width)
(begin (display num) (display-blank (- width (number-char-width num))) (display-tab 2))))
(define (display-pascal-triangle-helper start-level max-level char-width)
(if (<= start-level max-level) (begin
(display-pascal-helper (generate-pascal start-level) start-level max-level char-width)
(display-pascal-triangle-helper (+ start-level 1) max-level char-width))))
(define (display-pascal-triangle start-level max-level)
(display-pascal-triangle-helper start-level max-level (number-char-width (max-pascal-number max-level))))