Skip to content

Instantly share code, notes, and snippets.

@tonyg
Created June 19, 2011 02:39
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 tonyg/1033686 to your computer and use it in GitHub Desktop.
Save tonyg/1033686 to your computer and use it in GitHub Desktop.
Pattern-matching over objects using views and coviews
(define-language <list>
(Nil)
(Cons first rest))
(define-language <maybe>
(Nothing)
(Just value))
(define-language <tsil>
(Snoc butlast last))
(extend-behaviour <list>
(object self
((meta (coerce '<tsil>))
(ocase self
((Cons a (Nil)) (Just (Snoc (Nil) a)))
((Cons a (Snoc b c)) (Just (Snoc (Cons a b) c)))
(_ (Nothing))))))
(extend-behaviour <tsil>
(object self
((meta (coerce '<list>))
(ocase self
((Snoc (Nil) a) (Just (Cons a (Nil))))
((Snoc (Cons a b) c) (Just (Cons a (Snoc b c))))
(_ (Nothing))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment