pattern-matching

Prove a match statement

让人想犯罪 __ 提交于 2021-01-27 07:24:08
问题 Trying to solve an exercise, I have the following definition that represents the integers : Inductive bin : Type := | Zero : bin | Twice : bin -> bin | TwiceOne : bin -> bin. The idea is that : Twice x is 2*x . TwiceOne x is 2*x +1 . However, this representation has a problem: there are several representations of the number 0 . Therefore, I need to implement a function that normalize a number writing in bin . To do this I have declared the following function : Fixpoint normalize_bin (b:bin) :

Prove a match statement

爷,独闯天下 提交于 2021-01-27 07:20:34
问题 Trying to solve an exercise, I have the following definition that represents the integers : Inductive bin : Type := | Zero : bin | Twice : bin -> bin | TwiceOne : bin -> bin. The idea is that : Twice x is 2*x . TwiceOne x is 2*x +1 . However, this representation has a problem: there are several representations of the number 0 . Therefore, I need to implement a function that normalize a number writing in bin . To do this I have declared the following function : Fixpoint normalize_bin (b:bin) :

Haskell/GHC: Matching multiple unary constructors with the same pattern

主宰稳场 提交于 2021-01-27 06:53:33
问题 So I was playing around with defining a TrieSet datatype (even though I know I don't need to): module Temp where import Data.Map data TrieSet a = Nonterminal (Data.Map a (TrieSet a)) | Terminal (Data.Map a (TrieSet a)) insert :: Ord a => [a] -> TrieSet a -> TrieSet a insert [] (_ m) = Terminal m insert (a:as) (c m) = c $ insertWith (insert as . flip const) a (insert as $ Nonterminal empty) m When I got an error I've never seen before: % ghc -c Temp.hs Temp.hs:8:11: Parse error in pattern So

Splitting a string based on Pattern Java

一世执手 提交于 2021-01-24 08:30:48
问题 Hi I have log files of the following pattern- 2014-03-06 03:21:45,432 ERROR [mfs:pool-3-thread-19] dispatcher.StatusNotification - Error processing notification. Operation aborted. java.sql.SQLException: Network error IOException: Connection timed out: connect 2014-03-06 03:22:06,454 ERROR [mfs:pool-3-thread-19] dispatcher.ClientStatusNotification - Error processing notification. Operation aborted. java.sql.SQLException: Network error IOException: Connection timed out: connect 2014-03-06 03

F# How to catch all exceptions

核能气质少年 提交于 2021-01-23 06:25:31
问题 I know how to catch specific exceptions as in the following example: let test_zip_archive candidate_zip_archive = let rc = try ZipFile.Open(candidate_zip_archive.ToString(), ZipArchiveMode.Read) |> ignore zip_file_ok with | :? System.IO.InvalidDataException -> not_a_zip_file | :? System.IO.FileNotFoundException -> file_not_found | :? System.NotSupportedException -> unsupported_exception rc I am reading a bunch of articles to see if I can use a generic exception in the with , like a wildcard

Getting MatchError when using a placeholder for an unused variable

泪湿孤枕 提交于 2021-01-23 04:49:37
问题 With Scala 2.13.x, I am getting scala.MatchError: null when I use a placeholder for an unused variable: scala> object Test { | val _: Any = null | } object Test scala> Test scala.MatchError: null ... 41 elided But with Scala 2.12.x, I am not getting scala.MatchError: null : scala> object Test { | val _: Any = null | } defined object Test scala> Test res1: Test.type = Test$@784c5ef5 Any reason? 回答1: As stated in scala 2.13 release notes: Underscore is no longer a legal identifier unless

Why is `ref` used instead of an asterisk in pattern matching?

拥有回忆 提交于 2021-01-07 02:52:32
问题 I am having trouble trying to understand pattern matching rules in Rust. I originally thought that the idea behind patterns are to match the left-hand side and right-hand side like so: struct S { x: i32, y: (i32, i32) } let S { x: a, y: (b, c) } = S { x: 1, y: (2, 3) }; // `a` matches `1`, `(b, c)` matches `(2, 3)` However, when we want to bind a reference to a value on the right-hand side, we need to use the ref keyword. let &(ref a, ref b) = &(3, 4); This feels rather inconsistent. Why can

Why is `ref` used instead of an asterisk in pattern matching?

筅森魡賤 提交于 2021-01-07 02:52:25
问题 I am having trouble trying to understand pattern matching rules in Rust. I originally thought that the idea behind patterns are to match the left-hand side and right-hand side like so: struct S { x: i32, y: (i32, i32) } let S { x: a, y: (b, c) } = S { x: 1, y: (2, 3) }; // `a` matches `1`, `(b, c)` matches `(2, 3)` However, when we want to bind a reference to a value on the right-hand side, we need to use the ref keyword. let &(ref a, ref b) = &(3, 4); This feels rather inconsistent. Why can

Pattern matching against type-member with Aux-pattern

心已入冬 提交于 2020-12-31 06:09:25
问题 Consider: sealed trait A case object B extends A case object C extends A case object D extends A sealed trait Test { type AA <: A val aa: AA } object Test { type Aux[AAA <: A] = Test { type AA = AAA } } def compilesOk(t: Test) = t.aa match { case _: B.type => println("B") case _: C.type => println("C") case _: D.type => println("D") } def compileError(t: Test) = t.aa match { case B => println("B") case C => println("C") case D => println("D") } The compileError function fails to compile with

Pattern matching against type-member with Aux-pattern

丶灬走出姿态 提交于 2020-12-31 06:05:27
问题 Consider: sealed trait A case object B extends A case object C extends A case object D extends A sealed trait Test { type AA <: A val aa: AA } object Test { type Aux[AAA <: A] = Test { type AA = AAA } } def compilesOk(t: Test) = t.aa match { case _: B.type => println("B") case _: C.type => println("C") case _: D.type => println("D") } def compileError(t: Test) = t.aa match { case B => println("B") case C => println("C") case D => println("D") } The compileError function fails to compile with