-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path118-pascal.rkt
More file actions
23 lines (20 loc) · 766 Bytes
/
118-pascal.rkt
File metadata and controls
23 lines (20 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#lang racket
(define (pascal-optimized previous-row)
(if (empty? previous-row)
'(1)
(let* ([middle (map +
(build-list (sub1 (length previous-row))
(lambda (i)
(+ (list-ref previous-row i)
(list-ref previous-row (add1 i))))))])
(cons 1 (append middle '(1))))))
(define/contract (generate numRows)
(-> exact-integer? (listof (listof exact-integer?)))
(let loop ([n 1]
[current '()]
[all '()])
(cond
[(> n numRows) (reverse all)]
[else
(let ([next-row (pascal-optimized current)]) (loop (add1 n) next-row (cons next-row all)))])))
(generate 5)