Skip to content

Instantly share code, notes, and snippets.

@schacon
Forked from anonymous/pattern_matching_example.nu
Created July 23, 2008 17:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save schacon/1820 to your computer and use it in GitHub Desktop.
Save schacon/1820 to your computer and use it in GitHub Desktop.
;; Issac Trotts' demonstration of pattern matching in Nu
(function people-to-string (people)
(match people
(() "no people")
((p1) "one person: #{p1}")
((p1 p2) "two people: #{p1} and #{p2}")
(else "too many people: #{(people length)}")))
(assert_equal "no people"
(people-to-string '()))
(assert_equal "one person: Tim"
(people-to-string '(Tim)))
(assert_equal "two people: Tim and Matz"
(people-to-string '(Tim Matz)))
(assert_equal "too many people: 3"
(people-to-string '(Tim Guido Matz)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment