现在我知道,在使用 Haskell 声明函数签名后,我可以使用函数重载进行模式匹配,如下所示:
frog :: Int -> String
frog 1 = "Ribbit"
frog 7 = "Croak"
frog 12 = "Pop!"
frog x = replicate x "Z"
Run Code Online (Sandbox Code Playgroud)
我知道我也可以以类似的方式使用守卫:
frog :: Int -> String
frog x =
| x == 1 = "Ribbit"
| x == 7 = "Croak"
| x == 12 = "Pop!"
| otherwise = replicate x "Z"
Run Code Online (Sandbox Code Playgroud)
然而,我宁愿结合使用布尔防护和模式来确定将执行哪个臂的两种方式。类似于这个 Rust 片段的东西:
fn frog(x: u32) -> String {
match x {
k if k >= 1000 => todo!()
k if …Run Code Online (Sandbox Code Playgroud) 我尝试通过迭代一个带有空列表的列表来编写complst自己的解决方案,其中所有非重复项都被插入然后返回。在查找解决方案后,我知道这是一种过于复杂的方法,但仍然想了解为什么模式匹配不能按预期工作:
let compress list =
let rec aux complst lst =
match lst with
| [] -> complst
| a :: (b :: c) -> if a = b then aux complst (b::c) else aux (a::complst) (b::c)
| x -> x
in aux [] list;;
val comp : 'a list -> 'a list = <fun>
Run Code Online (Sandbox Code Playgroud)
无论输入如何,输出始终是一个仅包含最后一个元素的列表:
compress [1;1;2;2;3];;
- : int list = [3]
compress [1;2;3];;
- : int list = [3]
Run Code Online (Sandbox Code Playgroud) recursion ocaml functional-programming list pattern-matching
我正在编写一个函数,该函数接受列表输入、创建子列表并检索输出到新列表中的 n 个元素。我正在根据输入的值编写防护,但我不断收到错误“模式不匹配:( : )_”。
\n有人确定这个问题吗?
\nnKsets :: [Int] -> Int -> [[Int]]\nnKsets [] _ = error "Empty list should not be given as input"\nnKsets l n\n | n <= 0 = []\n | n > 0 = ...\nRun Code Online (Sandbox Code Playgroud)\n我收到的错误是:
\nPattern match(es) are non-exhaustive\nIn an equation for \xe2\x80\x98nKsets\xe2\x80\x99: Patterns not matched: (_:_) _\nRun Code Online (Sandbox Code Playgroud)\n 我正在尝试编写一个函数(字符计数),它接受一个模式和一个字符串,然后返回一个数字(计数),该数字表示模式中的任何字符在字符串中出现的次数。
例如:
(char-count "Bb" "Best buy")
会返回,2因为 1 场比赛B和 1 场比赛b,所以加在一起我们得到2
(char-count "AaR" "A Tale of Recursion")
会回来3等等
我尝试re-seq在我的函数中使用,但它似乎只适用于连续字符串。因为(re-seq #Bb "Best Buy)仅查找模式Bb,而不查找每个单独的字符。
到目前为止,我的函数是这样的:
(defn char-count [pattern text]
(count (re-seq (#(pattern)) text)))
Run Code Online (Sandbox Code Playgroud)
但它并没有达到我想要的效果。有人可以帮忙吗?
Ps 对 clojure(以及一般的函数式编程)非常陌生。
character clojure pattern-matching string-matching clojure-repl
是否可以对枚举的变体进行通配符,同时仍然计算其关联数据?
enum BAR {
BAR1,
BAR2,
}
enum BAZ {
BAZ1,
BAZ2,
BAZ3,
}
enum FOO {
FOO1(BAR, BAZ),
FOO2(BAR, BAZ),
FOO3(BAR, BAZ),
//...
}
impl FOO {
pub fn getBar(&self) -> &BAR {
return match self {
FOO::FOO1(bar, _) => bar,
// _(bar, _) => bar,
}
}
}
Run Code Online (Sandbox Code Playgroud)
枚举 FOO 有 50 多个变体。该产品线FOO::FOO1(bar, _) => bar,满足了我的需要,但列出所有 50 个变体会非常难看,因为所有手臂基本上看起来都一样。是否存在某种形式的_(bar, _) => bar,变体被通配符但相关数据仍然可检索的情况?
在下面的代码中,result保证是1,或者没有顺序保证并且它也可以返回 吗2?
record Foo
{
bool X { get; init; }
bool Y { get; init; }
bool Z { get; init; }
}
var foo = new Foo { X = true, Y = true };
var result = foo switch
{
{ X: true } => 1,
{ Y: true } => 2,
{ Z: true } => 3,
_ => 0
}
Run Code Online (Sandbox Code Playgroud) 我想foo接受两个参数,如果第一个参数是:ok它应该返回第二个参数,否则返回第一个参数。
我想用这样的模式匹配来做到这一点:
(defn foo [:ok val] (val))
(defn foo [key val] (key))
Run Code Online (Sandbox Code Playgroud)
但不支持。我可以用类似的方式(使用模式匹配或某种)来做到这一点吗?
无法理解Google 综合 Rust 的模式匹配 - 解构结构中的第二个演讲者注释点。
\n这是示例代码。
\nstruct Foo {\n x: (u32, u32),\n y: u32,\n}\n\n#[rustfmt::skip]\nfn main() {\n let foo = Foo { x: (1, 2), y: 3 };\n match foo {\n Foo { x: (1, b), y } => println!("x.0 = 1, b = {b}, y = {y}"),\n Foo { y: 2, x: i } => println!("y = 2, x = {i:?}"),\n Foo { y, .. } => println!("y = {y}, other fields were ignored"),\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n … 我一直在尝试使用 Java 21 中的记录模式进行模式匹配的示例。官方文档断言空值与任何记录模式都不匹配。但是,我尝试这个例子:
record Point(Integer x, Integer y) {}
public class MainPatternMatchingRecord {
public static void main(String[] args) {
printSum(new Point(null, 2));
}
private static void printSum(Object obj) {
if (obj instanceof Point(var x, var y)) {
System.out.println(x + 1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,根据我对 JEP 的理解,new Point(null, 2)不应该匹配 in instanceof Point(var x, var y),但是当运行程序时,会抛出此异常:
线程“main”中的异常 java.lang.NullPointerException:无法调用“java.lang.Integer.intValue()”,因为“x”为 null
为什么会出现这种行为?应该如何正确解释空值与任何记录模式不匹配?
我想删除CSS文件中包含单词"color"的所有行.
这将包括:
body {background-color:#000;}
div {color:#fff;}
Run Code Online (Sandbox Code Playgroud)
你会怎么用:%s /命令?