SICP/util.scm

156 lines
3.8 KiB
Scheme
Raw Normal View History

2020-10-20 03:36:17 +02:00
(define (assert a b)
(cond ((equal? a b) (display "[ok]"))
(else
(display "[error] ")
(display a)
(display " != ")
(display b)))
(newline))
2021-02-01 19:02:33 +01:00
; I have this here to avoid name-conflicts with the amb implementation in
; amb.scm.
(define (my-assert a b)
(cond ((equal? a b) (display "[ok]"))
(else
(display "[error] ")
(display a)
(display " != ")
(display b)))
(newline))
2020-10-20 03:36:17 +02:00
(define (gcd a b)
(if (= b 0) (abs a) (gcd b (remainder a b))))
2020-10-24 17:24:13 +02:00
(define (average a b) (/ (+ a b) 2.0))
2020-10-26 02:40:51 +01:00
(define (id n) n)
2020-10-31 03:02:12 +01:00
(define identity id)
2020-10-26 02:40:51 +01:00
(define (inc n) (+ n 1))
2020-10-29 16:47:13 +01:00
(define nil '())
2020-10-31 03:02:12 +01:00
(define (divides? a b) (= (remainder b a) 0))
(define (cube n) (* n n n))
(define (even? n) (= (remainder n 2) 0))
(define (odd? n) (= (remainder n 2) 1))
2020-10-24 17:24:13 +02:00
2020-10-31 03:02:12 +01:00
; copied prime? from 1.21
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (+ test-divisor 1)))))
(define (smallest-divisor n)
(find-divisor n 2))
(define (prime? n) (if (= n 1) #f (= n (smallest-divisor n))))
; https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-15.html
(define (enumerate-interval low high)
(if (> low high)
nil
(cons low (enumerate-interval (+ low 1) high))))
2020-11-17 19:33:55 +01:00
2020-11-21 04:04:19 +01:00
; Returns #t if there is no #f in xs, otherwise returns #f.
(define (all? xs)
(cond ((null? xs) #t)
((eq? (car xs) #f) #f)
(else (all? (cdr xs)))))
(define (all-eq? xs)
(cond ((null? xs) #t)
((null? (cdr xs)) #t)
((eq? (car xs) (cadr xs)) (all-eq? (cdr xs)))
(else #f)))
(define (fold-right op initial sequence) ; same as accumulate
(if (null? sequence)
initial
(op (car sequence)
(fold-right op initial (cdr sequence)))))
2020-11-17 19:33:55 +01:00
2020-12-13 18:26:22 +01:00
; From exercise 3.5
(define (random-in-range low high)
(let ((range (- high low)))
(+ low (random range))))
2020-12-14 14:45:16 +01:00
(define (contains x xs)
(cond
((null? xs) #f)
((eq? x (car xs)) #t)
(else (contains x (cdr xs)))))
2021-01-01 14:40:12 +01:00
(define (display-line x)
(display x)
(newline))
2021-01-05 15:44:02 +01:00
(define (take n xs)
(if (= n 0)
'()
(cons (stream-car xs)
(take (- n 1) (stream-cdr xs)))))
2021-01-07 20:48:38 +01:00
(define (drop n xs)
(if (= n 0)
xs
(drop (- n 1) (stream-cdr xs))))
2021-01-06 12:09:20 +01:00
(define (find item stream)
(define (iter n stream)
(if (equal? (stream-car stream) item)
n
(iter (+ n 1) (stream-cdr stream))))
(iter 0 stream))
2021-01-05 15:44:02 +01:00
(define (display-stream s)
(stream-for-each display-line s))
(define (show x)
(display-line x)
x)
(define (stream-ref s n)
(if (= n 0)
(stream-car s)
(stream-ref (stream-cdr s) (- n 1))))
(define (partial-sums xs)
(cons-stream (stream-car xs)
(add-streams (partial-sums xs)
(stream-cdr xs))))
(define (scale-stream stream factor)
(stream-map (lambda (x) (* x factor)) stream))
(define (add-streams s1 s2)
(stream-map + s1 s2))
2021-01-08 12:28:39 +01:00
(define (list->stream xs)
(if (null? xs)
'()
(cons-stream (car xs) (list->stream (cdr xs)))))
2021-01-06 12:09:20 +01:00
(define ones (cons-stream 1 ones))
(define integers (cons-stream 1 (add-streams ones integers)))
(define (compile-to-file code target linkage file-name)
(set! label-counter 0)
(define (write-list-to-port xs port)
(if (null? xs)
'()
(begin
(display (car xs) port)
(display "\n" port)
(write-list-to-port (cdr xs) port))))
(if #f ; #t means write to file; #f means don't write to file
(let* ((compile-result (compile code target linkage))
(assembly-insts (statements compile-result))
(port (open-output-file file-name)))
(write-list-to-port assembly-insts port)
(display "[")
(display file-name)
(display "]\n")
(close-output-port port))
(begin
(display "[")
(display file-name)
(display "]\n"))))
2020-12-13 18:26:22 +01:00
'util-loaded