例如,我有两个Try对象.如果一个或另一个失败并以相同的方式处理它,我想得到错误:
val t1 = Try(throw new Exception("one"))
val t2 = Try(throw new Exception("two"))
(t1, t2) match {
case (Success(_), Success(_)) => println("It's ok")
case _ : Failure(e), _) | (_, Failure(e) => // Compile error here
println("Fail", e) // Doesn't matter from where e was come
}
Run Code Online (Sandbox Code Playgroud)
是否可以e在两个失败选项编译中使用相同的代码?
我有一个功能:
fold_wrap :: (a -> a -> a) -> (Prop -> a) -> a -> Wrapper -> a
fold_wrap v x z (Mrappe l r) = v ( v(v x z l) v(v x z r) )
fold_wrap v x z (Wrap f) = x f
fold_warp v x z (Wtail ) = z
Run Code Online (Sandbox Code Playgroud)
我在哪里遇到以下错误:
mast: mast.hs:(15,1)-(16,31): Non-exhaustive patterns in function fold_mast
Run Code Online (Sandbox Code Playgroud)
所以我补充道
fold_wrap v x z _ = z
Run Code Online (Sandbox Code Playgroud)
哪个修正了错误但是
这让我相信某些模式确实无可比拟.
现在我解决这个问题的直觉是打印传递给函数的内容.
所以我加了这个
fold_wrap v x z g = …Run Code Online (Sandbox Code Playgroud) 我试图使用正则表达式来匹配这样的模式:
(任何字母)(另一封信)(同一封信再次)
例如:
这些都是有效的例子:
aba
bcb
dbd
Run Code Online (Sandbox Code Playgroud)
这些都无效:
aab
aaa
bac
Run Code Online (Sandbox Code Playgroud)
我试图这样做:
(.)[^\1]\1
Run Code Online (Sandbox Code Playgroud)
但是,这仍然符合第二个字母与第一个字母相似的情况(例如:) aaa.见这里:http://rubular.com/r/TTGEcyhE9g
正则表达式中是否有匹配的方法any letter except the captured one?
在下面的第一个代码片段中,case语句在函数内定义,它按预期工作.
def echoWhatYouGaveMe(x: Any): String = x match {
case (a, b) => s"got $a and $b"
case (a, b, c) => s"got $a, $b, and $c"
case _ => "Unknown"
}
object MatchTest extends App {
// trigger the tuple patterns
println(echoWhatYouGaveMe((1,2))) // two element tuple
println(echoWhatYouGaveMe((1,2,3))) // three element tuple
}
MatchTest.main(Array("dummy"))
Run Code Online (Sandbox Code Playgroud)
有1和2
得到1分,2分和3分
下面的情况不在函数内,但在上面非常类似.它给出了一个错误.我理解错误,但我不明白为什么我在下面收到错误而不是上面的错误.
val myTuple = (1, 2, 3)
val toPrint = myTuple match {
case (a, b, c) => s"got $a, $b, …Run Code Online (Sandbox Code Playgroud) 假设我有两个列表:
First = [1,2,3,4].
Second = [1,2,3,4,234242].
Run Code Online (Sandbox Code Playgroud)
我知道第二个列表的所有元素都与第一个列表的元素匹配,但最后一个额外元素除外.如何使用模式匹配来获取最后一个?
要确定字符串是否是等值线图:
确定单词或短语是否是等值线图.等值线(也称为"非模式词")是没有重复字母的单词或短语,但允许空格和连字符多次出现.
我的尝试:
def is_isogram(string):
word = string.lower()
if word == "":
return False
elif word == " ":
return False
else:
for char in word:
if (word.count(char) > 1) and (char != " " or char != "-") and (len(word) > 0):
return False
else:
return True
Run Code Online (Sandbox Code Playgroud)
但是,当传递给函数的字符串为空并且2个中心字符相同时,这会失败.
这些是单元测试:
import unittest
from isogram import is_isogram
class IsogramTest(unittest.TestCase):
def test_empty_string(self):
self.assertIs(is_isogram(""), True)
def test_isogram_with_only_lower_case_characters(self):
self.assertIs(is_isogram("isogram"), True)
def test_word_with_one_duplicated_character(self):
self.assertIs(is_isogram("eleven"), False)
def test_word_with_one_duplicated_character_from_end_of_alphabet(self):
self.assertIs(is_isogram("zzyzx"), False)
def test_longest_reported_english_isogram(self):
self.assertIs(is_isogram("subdermatoglyphic"), True)
def test_word_with_duplicated_character_in_mixed_case(self): …Run Code Online (Sandbox Code Playgroud) 假设我想在Haskell中建模树结构
data Tree = Null | Node Tree Integer Tree deriving Show
Run Code Online (Sandbox Code Playgroud)
我想测试每个条目是否小于10.我想我会使用模式匹配和写入
isSmall :: Tree -> Bool
isSmall _
| Null = True
| (Node a b c) = if b >= 10
then False
else isSmall a && isSmall c
Run Code Online (Sandbox Code Playgroud)
然而,它提供了有关的错误a,b和c被淘汰的范围.我本以为把他们放在守卫里基本上会把他们放在范围内.这不是你应该如何在Haskell中进行模式匹配吗?我四处寻找可以指导我的示例,但我没有在使用由其他几种数据结构组成的数据结构的警卫中找到任何模式匹配的示例.
错误:
test.hs:24:6: Not in scope: data constructor ‘Node’
test.hs:24:11: Not in scope: ‘a’
test.hs:24:13: Not in scope: ‘b’
test.hs:24:15: Not in scope: ‘c’
test.hs:24:27: Not in scope: ‘b’
test.hs:26:38: Not in …Run Code Online (Sandbox Code Playgroud) 当我第一次看到function在OCaml中使用关键字时,我得到的印象是,消除match x with模式匹配的行是语法糖.
但是,我发现两者之间存在签名差异,如下例所示.在什么情况下你想使用这个function例子?
type e = Foo | Bar
let eval1 exp =
match exp with
| Foo -> "Foo"
| Bar -> "Bar"
let eval2 exp = function
| Foo -> "Foo"
| Bar -> "Bar"
Run Code Online (Sandbox Code Playgroud)
第一个函数有一个签名 val eval1 : e -> bytes = <fun>
第二个函数有一个签名 val eval2 : 'a -> e -> bytes = <fun>
所以我正在尝试构建一个函数,它接受一个元组列表并找到具有最大第二元素的元组.但我得到一个模式匹配错误.
这是我的代码.
resultTuple :: [((Int,Int),Int)] -> (Int,Int)
resultTuple [] = error "something wrong"
resultTuple [x] = fst(x)
resultTuple (x:y:xs)
| snd(x) >= snd(y) = resultTuple(x:xs)
| snd(x) < snd(y) = resultTuple(y:xs)
Run Code Online (Sandbox Code Playgroud)
这是我的错误.
Pattern match(es) are non-exhaustive
In an equation for ‘resultTuple’: Patterns not matched: (_:_:_)
Run Code Online (Sandbox Code Playgroud) 我正在阅读一些Rust代码,我遇到了这一行
if let Some(path) = env::args().nth(1) {
Run Code Online (Sandbox Code Playgroud)
在这个功能里面
fn main() {
if let Some(path) = env::args().nth(1) {
// Try reading the file provided by the path.
let mut file = File::open(path).expect("Failed reading file.");
let mut content = String::new();
file.read_to_string(&mut content);
perform_conversion(content.as_str()).expect("Conversion failed.");
} else {
println!(
"provide a path to a .cue file to be converted into a MusicBrainz compatible tracklist."
)
}
}
Run Code Online (Sandbox Code Playgroud)
该行似乎是将env参数分配给变量路径,但我无法弄清楚Some()它周围正在做什么.
我看了一下文档Option,我理解它在右侧使用时是如何工作的=但是在左侧我有点困惑.
我是否认为这条线相当于
if let path = Some(env::args().nth(1)) …Run Code Online (Sandbox Code Playgroud)