From 71c5c8e85071a67904d1b08d2f7fb0204bee5772 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Fri, 1 Nov 2019 21:44:05 -0700 Subject: add experimental ml/clojure-like code --- com/simatime/core.scm | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'com') 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 () (_first)) + + +;; Functor + +(define-class ()) + +(define-method (fmap (f ) (coll ))) + + +;; Applicative + +;; a -> f a +(define-method (pure (a ))) + +;; f (a -> b) -> f a -> f b +(define-method (<*> (f ) (a ) (b ))) + +;; f a -> f b -> f b +(define-method (*> (a ) (b ))) + +;; f a -> f b -> f a +(define-method (<* (a ) (b ))) + +|# -- cgit v1.2.3