Benjamin Gale
Benjamin Gale

Reputation: 13177

Haskell define multiple functions using tuples

I've just come across some Haskell code that looks something like this:

(functionOne, functionTwo)
  | someCondition = (10, "Ten")
  | otherwise     = (20, "Twenty")

From the way the code is used I think I understand the intent of this code i.e. it is just a more concise way of writing this:

functionOne
  | someCondition = 10
  | otherwise     = 20

functionTwo
  | someCondition = "Ten"
  | otherwise     = "Twenty"

However, I can't recall ever seeing functions written this way before and have no idea what this technique is called so can't search for any additional information about this.

So my questions are:

Upvotes: 4

Views: 107

Answers (1)

amalloy
amalloy

Reputation: 92117

These aren't functions, just variable bindings. You correctly understand how it works. It doesn't have any particular name, because it's just another application of pattern matching. Anytime you could declare a variable, you can declare a more complex pattern in that same position.

Upvotes: 4

Related Questions