pattern-matching

Can `match` in Racket have patterns with variables from an outer scope?

扶醉桌前 提交于 2019-12-04 00:20:34
Consider the following example: #lang racket (match '(cat . doge) [`(,a . ,b) (match b [a #t] [_ #f])] [_ "Not a pair"]) This is what I might write if I wanted to match pairs where the head and tail are the same. This doesn't work though because the second a is bound as a new variable (and matches anything). Are there any pattern forms which allow me to use the previously bound a from the outer scope? I know this can be achieved in the following way (match* ('cat 'doge) [(a a) #t] [(_ _) #f]) but I still would like to know if there is a way to get that variable from the outer scope (or if

How does RouteBase.GetRouteData work or pointers for implementing pattern matching

余生颓废 提交于 2019-12-03 22:44:27
问题 I'm looking at implementing an option for defining specific URL patterns that my HttpModule is to ignore. I'm wanting to be able to define "filters" such as: /Admin/{*} /Products/{*}/Search Which should filter out urls like: http://mysite.com/admin/options http://mysite.com/products/toys/search but not filter out http://mysite.com/orders http://mysite.com/products/view/1 Much the same as how ASP.NET MVC has registered routes which match a pattern. I've looked at the source code of Phil Haack

Make Compile Fail on Non-Exhaustive Match in SBT

自作多情 提交于 2019-12-03 22:31:43
Let's say that I have a trait, Parent, with one child, Child. scala> sealed trait Parent defined trait Parent scala> case object Boy extends Parent defined module Boy I write a function that pattern matches on the sealed trait. My f function is total since there's only a single Parent instance. scala> def f(p: Parent): Boolean = p match { | case Boy => true | } f: (p: Parent)Boolean Then, 2 months later, I decide to add a Girl child of Parent . scala> case object Girl extends Parent defined module Girl And then re-write the f method since we're using REPL. scala> def f(p: Parent): Boolean = p

Is there any fundamental limitations that stops Scala from implementing pattern matching over functions?

自古美人都是妖i 提交于 2019-12-03 22:29:17
In languages like SML, Erlang and in buch of others we may define functions like this: fun reverse [] = [] | reverse x :: xs = reverse xs @ [x]; I know we can write analog in Scala like this (and I know, there are many flaws in the code below): def reverse[T](lst: List[T]): List[T] = lst match { case Nil => Nil case x :: xs => reverse(xs) ++ List(x) } But I wonder, if we could write former code in Scala, perhaps with desugaring to the latter. Is there any fundamental limitations for such syntax being implemented in the future (I mean, really fundamental -- e.g. the way type inference works in

State transition table for aho corasick algorithm

假如想象 提交于 2019-12-03 22:19:38
Please help me to understand the construction of state transition table for more than one patterns in the Aho-Corasick algorithm. Please give a simple and detailed explanation so that I could understand. I am following this paper and here is the animation for that. Thanks. Phase 1 Creating the keyword tree : Starting at the root, follow the path labeled by chars of P i If the path ends before P i , continue it by adding new edges and ... nodes for the remaining characters of P Store identifier i of P i at the terminal node of the path I demonstrate it by an example : Suppose we have a finite

How to detect identical part(s) inside string?

拟墨画扇 提交于 2019-12-03 21:53:06
I try to break down the decoding algorithm wanted question into smaller questions. This is Part I. Question: two strings: s1 and s2 part of s1 is identical to part of s2 space is separator how to extract the identical part(s)? example 1: s1 = "12 November 2010 - 1 visitor" s2 = "6 July 2010 - 100 visitors" the identical parts are "2010", "-", "1" and "visitor" example 2: s1 = "Welcome, John!" s2 = "Welcome, Peter!" the identical parts are "Welcome," and "!" example 3: (to clarify the "!" example) s1 = "Welcome, Sam!" s2 = "Welcome, Tom!" the identical parts are "Welcome," and "m!" Python and

Simulating ML-style pattern matching in C++

流过昼夜 提交于 2019-12-03 19:51:41
问题 The title says pretty much all, how would I go about simulating ML-style pattern matching in C++, that is for instance; Statement *stm; match(typeof(stm)) { case IfThen: ... case IfThenElse: ... case While: ... ... } Where 'IfThen', 'IfThenElse' and 'While' are classes which inherit from 'Statement' 回答1: There was a paper in the C++ committee recently which describe a library that allow to do just that: Open and Efficient Type Switch for C++ by Stroustup, Dos Reis and Solodkyy http://www.open

Elixir: pattern matching works differently for tuples and maps

只愿长相守 提交于 2019-12-03 17:38:50
问题 In Elixir, if I try to pattern match the following two tuples: {a} = {1, 2} I get a match error. But if I do the same for two maps: %{x: a} = %{x: 1, y: 2} It works fine, and a binds to 1. I can see why matching the two tuples gave an error, but why did matching the maps not give an error? 回答1: In the first example you are attempting to match a single element tuple against a two-element tuple. In the second example you are matching on the :x key in both the left and right maps. EDIT: I should

Ruby String#scan equivalent to return MatchData

半世苍凉 提交于 2019-12-03 17:01:00
问题 As basically stated in the question title, is there a method on Ruby strings that is the equivalent to String#Scan but instead of returning just a list of each match, it would return an array of MatchData s? For example: # Matches a set of characters between underscore pairs "foo _bar_ _baz_ hashbang".some_method(/_[^_]+_/) #=> [#&ltMatchData "_bar_"&rt, &ltMatchData "_baz_"&rt] Or any way I could get the same or similar result would be good. I would like to do this to find the positions and

Perl - regex - Position of first nonmatching character

安稳与你 提交于 2019-12-03 16:59:05
I want to find the position in a string, where a regular expression stops matching. Simple example: my $x = 'abcdefghijklmnopqrstuvwxyz'; $x =~ /gho/; This example shall give me the position of the character 'h' because 'h' matches and 'o' is the first nonmatching character. I thought of using pos or $- but it is not written on unsuccessful match. Another solution would be to iteratively shorten the regex pattern until it matches but that's very ugly and doesn't work on complex patterns. EDIT: Okay for the linguists: I'm sorry for my awful explanation. To clarify my situation: If you think of