Will Rust's type checker warn you of a non-exhaustive pattern match?
Will Rust's type checker warn you of a non-exhaustive pattern match?
The new bespoke scheme gives approximately the same results as HM but is drastically simpler. For all I know this could inhibit Rust's future ability to do even more powerful things with types, but AIUI this scheme has the advantage of being actually decidable given the extensions to HM that we would require in the current language.
I ain't a type theorist though, so take this as hearsay. :)
I'm curious (and a bit skeptical) of your claim that the scheme is "drastically simpler" than HM. HM is a beautifully simple design, which can be expressed (abstractly) in just a couple of lines.
http://smallcultfollowing.com/babysteps/blog/2014/07/09/an-e...
"This scheme simplifies the code of the type inferencer dramatically and (I think) helps to meet our intutions (as I will explain). It is however somewhat less flexible than the existing inference scheme, though all of rustc and all the libraries compile without any changes."
let fizzbuzz num =
match num % 3, num % 5 with
| 0,0 -> "FizzBuzz"
| 0,_ -> "Fizz"
| _,0 -> "Buzz"
| _,_ -> num.ToString()
[1..100]
|> List.map fizzbuzz
|> List.iter (fun (s:string) -> printfn "%s" s)FWIW your final wildcard match can be (modestly) simplified to just
| _ -> num.ToString()
and the last line can be distilled to just |> List.iter (printfn "%s") #lang racket
(define (fizz-buzz n)
(match (list (modulo n 3) (modulo n 5))
[(list 0 0) "FizzBuzz"]
[(list 0 _) "Fizz"]
[(list _ 0) "Buzz"]
[_ n]))
(for [(i (range 100))]
(displayln (fizz-buzz i))) fizzbuzz x = case (x `mod` 3, x `mod` 5) of
(0, 0) -> "FizzBuzz"
(0, _) -> "Fizz"
(_, 0) -> "Buzz"
_ -> show i
mapM_ (putStrLn . fizzbuzz) [1..100] [1..100] |> List.iter (fizzbuzz >> printfn "%s") for i in range(1, 101):
x = ""
if i % 3 == 0:
x += "Fizz"
if i % 5 == 0:
x += "Buzz"
if x == "":
x += str(i)
print(x)
If anybody is randomly curious it can be fun to solve FizzBuzz in Haskell the same way that this Python code does it, but (to be more idiomatically Haskell) storing the FizzBuzz success in a Maybe (or some other variable). If you define the appropriate `~>`, and higher-precedence `|~`, and higher-precedence `|~~>`, you can write the above function as: fizzbuzz x =
mod x 3 == 0 ~> "Fizz"
|~ mod x 5 == 0 ~> "Buzz"
|~~> show x
It's interesting because it's sort of a "follow-through guards" situation; the (|~) operator can at least be turned into a type signature of (Monoid m) => Maybe m -> Maybe m -> Maybe m.It refuses to compile entirely.