diff options
author | Ben Sima <ben@bsima.me> | 2020-05-02 18:26:32 -0700 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2020-05-02 18:26:32 -0700 |
commit | 4b50c03c9767750fbbd857121d500c3afbfdfdf2 (patch) | |
tree | 6de5ac77e8f36e0adc2de7ada01e1f9f8584ab58 | |
parent | b8c33100286ab307f46d3dfe7adf44008cc59d3e (diff) |
Clojure-inspired scheme macros
-rw-r--r-- | Alpha/Core.scm | 64 |
1 files changed, 37 insertions, 27 deletions
diff --git a/Alpha/Core.scm b/Alpha/Core.scm index 3a5ac6e..b785a13 100644 --- a/Alpha/Core.scm +++ b/Alpha/Core.scm @@ -10,6 +10,9 @@ ;; booleans true? false? nil nil? + ;; control flow + -> ->> fn /. + ;; dev helpers comment )) @@ -40,7 +43,7 @@ (cons init (unfold f (f init) pred)))) (define (sum lst) (fold + 0 lst)) -(define (produce lst) (fold * 1 lst)) +(define (product lst) (fold * 1 lst)) (define count length) @@ -128,31 +131,38 @@ ((_ s args ...) (format #f s args ...)))) -;; If I implement ML-like interface abstractions in scheme, what would it look like? - -;; -;; ;; seq - -;; (define-class <seq> () (_first)) - -;; -;; ;; Functor - -;; (define-class <functor> ()) - -;; (define-method (fmap (f <procedure>) (coll <functor>))) - -;; -;; ;; Applicative - -;; ;; a -> f a -;; (define-method (pure (a <any>))) - -;; ;; f (a -> b) -> f a -> f b -;; (define-method (<*> (f <procedure>) (a <applicative>) (b <applicative>))) +(define-syntax fn + (syntax-rules () + ((_ args body ...) + (lambda args body ...)))) -;; ;; f a -> f b -> f b -;; (define-method (*> (a <applicative>) (b <applicative>))) +(define-syntax -> + (syntax-rules () + [(_ a) a] + [(_ a (b c ...)) + (b a c ...)] + [(_ a b) + (-> a (b))] + [(_ a b c ...) + (-> (-> a b) c ...)])) + +(define-syntax ->> + (syntax-rules () + [(_ a) a] + [(_ a (b ...)) + (b ... a)] + [(_ a b) + (b a)] + [(_ a b c ...) + (->> (->> a b) c ...)])) + +;; Shen-like lambda +(define-syntax /. + (syntax-rules () + [(/. a b) + (lambda (a) b)] + [(/. a ... (b ...)) + (lambda (a ...) (b ...))])) -;; ;; f a -> f b -> f a -;; (define-method (<* (a <applicative>) (b <applicative>))) +(define (inc a) (+ 1 a)) +(define (dec a) (- a 1)) |