## 1 Scheme

### 1.1 Getting started

```brew install chezscheme guile
```

### 1.2 Pairs

```(define (sh/wal/pair f r)
(lambda (op)
(op f r)))

(define (sh/wal/first p)
(p (lambda (f r)
f)))

(define (sh/wal/rest p)
(p (lambda (f r)
r)))

(sh/wal/first
(sh/wal/rest
(sh/wal/pair
1
(sh/wal/pair
2
(sh/wal/pair 3 0)))))
```
```const pair = (f, r) => (op) => op(f, r);
const first = (p) => p((f, r) => f);
const rest = (p) => p((f, r) => r);

console.log(first(pair(1, 2)));
```

### 1.3 Lists

```(define (sh/wal/accumulate op initial sequence)
"Reduce a list to another structure"
(if (null? sequence)
initial
(op (car sequence)
(sh/wal/accumulate op initial (cdr sequence)))))

(sh/wal/accumulate + 0 (list 1 2 3 4 5))
```

### 1.4 Map Reduce

```(define (sh/wal/square n)
(* n n))

(map sh/wal/square (list 1 2 3 4 5))
```

### 1.5 Numbers

```(define (sh/wal/pct numer denom)
(exact->inexact (* 100 (/ numer denom))))

(sh/wal/pct 1111 3333)
```

### 1.6 Functions

```(define sh/wal/fib
(lambda (n)
(if
(or (= 0 n)
(= 1 n))
n
(+ sh/wal/fib(- 1 n) fib(- 2 n)))))

(sh/wal/fib 2)
```

### 1.7 Conditionals

```(define (sh/wal/even? n)
(cond
((< n 2) (= 0 n))
(else
(sh/wal/even? (- n 2)))))

(sh/wal/even? 1000)
```

### 1.8 Recursion

```(define sh/wal/fact
(lambda (n)
(if
(= 0 n)
1
(* n (sh/wal/fact (- n 1))))))

(sh/wal/fact 10)
```

### 1.9 Math

```(define (sh/wal/limit n)
(* 2 (floor (sqrt n))))

(sh/wal/limit 1000)
```
```(define (sh/wal/gcd a b)
(cond
((= a b) a)
((> a b) (sh/wal/gcd (- a b) b))
((< a b) (sh/wal/gcd a (- b a)))))

(sh/wal/gcd 120 35)
```

### 1.10 Closures

```(define a 5.3)
(define b 4.7)
(define c 2.8)

(define (sh/wal/area a b c)
(let ((s (/ (+ a b c) 2)))
(sqrt (* s (- s a) (- s b) (- s c)))))

(sh/wal/area a b c)
```

## 2 Source

```;; Pairs
(define (sh/wal/pair f r)
(lambda (op)
(op f r)))

(define (sh/wal/first p)
(p (lambda (f r)
f)))

(define (sh/wal/rest p)
(p (lambda (f r)
r)))

;;  Numbers
(define (sh/wal/gcd a b)
(cond
((= a b) a)
((> a b) (sh/wal/gcd (- a b) b))
((< a b) (sh/wal/gcd a (- b a)))))

;; Rational Numbers
(define (sh/wal/make-rat n d)
(sh/wal/pair n d))

(define (sh/wal/numer0 x)
(sh/wal/first x))

(define (sh/wal/denom0 x)
(sh/wal/rest x))

(define half (sh/wal/make-rat 1 2))

(sh/wal/numer0 half)
(sh/wal/denom0 half)

;; Reduction
(define twenty-four-seven (sh/wal/make-rat 120 35))

(sh/wal/gcd
(sh/wal/numer0 twenty-four-seven)
(sh/wal/denom0 twenty-four-seven))

(define (sh/wal/numer x)
(let ((g (sh/wal/gcd (sh/wal/first x) (sh/wal/rest x))))
(/ (sh/wal/first x) g)))

(define (sh/wal/denom x)
(let ((g (sh/wal/gcd (sh/wal/first x) (sh/wal/rest x))))
(/ (sh/wal/rest x) g)))

(sh/wal/numer twenty-four-seven)
(sh/wal/denom twenty-four-seven)

;; Sequences
(define (sh/wal/list-ref items n)
(if (= n 0)
(sh/wal/first items)
(sh/wal/list-ref (sh/wal/rest items) (- n 1))))

(define squares (sh/wal/pair
1
(sh/wal/pair
4
(sh/wal/pair
9
(sh/wal/pair
16
(sh/wal/pair 25 36))))))

(sh/wal/list-ref squares 3)

;; Trees
```

```@book{10.5555/230223,
author = {Friedman, Daniel P. and Felleisen, Matthias},
title = {The Little Schemer (4th Ed.)},
year = {1996},
isbn = {0262560992},
publisher = {MIT Press},
}

@book{10.5555/230222,
author = {Friedman, Daniel P. and Felleisen, Matthias},
title = {The Seasoned Schemer},
year = {1996},
isbn = {026256100X},
publisher = {MIT Press},
}

@book{10.5555/547755,
author = {Abelson, Harold and Sussman, Gerald J.},
title = {Structure and Interpretation of Computer Programs},
year = {1996},
isbn = {0262011530},
publisher = {MIT Press},
edition = {2nd}
}

@book{10.5555/1096473,
author = {McCarthy, John},
title = {LISP 1.5 Programmer’s Manual},
year = {1962},
isbn = {0262130114},
publisher = {The MIT Press}
}
```

Created: 2020-07-31 Fri 19:01

Validate