diff options
author | Ben Sima <ben@bsima.me> | 2019-11-01 21:44:05 -0700 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2019-11-01 21:44:05 -0700 |
commit | 71c5c8e85071a67904d1b08d2f7fb0204bee5772 (patch) | |
tree | cdb6c165bf48bc7a753d1e0b236ebe5628a1a23a | |
parent | 3020451c7f455eed9417e838e0ae2f0b26d4dbfc (diff) |
add experimental ml/clojure-like code
-rw-r--r-- | com/simatime/core.scm | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/com/simatime/core.scm b/com/simatime/core.scm index 611aaca..155214c 100644 --- a/com/simatime/core.scm +++ b/com/simatime/core.scm @@ -39,3 +39,74 @@ (for-each display args)) (define (prn . a) (apply pr a) (newline)) + +(define first + "Return the first item in the collection." + car) + +(define next + "Returns a list of the items after the first." + cadr) + +(define (second x) + (first (next x))) + +(define (ffirst x) + (first (first x))) + +(define (nnext x) + (next (next))) + +(define (last coll) + "Return the last time in coll, in linear time." + (if (next coll) + (last coll) + (first coll))) + +(define (false? x) + (eq? #f x)) + +(define (true? x) + (eq? #t x)) + +(define nil 'nil) + +(define (nil? x) + (eq? nil x)) + +(define-syntax when-not + (syntax-case + (()))) + +#| + +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>))) + +;; f a -> f b -> f b +(define-method (*> (a <applicative>) (b <applicative>))) + +;; f a -> f b -> f a +(define-method (<* (a <applicative>) (b <applicative>))) + +|# |