little explanation
This commit is contained in:
parent
74c00f4962
commit
df44c8979b
|
@ -2,6 +2,12 @@
|
||||||
(require remix/stx0
|
(require remix/stx0
|
||||||
remix/num/gen0)
|
remix/num/gen0)
|
||||||
|
|
||||||
|
;; define is replaced with def
|
||||||
|
(def z 42)
|
||||||
|
(module+ test
|
||||||
|
z)
|
||||||
|
|
||||||
|
;; when def has more forms than one, they are put inside of a block
|
||||||
(def x
|
(def x
|
||||||
(def a 40)
|
(def a 40)
|
||||||
(def b 2)
|
(def b 2)
|
||||||
|
@ -9,22 +15,35 @@
|
||||||
(module+ test
|
(module+ test
|
||||||
x)
|
x)
|
||||||
|
|
||||||
|
;; but of course def supports function definitions. [] is NOT the same
|
||||||
|
;; as () and defaults to expanding to a block definition
|
||||||
(def (f x y)
|
(def (f x y)
|
||||||
(+ [(def z (+ x x)) z] y))
|
(+ [(def z (+ x x))
|
||||||
|
z]
|
||||||
|
y))
|
||||||
(module+ test
|
(module+ test
|
||||||
(f x x))
|
(f x x))
|
||||||
|
|
||||||
|
;; cond requires []s for the question-answer pairs. It uses this to
|
||||||
|
;; make any code in between clauses go in between the `if`s that pop
|
||||||
|
;; out of the cond macro. finally, cond REQUIRES a #:else clause.
|
||||||
(def (g x)
|
(def (g x)
|
||||||
(cond
|
(cond
|
||||||
[(< x 100) "100"]
|
[(< x 100) "100"]
|
||||||
(def z (/ x 2))
|
(def z (/ x 2))
|
||||||
[(< z 100) "div 100"]
|
[(< z 100) "div 100"]
|
||||||
[#:else "other"]))
|
[#:else z]))
|
||||||
(module+ test
|
(module+ test
|
||||||
(g 50)
|
(g 50)
|
||||||
(g 199)
|
(g 199)
|
||||||
(g 200))
|
(g 200))
|
||||||
|
|
||||||
|
;; the @ reader is always on. One fun thing about this is that you can
|
||||||
|
;; make non-() macros. I wrote a little helper function to turn the
|
||||||
|
;; string arguments that @{} produces into a string port that has
|
||||||
|
;; accurate source location information for the original file. datalog
|
||||||
|
;; uses this to make all the source locations correct, so errors in
|
||||||
|
;; datalog will give accurate source locations.
|
||||||
(require remix/datalog0)
|
(require remix/datalog0)
|
||||||
(def graph (make-theory))
|
(def graph (make-theory))
|
||||||
@datalog[graph]{
|
@datalog[graph]{
|
||||||
|
@ -34,26 +53,26 @@
|
||||||
path(X, Y)?
|
path(X, Y)?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
;; {} is also not (), but is an infix macro
|
||||||
(def v7
|
(def v7
|
||||||
{3 + 4})
|
{3 + 4})
|
||||||
(module+ test
|
(module+ test
|
||||||
v7)
|
v7)
|
||||||
|
|
||||||
|
;; {} use C's precedence and considers the things you expect to be
|
||||||
|
;; operators
|
||||||
(def v-26
|
(def v-26
|
||||||
{2 * 3 - 48 / 4 - 4 * 5})
|
{2 * 3 - 48 / 4 - 4 * 5})
|
||||||
(module+ test
|
(module+ test
|
||||||
v-26)
|
v-26)
|
||||||
|
|
||||||
(def v15
|
;; if a symbol contains no alphabetic or numeric characters, then it
|
||||||
{v7 * 2 + 1})
|
;; is considered an operator. This means you can automatically use
|
||||||
|
;; stuff like & and →, but you won't confuse it with symbols like z
|
||||||
|
(def v85
|
||||||
|
{z * 2 + 1})
|
||||||
(module+ test
|
(module+ test
|
||||||
v15)
|
v85)
|
||||||
|
|
||||||
(def v14
|
|
||||||
(def (f x y) (+ x y))
|
|
||||||
{v7 ,f v7})
|
|
||||||
(module+ test
|
|
||||||
v14)
|
|
||||||
|
|
||||||
(def v1
|
(def v1
|
||||||
(def & bitwise-and)
|
(def & bitwise-and)
|
||||||
|
@ -61,6 +80,26 @@
|
||||||
(module+ test
|
(module+ test
|
||||||
v1)
|
v1)
|
||||||
|
|
||||||
|
(def v56
|
||||||
|
(def (→ x y) (+ (* x x) y))
|
||||||
|
{v7 → v7})
|
||||||
|
(module+ test
|
||||||
|
v56)
|
||||||
|
|
||||||
|
;; However, if you use , then you can force anything to be a binary
|
||||||
|
;; operator and force something that would have been a binary operator
|
||||||
|
;; into an argument.
|
||||||
|
(def v14
|
||||||
|
(def (f x y) (+ x y))
|
||||||
|
{v7 ,f v7})
|
||||||
|
(module+ test
|
||||||
|
v14)
|
||||||
|
|
||||||
|
(def v14b
|
||||||
|
{v7 ,(λ (x y) (+ x y)) v7})
|
||||||
|
(module+ test
|
||||||
|
v14b)
|
||||||
|
|
||||||
(def v9
|
(def v9
|
||||||
(def & 2)
|
(def & 2)
|
||||||
{v7 + ,&})
|
{v7 + ,&})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user