Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell define multiple functions using tuples

Tags:

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:

  • Is my understanding of what is going on here correct?
  • Does this technique have a name?
like image 982
Benjamin Gale Avatar asked Aug 15 '21 11:08

Benjamin Gale


1 Answers

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.

like image 77
amalloy Avatar answered Oct 12 '22 22:10

amalloy