Haskell - In Functional Programming is it considered a bad practice to have incomplete pattern matchings

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Is it generally considered a bad practice to use non-exhaustive pattern machings in functional languages like Haskell or F#, which means that the cases specified don t cover all possible input cases?

In particular, should I allow code to fail with a MatchFailureException etc. or should I always cover all cases and explicitly throw an error if necessary?

Example:

let head (x::xs) = x

Or

let head list = 
    match list with
    | x::xs -> x
    |    _  -> failwith "Applying head to an empty list"

F# (unlike Haskell) gives a warning for the first code, since the []-case is not covered, but can I ignore it without breaking functional style conventions for the sake of succinctness? A MatchFailure does state the problem quite well after all ...

Answers

If you complete your pattern-matchings with a constructor [] and not the catch-all _, the compiler will have a chance to tell you to look again at the function with a warning the day someone adds a third constructor to lists.

http://caml.inria.fr/mantis/view.php?id=4073 http://caml.inria.fr/mantis/view.php?id=4073

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/1882334/in-functional-programming-is-it-considered-a-bad-practice-to-have-incomplete-pa

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils