我正在尝试将以下模式匹配函数转换为匹配表达式:
let reverse ls =
let rec rev acc =
function
| h :: t -> rev (h :: acc) t
| [] -> acc
rev [] ls
Run Code Online (Sandbox Code Playgroud)
当我尝试转换为等效匹配表达式类型时,会发生不匹配错误:
let reverse ls =
let rec rev acc =
match acc with
| h :: t -> rev (h :: acc) t
| [] -> acc
rev [] ls
Run Code Online (Sandbox Code Playgroud)
两者所需的输出是:
reverse [ 1; 2; 3 ]
// val it : int list = [3; 2; 1]
Run Code Online (Sandbox Code Playgroud) 我已经使用clap.
在其中,我有几个结构体和枚举,其中包含特定子命令的这些结构体。
我现在遇到了某种类型的代码重复问题,想知道 Rust 语言是否有一些我不知道的功能可以消除重复。
这是一个 MRE:
pub trait Runner {
fn run(&self);
}
pub struct Foo;
impl Runner for Foo {
fn run(&self) {
println!("Fooing");
}
}
pub struct Bar;
impl Runner for Bar {
fn run(&self) {
println!("Baring");
}
}
pub enum Action {
DoFoo(Foo),
DoBar(Bar),
}
impl Runner for Action {
fn run(&self) {
match self {
Self::DoFoo(runner) => runner.run(),
Self::DoBar(runner) => runner.run(),
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,模式匹配<Action as Runner>::run()在所有变体上都具有几乎相同的代码,因为所有枚举变体的元组都包含一个实现Runner.
在这个 …
我有以下格式的输入字符串
first|second|third|<forth>|<fifth>|$sixth我想将此字符串拆分为一个字符串数组,其值为[first,second,third ,,, $ six].我使用以下代码来分割字符串,但这不起作用.请帮我.
public String[] splitString(String input){
String[] resultArray = input.split("|")
return resultArray;
}
Run Code Online (Sandbox Code Playgroud)
你能告诉我我做错了什么吗?
我正在使用以下形式的元组填充的列表:
tups = [(1, 2, 4.56), (2, 1, 1.23), (1, 3, 2.776), ...]
Run Code Online (Sandbox Code Playgroud)
我想执行两个操作.
第一个是查找以数字n开头的所有元组,例如:
def starting_with(n, tups):
'''Find all tuples with tups that are of the form (n, _, _).'''
# ...
Run Code Online (Sandbox Code Playgroud)
第二个是相反的,找到第二个值为n的所有元组:
def middle_with(n, tups):
'''Find all tuples with tups that are of the form (_, n, _).'''
# ...
Run Code Online (Sandbox Code Playgroud)
从某种意义上说,在元组列表上进行模式匹配.我如何在Python中执行此操作?
假设我有一个名为foo的ADT
data foo = N Integer | V Var | Div foo foo
Run Code Online (Sandbox Code Playgroud)
有没有办法在模式匹配中使用ADT,所以我不必写出每种可能的数据类型组合?
myfunc :: foo -> [Var]
myfunc (N _) = []
myfunc (V a) = [a]
myfunc (Div (foo) (foo)) = myfunc(foo) ++ myfunc(foo)
Run Code Online (Sandbox Code Playgroud)
有没有办法做这样的工作,所以我不必写
myfunc (Div (N a) (N b)) = myfunc(N a) ++ myfunc(N b)
myfunc (Div (V a) (N b)) = myfunc(V a) ++ myfunc(N b)
myfunc (Div (N a) (V b)) = myfunc(N a) ++ myfunc(V b)
...
Run Code Online (Sandbox Code Playgroud)
等等
我对正则表达式的概念很新,所以我希望专家用户可以帮助我制作正确的表达式来查找字符串中的所有匹配项.我有一个字符串,它代表了漏洞数据的大量支持信息.在该字符串中是一系列CVE引用,格式为:CVE-2015-4000.任何人都可以提供一个样本正则表达式来查找所有出现的情况吗?很明显,整个字符串中的数字部分会发生变化......
f x zero = Nothing
f x y = Just $ x / y
where zero = 0
Run Code Online (Sandbox Code Playgroud)
文字绑定标识符zero仅在警告后匹配所有标识符Pattern match(es) are overlapped.
对于以下代码,我继续收到编译器错误,因为我正在编写(List _)并且它正在尝试对列表进行模式匹配.我该如何解决这个问题?
data List a = Cons a (List a) | Empty
instance Monad List where
Empty >>= _ = Empty
Cons x (List _) >>= f = f x
Run Code Online (Sandbox Code Playgroud) 我想将一个列表作为参数传递给一个函数,该函数将该列表的每个元素乘以3。我必须使用递归(我知道该怎么做)和map函数(有问题)。
我正在尝试将列表作为参数传递,就像我在其他帖子中看到的那样,但是它不起作用。
fun x = 3 * x + 1
mult :: [Int] -> [Int]
mult [a] = map fun [a]
Run Code Online (Sandbox Code Playgroud)
我尝试的代码显示:异常:x:函数mult中的非穷举模式
我知道我可以使用守卫来检查列表中是否出现一个值,但我想知道是否也可以单独使用模式匹配。
-- Using guards
f :: [Int] -> Int
f xs
| 42 `elem` xs = 42
| otherwise = 0
-- Using pattern matching?
g :: [Int] -> Int
g (_:)*42:_ = 42 -- i.e. zero or more elements: we discard until 42, followed by whatever.
g _ = 0
Run Code Online (Sandbox Code Playgroud)