pattern-matching

Case Statements and Pattern Matching

心不动则不痛 提交于 2019-12-03 13:28:32
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 : string list) = case lst of [] => NONE | x::xs => case same_string(x, str) of true => SOME xs | false =

Unexpected Scala pattern matching syntax

独自空忆成欢 提交于 2019-12-03 12:54:45
I had a List of Scala tuples like the following: val l = List((1,2),(2,3),(3,4)) and I wanted to map it in a list of Int where each item is the sum of the Ints in a the corresponding tuple. I also didn't want to use to use the x._1 notation so I solved the problem with a pattern matching like this def addTuple(t: (Int, Int)) : Int = t match { case (first, second) => first + second } var r = l map addTuple Doing that I obtained the list r: List[Int] = List(3, 5, 7) as expected. At this point, almost by accident, I discovered that I can achieve the same result with an abbreviated form like the

How to string.find the square bracket character in lua?

南笙酒味 提交于 2019-12-03 12:52:22
So I'm trying to find square brackets inside a string: s = "testing [something] something else" x,y = string.find(s,"[") which gives me an error: malformed pattern (missing ']'). I also tried: x,y = string.find(s,"\[") giving me the same error. And this: x,y = string.find(s,"\\[") in this case x and y are nil. Any thoughts on how to do this properly? Thanks in advance. John's answer will work -- turning off pattern matching. What you're trying to do -- escape the [ -- is done with the % character in Lua: x,y = string.find(s,'%[') Also strings in Lua all have the string module as their

View patterns vs. pattern guards

别说谁变了你拦得住时间么 提交于 2019-12-03 12:45:05
问题 I'm trying to get a sense of the relationship between view patterns and pattern guards in GHC. Pattern guards seem quite intuitive, while view patterns seem a bit confusing. It kind of looks like view patterns are better for dealing with things deep in a pattern, while pattern guards can reuse a view more intuitively, but I don't quite get it. 回答1: View patterns have significant overlap with pattern guards. The main advantage of view patterns is that they can be nested, and avoid introducing

multiline regexp matching in bash

こ雲淡風輕ζ 提交于 2019-12-03 11:53:52
I would like to do some multiline matching with bash's =~ #!/bin/bash str='foo = 1 2 3 bar = what about 42? boo = more words ' re='bar = (.*)' if [[ "$str" =~ $re ]]; then echo "${BASH_REMATCH[1]}" else echo no match fi Almost there, but if I use ^ or $ , it will not match, and if I don't use them, . eats newlines too. EDIT: sorry, values after = could be multi-word values. I could be wrong, but after a quick read from here , especially Note 2 at the end of the page, bash can sometimes include the newline character when matching with the dot operator. Therefore, a quick solution would be: #!

How to identify different objects in an image?

吃可爱长大的小学妹 提交于 2019-12-03 11:50:48
问题 I'm intending to write a program to detect and differentiate certain objects from a nearly solid background. The foreground and the background have a high contrast difference which I would further increase to aid in the object identification process. I'm planning to use Hough transform technique and OpenCV. Sample image As seen in the above image, I would want to separately identify the circular objects and the square objects (or any other shape out of a finite set of shapes). Since I'm quite

How to use switch/case (simple pattern matching) in Scala?

允我心安 提交于 2019-12-03 11:27:18
问题 I've found myself stuck on a very trivial thing :-] I've got an enum: object Eny extends Enumeration { type Eny = Value val FOO, BAR, WOOZLE, DOOZLE = Value } In a code I have to convert it conditionally to a number (varianr-number correspondence differs on context). I write: val en = BAR val num = en match { case FOO => 4 case BAR => 5 case WOOZLE => 6 case DOOZLE => 7 } And this gives me an "unreachable code" compiler error for every branch but whatever is the first ("case FOO => 4" in this

What's the reasoning behind adding the “case” keyword to Scala?

删除回忆录丶 提交于 2019-12-03 11:23:26
Apart from: case class A ... case which is quite useful? Why do we need to use case in match ? Wouldn't: x match { y if y > 0 => y * 2 _ => -1 } ... be much prettier and concise? Or why do we need to use case when a function takes a tuple? Say, we have: val z = List((1, -1), (2, -2), (3, -3)).zipWithIndex Now, isn't: z map { case ((a, b), i) => a + b + i } ... way uglier than just: z map (((a, b), i) => a + b + i) ...? The second issue, anonymous functions that avoid the case , is a matter of debate: https://groups.google.com/d/msg/scala-debate/Q0CTZNOekWk/z1eg3dTkCXoJ Also: http://www.scala

Mathematica's pattern matching poorly optimized?

女生的网名这么多〃 提交于 2019-12-03 11:02:58
I recently inquired about why PatternTest was causing a multitude of needless evaluations: PatternTest not optimized? Leonid replied that it is necessary for what seems to me as a rather questionable method. I can accept that, though I would prefer a more efficient alternative. I now realize, which I believe Leonid has been saying for some time, that this problem runs much deeper in Mathematica , and I am troubled. I cannot understand why this is not or cannot be better optimized. Consider this example: list = RandomReal[9, 20000]; Head /@ list; // Timing MatchQ[list, {x__Integer, y__}] //

In Scala, why is NaN not being picked up by pattern matching?

霸气de小男生 提交于 2019-12-03 10:35:31
问题 My method is as follows def myMethod(myDouble: Double): Double = myDouble match { case Double.NaN => ... case _ => ... } The IntelliJ debugger is showing NaN but this is not being picked up in my pattern matching. Are there possible cases I am omitting 回答1: It is a general rule how 64-bit floating point numbers are compared according to IEEE 754 (not Scala or even Java related, see NaN): double n1 = Double.NaN; double n2 = Double.NaN; System.out.println(n1 == n2); //false The idea is that NaN