Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell Pattern Matching on the Empty Set

Tags:

I'm changing some Haskell code from using lists to sets. I understand everything required, I think, but I'm not sure how to pattern match on sets. Lists have this nice literal syntax that seems hard to emulate with the Set constructor. For example, I might have some code like this:

foo [] = []
foo x = other_thing

How can I write this code so it uses Sets instead of lists?

like image 868
Derek Thurn Avatar asked Jul 25 '10 00:07

Derek Thurn


People also ask

Does Haskell have pattern matching?

(Pattern matching in Haskell is different from that found in logic programming languages such as Prolog; in particular, it can be viewed as "one-way" matching, whereas Prolog allows "two-way" matching (via unification), along with implicit backtracking in its evaluation mechanism.)

How does Haskell pattern matching work?

Pattern matching consists of specifying patterns to which some data should conform and then checking to see if it does and deconstructing the data according to those patterns. When defining functions, you can define separate function bodies for different patterns.

Where is pattern matching in Haskell?

We use pattern matching in Haskell to simplify our codes by identifying specific types of expression. We can also use if-else as an alternative to pattern matching. Pattern matching can also be seen as a kind of dynamic polymorphism where, based on the parameter list, different methods can be executed.


1 Answers

import qualified Data.Set as Set

foo set
  | Set.null set = bar
  | otherwise = baz
like image 144
Thomas Eding Avatar answered Oct 09 '22 15:10

Thomas Eding