Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern matching on two records with the same fields

Say I have this record:

type alias Rec = { a : Int }

And, for example, a function that takes two of these and sums their integers.

f: Rec -> Rec -> Int

This can be implemented using record accessors (i.e. f x y = x.a + y.a), but is there a way to use pattern matching to extract both integers?

Obviously, these two do not work because they would be binding two different numbers to the same variable:

f {a} {a} = a + a

f x y = case (x, y) of ({a}, {a}) -> a + a
like image 524
dcastro Avatar asked Jun 29 '16 12:06

dcastro


People also ask

How can you perform pattern matching 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 technique?

Pattern matching is comparing two patterns in order to determine whether they match (i.e., that they are the same) or do not match (i.e., that they differ). Pattern matching is the core procedure of theory-testing with cases.

What is pattern matching in information retrieval?

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.

Which operators can be used in query for pattern matching?

LIKE operator is used for pattern matching, and it can be used as -. % – It matches zero or more characters.


2 Answers

There seems to be no such way in the current Elm language. In other functional languages such as ML and Haskell, you could write patterns inside records like:

$ sml
Standard ML of New Jersey v110.74 [built: Sat Oct  6 00:59:36 2012]
- fun func {field=x} {field=y} = x+y ;
val func = fn : {field:int} -> {field:int} -> int
- func {field=123} {field=45} ;
val it = 168 : int

You might as well make a feature request to the developer(s) of Elm - or ask a question in the community mailing list at least.

P.S. After a quick search, I found such a proposal to add ML-like pattern matching on record fields in Elm, but it seems to have been turned down.:-(

like image 66
FPstudent Avatar answered Oct 29 '22 13:10

FPstudent


There's no way to do this currently. There is pattern aliasing (as) but it only works for a whole pattern, so this is invalid:

type alias Rec = { a : Int }

f: Rec -> Rec -> Int
f { a as xa } { a as ya } = xa + ya

main = f { a = 1 } { a = 2 }

results in:

Detected errors in 1 module.


-- SYNTAX PROBLEM --------------------------------------------------------------

I ran into something unexpected when parsing your code!

4| f { a as xa } { a as ya } = xa + ya
         ^
I am looking for one of the following things:

    a closing bracket '}'
    whitespace
like image 38
thSoft Avatar answered Oct 29 '22 13:10

thSoft