我遇到了一个我不太懂的问题.我以为我可以在Haskell中编写这样的代码:
foo :: Maybe Int -> Int
foo Nothing = 0
foo Just x = x
Run Code Online (Sandbox Code Playgroud)
但是当我尝试编译它时,我收到错误:
'foo'的等式具有不同数量的参数
我可以通过将我的代码更改为以下代码来修复它:
foo :: Maybe Int -> Int
foo Nothing = 0
foo (Just x) = x
Run Code Online (Sandbox Code Playgroud)
这让我觉得GHC正在解释Just为一个论据foo.但是Haskell禁止使用大写字母来启动变量名,所以我不认为这里应该有任何歧义.这是怎么回事?
考虑下面的代码:
val first = ...
val second = ...
val third = ...
val fours = ...
first match {
case "someString" => second match {
case s:String => third match {
case MyEnum.A => //some logic
case MyEnum.B => fours match {
case Some(old:String) => //some other logic
case default=> defaulLogic
}
case default=> defaulLogic
}
case default=> defaulLogic
}
case default=> defaulLogic
}
private def defaulLogic()= {
//log error here
}
Run Code Online (Sandbox Code Playgroud)
有没有办法将case default逻辑放在一个地方而不是在每个模式匹配中复制它?
注意
只有顶部的match-case …
模式匹配是否可以匹配一系列值?例如 :
有没有办法使用以下形式的区别联合与活动模式匹配?我找不到任何例子.
这就是我想要做的:
type c = a | b
type foo =
| bar1
| bar2 of c
//allowed
let (|MatchFoo1|_|) aString =
match aString with
| "abcd" -> Some bar1
| _ -> None
//not allowed
let (|MatchFoo2|_|) aString =
match aString with
| "abcd" -> Some (bar2 of a)
| _ -> None
Run Code Online (Sandbox Code Playgroud)
为什么"Some"不能以第二种方式使用?还有另一种方法可以达到同样的目的吗?
这无法编译:
Array[Byte](...) match { case Array(0xFE.toByte, 0xFF.toByte, tail @ _* ) => tail }
Run Code Online (Sandbox Code Playgroud)
但是,我可以编译这个:
val FE = 0xFE.toByte
val FF = 0xFF.toByte
bytes match { Array( FE, FF, tail @ _* ) => tail }
Run Code Online (Sandbox Code Playgroud)
为什么第一种情况不能编译而不是第二种情况?
我想在Haskell中将Maybe Int转换为Int,如下所示:
convert :: Maybe Int -> Int
convert mx = case mx of
Just x -> x
Nothing -> error "error message"
Run Code Online (Sandbox Code Playgroud)
当我编译它时,Haskell告诉我:parse error on input 'Nothing'.
我需要这个,因为我想从Data.List模块中使用elem.Index函数获取列表中元素的索引,然后在take函数上使用此索引.我的问题是elemIndex返回a Maybe Int,但take需要一个Int.
1> X = 10.
10
2> Y = 9.
9
3> X - 1 = Y.
* 1: illegal pattern
4> Y = X - 1.
9
5> 10 - 1 = Y.
9
Run Code Online (Sandbox Code Playgroud)
你能解释一下查询3>中的非法模式是什么吗?谢谢!
这是对jsonb键/值的模式匹配的后续跟进
我有一张桌子如下
CREATE TABLE "PreStage".transaction (
transaction_id serial NOT NULL,
transaction jsonb
CONSTRAINT pk_transaction PRIMARY KEY (transaction_id)
);
Run Code Online (Sandbox Code Playgroud)
我的事务jsonb列中的内容看起来像
{"ADDR": "abcd", "CITY": "abcd", "PROV": "",
"ADDR2": "",
"ADDR3": "","CNSNT": "Research-NA", "CNTRY": "NL", "EMAIL": "@.com",
"PHONE": "12345", "HCO_NM": "HELLO", "UNQ_ID": "",
"PSTL_CD": "1234", "HCP_SR_NM": "", "HCP_FST_NM": "",
"HCP_MID_NM": ""}
Run Code Online (Sandbox Code Playgroud)
我需要搜索查询,如:
SELECT transaction AS data FROM "PreStage".transaction
WHERE transaction->>'HCP_FST_NM' ILIKE '%neer%';
Run Code Online (Sandbox Code Playgroud)
但我需要让我的用户灵活地搜索任何键/值.
对上一个问题的回答建议将索引创建为:
CREATE INDEX idxgin ON "PreStage".transaction
USING gin ((transaction->>'HCP_FST_NM') gin_trgm_ops);
Run Code Online (Sandbox Code Playgroud)
哪个有效,但我也想索引其他键.因此尝试了类似的事情:
CREATE INDEX idxgin ON "PreStage".transaction USING gin …Run Code Online (Sandbox Code Playgroud) 我正在尝试根据比例模式匹配:
isValid :: Ratio Int -> Bool
isValid (num % den) = ...
Run Code Online (Sandbox Code Playgroud)
但是,这会产生:
Parse error in pattern: num % den
Run Code Online (Sandbox Code Playgroud)
有趣的是,Data.Ratio包以这种方式定义numerator和denominator功能,但与:%运营商:
numerator (x :% _) = x
denominator (_ :% y) = y
Run Code Online (Sandbox Code Playgroud)
但是,我无法访问后一种运营商.
任何人都可以解释为什么我的模式匹配不起作用以及我如何解决它?
这可能很简单,但有人可以解释为什么下面的模式匹配不合理吗?它说其他规则,例如1,0,_永远不会匹配.
let matchTest(n : int) =
let ran = new Random()
let i = ran.Next(0, 2)
match i with
| n -> printfn "%i" n
| 1 -> printfn "1"
| 0 -> printfn "0"
| _ -> printfn "impossible"
Run Code Online (Sandbox Code Playgroud)
同样的事情:
let matchTest(n : int) =
let ran = new Random()
let i = ran.Next(0, 2)
let m = n
match i with
| m -> printfn "%i" m
| 1 -> printfn "1"
| 0 -> printfn "0"
| …Run Code Online (Sandbox Code Playgroud) pattern-matching ×10
haskell ×4
f# ×2
scala ×2
erlang ×1
ghc ×1
indexing ×1
jsonb ×1
maybe ×1
parse-error ×1
postgresql ×1