{"id":1110,"date":"2022-08-30T15:12:33","date_gmt":"2022-08-30T15:12:33","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-define-a-wildcard-pattern-using-cerlc_clause-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:12:33","modified_gmt":"2022-08-30T15:12:33","slug":"how-to-define-a-wildcard-pattern-using-cerlc_clause-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-define-a-wildcard-pattern-using-cerlc_clause-collection-of-common-programming-errors\/","title":{"rendered":"How to define a wildcard pattern using cerl:c_clause-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to compile some personal language to erlang. I want to create a function with pattern matching on clauses.<\/p>\n<p>This is my data :<\/p>\n<pre><code>Data =\n    [ {a,  }\n    , {b,  }\n    , {c,  }\n    ].\n<\/code><\/pre>\n<p>This is what i want :<\/p>\n<pre><code>foo(a) -&gt; ;\nfoo(b) -&gt; ;\nfoo(c) -&gt; ;\nfoo(_) -&gt; undefined. %% \n        cerl:c_clause([cerl:c_atom(Pattern)], deep_literal(Body))\n    end,\nWildCardClause = cerl:c_clause([ ??? ], cerl:c_atom(undefined)),\nCaseClauses = [MkCaseClause(S) || S \n        case Key of\n           when 'true' -&gt; ...\n           when 'true' -&gt; ...\n           when 'true' -&gt; ...\n        end\n<\/code><\/pre>\n<p>So okay, <code>case<\/code> is translated to <code>if<\/code> when core is compiled. So i need to specify a <code>true<\/code> clause as in an <code>if<\/code> expression to get a pure wildcard. I don&#8217;t know how to do it, since matching <code>true<\/code> in an <code>if<\/code> expression and in a <code>case<\/code> one are different semantics. In a case, <code>true<\/code> is not a wildcard.<\/p>\n<p>And what if i would like match expressions with wildcards inside like <code>{sometag,_,_,Thing} -&gt; {ok, Thing}<\/code>.<\/p>\n<p>Thank you<\/p>\n<ol>\n<li>\n<p>I&#8217;ve found a way to do this<\/p>\n<pre><code>...\nWildCardVar = cerl:c_var('_Any'),\nWildCardClause = cerl:c_clause([WildCardVar], cerl:c_atom(undefined)),\n...\n<\/code><\/pre>\n<p>It should work for inner wildcards too, but one has to be careful to give different variable names to each <code>_<\/code> wildcard since only multiple <code>_<\/code> do not match each other, variables do.<\/p>\n<pre><code>f(X,_, _ ) %% matches f(a,b,c)\nf(X,_X,_X) %% doesn't\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:29:24. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to compile some personal language to erlang. I want to create a function with pattern matching on clauses. This is my data : Data = [ {a, } , {b, } , {c, } ]. This is what i want : foo(a) -&gt; ; foo(b) -&gt; ; foo(c) -&gt; ; foo(_) -&gt; undefined. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[],"class_list":["post-1110","post","type-post","status-publish","format-standard","hentry","category-semantic","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1110"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1110\/revisions"}],"predecessor-version":[{"id":8811,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1110\/revisions\/8811"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}