Table of Contents

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))

;; (define (filter predicate sequence))

(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)

1.11. Modules

1.12. Abstractions

2. Source

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

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

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


;; Lambda Calculus Style
(define sh/wal/switch
  (lambda (f)
    (lambda (r)
      (lambda (op)
        ((op f) r)))))

(define sh/wal/left
  (lambda (a)
    (lambda (b) a)))

(define sh/wal/right
  (lambda (a)
    (lambda (b) b)))

(sh/wal/right ((sh/wal/switch 1) 2))

(define sh/wal/true
  (lambda (a)
    (lambda (b) a)))

(define sh/wal/false
  (lambda (a)
    (lambda (b) b)))

(define sh/wal/not
  (lambda (x)
    ((x sh/wal/false) sh/wal/true)))

(eq? (sh/wal/not sh/wal/true) sh/wal/false)
(eq? (sh/wal/not (sh/wal/not sh/wal/true)) sh/wal/true)
(eq? (sh/wal/not sh/wal/false) sh/wal/true)

(define sh/wal/and
  (lambda (x)
    (lambda (y)
      ((x y) x))))

(define sh/wal/or
  (lambda (x)
    (lambda (y)
      ((x x) y))))


;;  Numbers
(define sh/wal/gcd
  (lambda (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
  (lambda (n d)
    (sh/wal/pair n d)))

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

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

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

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


;; TODO Map
(define (sh/wal/map op sequence)
  "Reduce a list to another structure"
  (if (not (null? sequence))
      (cons (op (car sequence))
           (sh/wal/map op (cdr sequence)))))

(define sh/wal/inc
  (lambda (n)
    (+ 1 n)))

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


;; Reduce
(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))


;; Types
(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

3. Reading

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

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

@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},
address = {Cambridge, MA, USA},
edition = {2nd}
}

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

Author: Jason Walsh

Created: 2023-11-29 Wed 15:08

Validate