Skip to content

Instantly share code, notes, and snippets.

View tetsu-miyagawa's full-sized avatar

Tetsu Miyagawa tetsu-miyagawa

View GitHub Profile
@tetsu-miyagawa
tetsu-miyagawa / CTMCP-Section2.9-4-a-1.oz
Created November 17, 2015 14:08
CTMCP Section 2.9 Exercise 4 (a) 1
local X Y T in
X=1
Y=1
T=X==Y
case T of true then {Browse true} else {Browse false} end
end
@tetsu-miyagawa
tetsu-miyagawa / CTMCP-Section2.9-2-2.oz
Created November 16, 2015 23:14
CTMCP Section 2.9 Exercise 2 (2)
local MulByN A B N in
local N in
N=3
proc {MulByN X ?Y}
Y=N*X
end
end
A=4
N=5
{Browse N}
@tetsu-miyagawa
tetsu-miyagawa / CTMCP-Section2.9-2-1.oz
Created November 16, 2015 23:12
CTMCP Section 2.9 Exercise 2 (1)
local MulByN A B in
local N in
N=3
proc {MulByN X ?Y}
Y=N*X
end
end
A=4
{Browse N}
{MulByN A B}
@tetsu-miyagawa
tetsu-miyagawa / CTMCP-Section2.9-8b.oz
Last active November 16, 2015 23:05
CTMCP Section 2.9 Exercise 8 (b)
declare OrElse
fun {OrElse BP1 BP2}
if {BP1} then true
else {BP2}
end
end
{Browse {OrElse fun {$} 1==1 end fun {$} 2==3 end}}
{Browse {OrElse fun {$} 1==2 end fun {$} 2==2 end}}
{Browse {OrElse fun {$} 1==2 end fun {$} 2==3 end}}
@tetsu-miyagawa
tetsu-miyagawa / CTMCP-Section2.9-9a.oz
Created November 16, 2015 23:00
CTMCP Section 2.9 Exercise 9 (a)
declare Sum1 Sum2
Sum1 = proc {$ N ?R}
local T in
T=N==0
if T then R=0
else
local S M in
S=N-1
{Sum1 S M}
R=N+M
@tetsu-miyagawa
tetsu-miyagawa / SICP Exercise 1.22 odd-sequence.scm
Created July 2, 2015 23:05
SICP Exercise 1.22 odd-sequence
(define (odd-sequence min max)
(cond ((even? min) (odd-sequence (+ 1 min) max))
((> min max) '())
(else (cons min (odd-sequence (+ 2 min) max)))))
(define (search-for-primes min max)
(map timed-prime-test (odd-sequence min max))
(newline))
@tetsu-miyagawa
tetsu-miyagawa / SICP Exercise 1.22 runtime.scm
Created July 2, 2015 23:04
SICP Exercise 1.22 runtime
(define (runtime) (current-milliseconds))
@tetsu-miyagawa
tetsu-miyagawa / SICP Execise 1.22 start-prime-test.scm
Created July 2, 2015 23:04
SICP Execise 1.22 start-prime-test
(define (start-prime-test n start-time)
(if (prime? n)
(report-prime (- (runtime) start-time))
#f))
(define (fib n)
(fib-iter 1 0 0 1 n))
(define (fib-iter a b p q count)
(cond ((= count 0) b)
((even? count)
(fib-iter a
b
(+ (* p p) (* q q)) ; compute p'
(+ (* q q) (* 2 p q)) ; compute q'
(/ count 2)))
(define (fast-multi a b)
(define (double n) (+ n n))
(define (halve n) (/ n 2))
(define (even? n) (= (remainder n 2) 0))
(define (iter a b x)
(cond ((or (= a 0) (= b 0)) 0)
((= b 1) (+ a x))
((even? b) (iter (double a) (halve b) x))
(else (iter a (- b 1) (+ a x)))))
(iter a b 0))