Skip to content

Instantly share code, notes, and snippets.

Created July 21, 2008 20:27
Show Gist options
  • Select an option

  • Save anonymous/116 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/116 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Jul 21, 2008.
    22 changes: 22 additions & 0 deletions pattern_matching_example.nu
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    ;; 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)))