pattern-matching

fuzzy string matching with grep

本秂侑毒 提交于 2019-12-19 18:20:24
问题 I am trying to match rows in a file containing a string say ACTGGGTAAACTA . If I do grep "ACTGGGTAAACTA" file It gives me rows which have exact matches. Is there a way to allow for certain number of mismatches (substitutions, insertions or deletions)? For example, I am looking for sequences Up to 3 allowed subtitutions like "AGTGGGTAACCAA" etc. Insertions/deletions (having a partial match like "ACTGGGAAAATAAACTA" or "ACTAAACTA") 回答1: There used to be a tool called agrep for fuzzy regex

Pattern match variable scope

自闭症网瘾萝莉.ら 提交于 2019-12-19 13:49:10
问题 In the Roslyn Pattern Matching spec it states that: The scope of a pattern variable is as follows: If the pattern appears in the condition of an if statement, its scope is the condition and controlled statement of the if statement, but not its else clause. However the latest Microsoft "What's new" posts and presentations are showing this example: public void PrintStars(object o) { if (o is null) return; // constant pattern "null" if (!(o is int i)) return; // type pattern "int i" WriteLine

How to do pattern matching on a binary in Erlang?

妖精的绣舞 提交于 2019-12-19 10:36:35
问题 I would like to do recursion over a binary, and in each call read up to 32 bits from the binary, and return it in a new binary. But I can't get the pattern matching to work as I want. binaryToBinary(Source) -> binaryToBinaryAux(Source, <<>>). binaryToBinaryAux(<<>>, Target) -> Target; binaryToBinaryAux(<<H:32/binary, T/binary>>, Target) -> binaryToBinaryAux(<<T/binary>>, <<Target/binary, H>>). Here is the error I get for the pattern matching: 10> mymodule:binaryToBinary(<<"JonasPonas">>). **

SML How to define proper option

跟風遠走 提交于 2019-12-19 10:22:05
问题 Why doesn't the following code doesn't work? fun sum_list xs = case xs of [] => NONE | x::xs' => SOME (x+sum_list xs') This code works well when Instead of NONE it is zero and when I remove SOME. I know that for sum of an empty list zero is the reasonable answer. But why does the following example fails? Update: Made it work by following Diego's Answer: fun sum_list xs = case xs of [] => NONE | x => let fun slist x = case x of [] => 0 | x::xs' => x + slist xs' in SOME (slist x) end 回答1: The

Deconstructing an existential type

荒凉一梦 提交于 2019-12-19 10:19:05
问题 I am using an existential type as a wrapper. At a point in my code where I know the enclosed type, I want to do something with it that is specific to the enclosed type. This is the closest I can get: {-# LANGUAGE ExistentialQuantification #-} class Agent a where agentId :: a -> String speciesId :: a -> String -- plus other functions that all agents support -- | A wrapper allowing my daemon to read and write agents of any species. -- (Agents are stored in files that contain a tag so I know

Haskell - Using a constant in pattern matching

删除回忆录丶 提交于 2019-12-19 09:38:41
问题 Let's say I have the following code (text in <> is a shorthand, not actually part of the code): data A = <something> defaultA :: A defaultA = <Really complicated expression of type A> Now I want to have a function pattern match on defaultA , like this: f defaultA = <case 1> f _ = <case 2> However, defaultA in the first line becomes a new variable, not a condition that means the parameter will equal defaultA . The best way I know to achieve something like what I want is: f x | x == defaultA =

Pattern matching with guards vs if/else construct in F#

对着背影说爱祢 提交于 2019-12-19 06:34:26
问题 In ML-family languages, people tend to prefer pattern matching to if/else construct. In F#, using guards within pattern matching could easily replace if/else in many cases. For example, a simple delete1 function could be rewritten without using if/else (see delete2 ): let rec delete1 (a, xs) = match xs with | [] -> [] | x::xs' -> if x = a then xs' else x::delete1(a, xs') let rec delete2 (a, xs) = match xs with | [] -> [] | x::xs' when x = a -> xs' | x::xs' -> x::delete2(a, xs') Another

Find simplest regular expression matching all given strings

亡梦爱人 提交于 2019-12-19 05:45:17
问题 Is there an algorithm that can produce a regular expression (maybe limited to a simplified grammar) from a set of strings such that the evaluation of all possible strings that match the regular expression reproduces the initial set of strings? It is probably unrealistic to find such a algorithm for grammars of regular expressions with very "complicated" syntax (including arbitrary repetitions, assertions etc.), so let's start with a simplified one which only allows for an OR of substrings:

Expected String, found &str when matching an optional string

情到浓时终转凉″ 提交于 2019-12-19 05:33:51
问题 I am trying to write a simple function in Rust that will ask user a question expecting answer of "you" or "me". It should return a boolean value or ask again if the user answers wrong. I came up with: fn player_starts() -> bool { println!("Who will start (me/you)"); loop { let input = readline::readline(">"); match input { Some("me") => return true, Some("you") => return false, _ => None, } } } What I get is: error: mismatched types: expected `collections::string::String`, found `&'static str

Error combining 'if' statements that null-checks and Pattern Matches

一笑奈何 提交于 2019-12-19 05:16:43
问题 The following works as expected: dynamic foo = GetFoo(); if (foo != null) { if (foo is Foo i) { Console.WriteLine(i.Bar); } } but if I combine the if statements like so: if (foo != null && foo is Foo i) { Console.WriteLine(i.Bar); } then I receive a compiler warning Use of unassigned local variable 'i' Can anyone explain why this happens? 回答1: It would appear that this is not, in fact, a compiler error. It was previously reported as a bug here. However, it has been closed as not a bug. The