Skip to content

Commit 20129f1

Browse files
committed
scheme.cljc
1 parent 74fba31 commit 20129f1

2 files changed

Lines changed: 49 additions & 59 deletions

File tree

src/mentat_collective/emmy/fdg_prologue.clj

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
[scicloj.kindly.v4.kind :as kind]
1212
[emmy.env :as e :refer [->infix simplify Lagrange-equations literal-function]]
1313
[emmy.mechanics.lagrange :as lg]
14-
[emmy.expression.render :as r :refer [->infix]]
1514
[civitas.repl :as repl]))
1615

1716
;; Elemetary introduction to Emmy, taken from the first pages of the open-access book
@@ -21,6 +20,9 @@
2120
;; The [Emmy]((https://emmy.mentat.org)) maintainer, [Sam Ritchie](https://roadtoreality.substack.com/), wrote the source for this page, namely the
2221
;; [LaTex version of FDG](https://github.com/mentat-collective/fdg-book/blob/main/scheme/org/prologue.org).
2322

23+
;; In adopting MIT-Scheme's `(define ...)`, I trust that Clojure people will bridge that gap quickly
24+
;; and am sure of the eventual gratitude of all readers of that immutable, dense book. So without further ado ...
25+
2426
^:kindly/hide-code
2527
(kind/hiccup
2628
[:div
@@ -29,62 +31,8 @@
2931
[:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen/dist/scittle.cljs-ajax.js"}]
3032
[:script {:src "https://cdn.jsdelivr.net/npm/react@18/umd/react.production.min.js", :crossorigin ""}]
3133
[:script {:src "https://cdn.jsdelivr.net/npm/react-dom@18/umd/react-dom.production.min.js", :crossorigin ""}]
32-
[:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen/dist/scittle.reagent.js"}]])
33-
34-
^:kindly/hide-code
35-
(def md
36-
(comp kindly/hide-code kind/md))
37-
38-
39-
^:kindly/hide-code
40-
(kind/scittle
41-
'(defn walk [inner outer form]
42-
(cond
43-
(list? form) (outer (apply list (map inner form)))
44-
(seq? form) (outer (doall (map inner form)))
45-
(coll? form) (outer (into (empty form) (map inner form)))
46-
:else (outer form))))
47-
48-
^:kindly/hide-code
49-
(kind/scittle
50-
'(defn postwalk [f form]
51-
(walk (partial postwalk f) f form)))
52-
53-
^:kindly/hide-code
54-
(kind/scittle
55-
'(defn postwalk-replace [smap form]
56-
(postwalk (fn [x] (if (contains? smap x) (smap x) x)) form)))
57-
58-
^:kindly/hide-code
59-
(kind/scittle
60-
'(defmacro let-scheme [b & e]
61-
(concat (list 'let (into [] (apply concat b))) e)))
62-
63-
^:kindly/hide-code
64-
(kind/scittle
65-
'(defmacro define-1 [h & b]
66-
(let [body (postwalk-replace {'let 'let-scheme} b)]
67-
(if (coll? h)
68-
(if (coll? (first h))
69-
(list 'defn (ffirst h) (into [] (rest (first h)))
70-
(concat (list 'fn (into [] (rest h))) body))
71-
(concat (list 'defn (first h) (into [] (rest h)))
72-
body))
73-
(concat (list 'def h) body)))))
74-
75-
^:kindly/hide-code
76-
(kind/scittle
77-
'(defmacro define [h & b]
78-
(if (and (coll? h) (= (first h) 'tex-inspect))
79-
(list 'do
80-
(concat ['define-1 (second h)] b)
81-
h)
82-
(concat ['define-1 h] b))))
83-
84-
^:kindly/hide-code
85-
(kind/scittle
86-
'(defmacro lambda [h b]
87-
(list 'fn (into [] h) b)))
34+
[:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen/dist/scittle.reagent.js"}]
35+
[:script {:type "application/x-scittle" :src "scheme.cljc"}]])
8836

8937
^:kindly/hide-code
9038
(kind/scittle
@@ -119,8 +67,10 @@
11967
(list 'kind/reagent [:h3 (list 'quote (cons 'show-expression b))]))
12068

12169
^:kindly/hide-code
122-
(kind/scittle '(declare Gamma))
70+
(def md
71+
(comp kindly/hide-code kind/md))
12372

73+
;;
12474
;; ## Programming and Understanding
12575

12676
;; One way to become aware of the precision required to unambiguously communicate a
@@ -260,6 +210,9 @@
260210

261211
;; This expression is equivalent to a computer program:[fn:6]
262212

213+
^:kindly/hide-code
214+
(kind/scittle '(declare Gamma))
215+
263216
(define ((Lagrange-equations Lagrangian) w)
264217
(- (D (compose ((partial 2) Lagrangian) (Gamma w)))
265218
(compose ((partial 1) Lagrangian) (Gamma w))))
@@ -310,7 +263,7 @@
310263
proposed-solution)
311264
't))
312265

313-
;; [note by MAK: copy-paste the code into the sidebar and verify the above result.]
266+
;; [note by MAK: copy-paste the `(show-expression ...)` code-snippet into the sidebar, press Ctrl+Enter and verify the above result.]
314267

315268
;; The residual here shows that for nonzero amplitude, the only solutions allowed
316269
;; are ones where $(k - m\omega^2) = 0$ or $\omega = \sqrt{k/m}$.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(ns mentat-collective.emmy.scheme)
2+
3+
(defn walk [inner outer form]
4+
(cond
5+
(list? form) (outer (apply list (map inner form)))
6+
(seq? form) (outer (doall (map inner form)))
7+
(coll? form) (outer (into (empty form) (map inner form)))
8+
:else (outer form)))
9+
10+
(defn postwalk [f form]
11+
(walk (partial postwalk f) f form))
12+
13+
(defn postwalk-replace [smap form]
14+
(postwalk (fn [x] (if (contains? smap x) (smap x) x)) form))
15+
16+
(defmacro let-scheme [b & e]
17+
(concat (list 'let (into [] (apply concat b))) e))
18+
19+
(defmacro define-1 [h & b]
20+
(let [body (postwalk-replace {'let 'let-scheme} b)]
21+
(if (coll? h)
22+
(if (coll? (first h))
23+
(list 'defn (ffirst h) (into [] (rest (first h)))
24+
(concat (list 'fn (into [] (rest h))) body))
25+
(concat (list 'defn (first h) (into [] (rest h)))
26+
body))
27+
(concat (list 'def h) body))))
28+
29+
(defmacro define [h & b]
30+
(if (and (coll? h) (= (first h) 'tex-inspect))
31+
(list 'do
32+
(concat ['define-1 (second h)] b)
33+
h)
34+
(concat ['define-1 h] b)))
35+
36+
(defmacro lambda [h b]
37+
(list 'fn (into [] h) b))

0 commit comments

Comments
 (0)