Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the syntax for single-line pattern matching?

Tags:

word 0 = "Zero" word 1 = "One" word 2 = "Two" 

How would I condense a pattern-matching function like this one into a single line?

I tried word 0 = "Zero" word 1 = "One" word 2 = "Two", but that didn't work.

like image 725
Maxpm Avatar asked Dec 05 '11 15:12

Maxpm


People also ask

How pattern matching is done in SQL?

SQL pattern matching allows you to search for patterns in data if you don't know the exact word or phrase you are seeking. This kind of SQL query uses wildcard characters to match a pattern, rather than specifying it exactly. For example, you can use the wildcard "C%" to match any string beginning with a capital C.

What is pattern matching in programming?

Pattern matching in computer science is the checking and locating of specific sequences of data of some pattern among raw data or a sequence of tokens. Unlike pattern recognition, the match has to be exact in the case of pattern matching.

What is pattern matching in C#?

Pattern matching is a technique where you test an expression to determine if it has certain characteristics. C# pattern matching provides more concise syntax for testing expressions and taking action when an expression matches.

What is pattern matching in Ruby?

What Is Pattern Matching? Pattern matching is a feature that is commonly found in functional programming languages. According to Scala documentation, pattern matching is “a mechanism for checking a value against a pattern. A successful match can also deconstruct a value into its constituent parts.”


2 Answers

It's a strange requirement to write a function in a single line. Anyway, case could be helpful there too.

word x = case x of {0 -> "Zero"; 1 -> "One"; 2 -> "Two"} 
like image 115
ДМИТРИЙ МАЛИКОВ Avatar answered Oct 20 '22 03:10

ДМИТРИЙ МАЛИКОВ


Use semicolons:

word 0 = "Zero"; word 1 = "One"; word 2 = "Two" 
like image 43
augustss Avatar answered Oct 20 '22 03:10

augustss