Created
October 7, 2020 20:02
-
-
Save johnwcowan/dab93106d78a99274a0bd59a9f4b279f to your computer and use it in GitHub Desktop.
The K language's ! operator in Scheme, or, Polymorphism gone wild
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import (srfi 1)) | |
;; The K language's polymorphic ! operator | |
(define bang | |
(case-lambda | |
((x) | |
(iota x)) | |
((x y) | |
(cond | |
((exact-integer? y) | |
(modulo x y)) | |
((list? y) | |
(list-rotate x y)))))) | |
(define (list-rotate count list) | |
(if (negative? count) | |
(list-rotate (+ (length list) count) list) | |
(append (drop list count) (take list count)))) | |
;;; Scheme version of K expression 2!!7!4 from Wikipediaå | |
;;; K operators are right-associative | |
(bang 2 (bang (bang 7 4))) | |
;;; => (2 0 1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment