简体   繁体   中英

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 ie 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM