pattern-matching

A more complex version of “How can I tell if a string repeats itself in Python?”

若如初见. 提交于 2019-12-05 01:38:23
I was reading this post and I wonder if someone can find the way to catch repetitive motifs into a more complex string. For example, find all the repetitive motifs in string = 'AAACACGTACGTAATTCCGTGTGTCCCCTATACGTATACGTTT' Here the repetitive motifs: 'AAAC ACGTACGT AATTCC GTGTGT CCCC TATACGTATACG TTT' So, the output should be something like this: output = {'ACGT': {'repeat': 2, 'region': (5,13)}, 'GT': {'repeat': 3, 'region': (19,24)}, 'TATACG': {'repeat': 2, 'region': (29,40)}} This example comes from a typical biological phenomena termed microsatellite which is present into the DNA. UPDATE 1:

Pattern match list with exactly 2 elements in Haskell

╄→尐↘猪︶ㄣ 提交于 2019-12-05 01:32:09
I just started learning Haskell and I'm trying to use pattern matching to match a list that has exactly 2 elements. As an exercise, I'm trying to write a function which returns the one but last element from a list. So far I found this: myButLast :: [a] -> a myButLast [] = error "Cannot take one but last from empty list!" myButLast [x] = error "Cannot take one but last from list with only one element!" myButLast [x:y] = x myButLast (x:xs) = myButLast xs Now the line with myButLast [x:y] is clearly incorrect, but I don't know how to match a list that has exactly 2 elements, as that is what I'm

Match a Query to a Regular Expression in SQL?

浪子不回头ぞ 提交于 2019-12-05 00:27:12
问题 I'm trying to find a way to match a query to a regular expression in a database. As far as I can tell (although I'm no expert), while most DBMS like MySQL have a regex option for searching, you can only do something like: Find all rows in Column 1 that match the regex in my query. What I want to be able to do is the opposite, i.e.: Find all rows in Column 1 such that the regex in Column 1 matches my query. Simple example - say I had a database structured like so: +----------+-----------+ |

Haskell / GHC — is there any infix tag / pragma for “warn incomplete patterns”

故事扮演 提交于 2019-12-05 00:11:04
I'm looking for a pragma that will warn on a particular incomplete pattern. It would make the compiler fail with the following (hypothetical) code: {-# FAILIF incomplete-patterns #-} f :: Int -> Int f 0 = 0 I am trying to write a "compiler" using Arrows, and knowing pattern matching is complete would help isolate bugs. Thanks! You can require warnings, including incomplete patterns, with -Wall : {-# OPTIONS_GHC -Wall #-} module A where f :: Int -> Int f 0 = 0 Yielding: A.hs:6:1: Warning: Pattern match(es) are non-exhaustive In an equation for `f': Patterns not matched: GHC.Types.I# #x with #x

Scala Pattern Matching with Sets

流过昼夜 提交于 2019-12-04 23:11:28
The following doesn't work. object Foo { def union(s: Set[Int], t: Set[Int]): Set[Int] = t match { case isEmpty => s case (x:xs) => union(s + x, xs) case _ => throw new Error("bad input") } } error: not found: type xs How can I pattern match over a set? Well, x:xs means x of type xs , so it wouldn't work. But, alas, you can't pattern match sets, because sets do not have a defined order. Or, more pragmatically, because there's no extractor on Set . You can always define your own, though: object SetExtractor { def unapplySeq[T](s: Set[T]): Option[Seq[T]] = Some(s.toSeq) } For example: scala> Set

R Extract a word from a character string using pattern matching

别等时光非礼了梦想. 提交于 2019-12-04 22:55:29
I need some help with pattern matching in R. I need to extract a whole word that starts with a common prefix, from a long character string. The word I want to extract always starts with the same prefix (AA), but the word is not the same length, and does not occur in the same location of the string. mytext1 <- as.character("HORSE MONKEY LIZARD AA12345 SWORDFISH") # Return AA12345 mytext2 <- as.character("ELEPHANT AA100 KOALA POLAR.BEAR") # Want to return AA100 mytext3 <- as.character("CROCODILE DRAGON.FLY ANTELOPE") # Want to return NA As an extension of this, what if there were two different

Is it recommended to always have exhaustive pattern matches in Haskell, even for “impossible” cases?

我只是一个虾纸丫 提交于 2019-12-04 22:50:14
Is it recommended to always have exhaustive pattern matches in Haskell, even for "impossible" cases? For example, in the following code, I am pattern matching on the "accumulator" of a foldr. I am in complete control of the contents of the accumulator, because I create it (it is not passed to me as input, but rather built within my function). Therefore, I know certain patterns should never match it. If I strive to never get the "Pattern match(es) are non-exhaustive" error, then I would place a pattern match for it that simply error's with the message "This pattern should never happen." Much

PostgreSQL case insensitive SELECT on array

喜欢而已 提交于 2019-12-04 22:31:36
I'm having problems finding the answer here, on google or in the docs ... I need to do a case insensitive select against an array type. So if: value = {"Foo","bar","bAz"} I need SELECT value FROM table WHERE 'foo' = ANY(value) to match. I've tried lots of combinations of lower() with no success. ILIKE instead of = seems to work but I've always been nervous about LIKE - is that the best way? Craig Ringer One alternative not mentioned is to install the citext extension that comes with PostgreSQL 8.4+ and use an array of citext : regress=# CREATE EXTENSION citext; regress=# SELECT 'foo' = ANY( '{

Case Statements and Pattern Matching

孤街浪徒 提交于 2019-12-04 21:31:24
问题 I'm coding in SML for an assignment and I've done a few practice problems and I feel like I'm missing something- I feel like I'm using too many case statements. Here's what I'm doing and the problem statements for what I'm having trouble with.: Write a function all_except_option, which takes a string and a string list. Return NONE if the string is not in the list, else return SOME lst where lst is like the argument list except the string is not in it. fun all_except_option(str : string, lst :

Performance difference between pattern matching and if-else

耗尽温柔 提交于 2019-12-04 21:23:26
问题 Why can OCaml generate efficient machine code for pattern matching and not for if-else tests? I was reading Real World OCaml and I came across this section where they compared the performance of pattern matching to the performance of if-else tests. It turned out that pattern matching in their example was significantly faster than if-else tests. Even though the code doesn't utilize any special pattern match cases that would not be possible with if-else tests, it just compares integers. They