Skip to content

Instantly share code, notes, and snippets.

@johnwcowan
Created October 7, 2020 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnwcowan/dab93106d78a99274a0bd59a9f4b279f to your computer and use it in GitHub Desktop.
Save johnwcowan/dab93106d78a99274a0bd59a9f4b279f to your computer and use it in GitHub Desktop.
The K language's ! operator in Scheme, or, Polymorphism gone wild
(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