我正在执行以下操作,但收到以下警告:
'let' 模式无效;子模式没有绑定任何变量
enum Seat {
case middle
case window
case aisle(Int)
}
let m : Seat = .middle
let w : Seat = .window
let a : Seat = .aisle(5)
let seats = [m,w,a]
for seat in seats {
if case let .middle = seat {
print("middle")
}
if case let .window = seat {
print("window")
}
if case let Seat.aisle(row) = seat {
print("able to let row be the associatedvalue of seat; its value is: \(row)")
}
}
Run Code Online (Sandbox Code Playgroud) 我有两个数据框,它们的列的 ID 号应该匹配。但是,这两个数据帧没有相同数量的 ID。一个有 118 个 ID,一个有 103 个 ID。具有 118 个 ID 的数据帧虽然包含所有 103 个 ID,但还有一些额外内容。我想从 118 行数据框中取出两列,并根据 ID 号将它们与 103 行数据框中匹配的 ID 合并。我不知道解决这个问题的最简单方法。
我正在尝试编写一个函数来比较 Common Lisp 中的两个列表。
符号“&”表示任何元素序列,符号“$”表示任何单个元素。
(defun match (filter data)
(cond
((atom filter) (eq filter data))
((atom data) NIL)
((eq (car filter) '|$|)
(match (cdr filter) (cdr data)))
((eq (car filter) '|&|)
(cond
((match (cdr filter) data))
(data (match filter (cdr data)))))
((match (car filter) (car data))
(match (cdr filter) (cdr data)))))
Run Code Online (Sandbox Code Playgroud)
工作正常:
(match '(a b c) '(a b c)) ; => T
(match '(a $ c) '(a b c)) ; => T
(match '(a ff c) '(a b c)) ; …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种在字符串中搜索一组非常特定的字符的方法: "(),:;<>@[\]
specialChar = str:find("[\"][%(][%)][,][:][;][<][>][@][%[][%]][\\]")
Run Code Online (Sandbox Code Playgroud)
我认为由于Lua 模式的局限性,没有任何模式可以满足我的需求。
我已经非常彻底地阅读了Lua 手册模式匹配部分,但似乎仍然无法弄清楚。
有谁知道我可以识别给定字符串是否包含这些字符中的任何一个的方法?
请注意,如果有帮助,我不需要知道它在字符串中的哪个字符或位置。
我一直在研究一个问题,目标是获取一个字符串并在存在模式时返回最长的重复模式。例如,如果字符串是“aabbbaa”,则将返回“yes aa”。如果字符串是“aavbbbvaabbb”,则返回“yes bbb”。我看了一下别人的解决方案,相信除了这一行似乎是关键之外,还可以理解每一段代码:copy[idx..idx2]=""
这是整个解决方案:
def PatternChaser(str)
patterns = {}
(0..str.length-1).each do |idx|
(idx + 1..str.length - 1).each do |idx2|
copy = str.dup
chunk = str[idx..idx2]
len = chunk.length
copy[idx..idx2] = ""
patterns[chunk] = len if copy.include?(chunk) && !patterns.include?(chunk)
end
end
return "no null" if patterns.empty?
patterns.each { |k,v| return "yes" + " " + k if v == patterns.values.max }
end
puts PatternChaser(STDIN.gets)
Run Code Online (Sandbox Code Playgroud)
如果这是相当常见的语法,请原谅我。到目前为止,我编码的时间还不到一年,到目前为止还没有在 StackOverflow 上找到答案。帮助将不胜感激!:)
我有一个枚举:
enum MyEnum {
A(i32),
B(i32),
C,
D,
// ...
}
Run Code Online (Sandbox Code Playgroud)
我可以匹配A,并B与像这样同时:
let a = MyEnum::A(1);
match a {
_(n) => { // do something for all variants which have one i32 value
}
_ => { // ignore the rest
}
};
Run Code Online (Sandbox Code Playgroud)
有什么办法可以做到这一点吗?我是否必须匹配所有变体并为每个变体应用相同的“主体”?
我有这样的功能:
eval :: Expr -> Either ArithmeticError Int
eval (Const a) = Right a
eval (Add a b) = liftM2 (+) ea eb
where
ea = eval a
eb = eval b
eval (Sub a b) = liftM2 (-) ea eb
where
ea = eval a
eb = eval b
Run Code Online (Sandbox Code Playgroud)
我想用一种where用法重写它。我可以做吗?但是模式匹配应该留在这段代码中。谢谢!
代码:
let x = Some(3);
if x == Some(3) {
println!("if case");
}
if let Some(3) = x {
println!("if let case");
}
Run Code Online (Sandbox Code Playgroud)
结果:
if case
if let case
Run Code Online (Sandbox Code Playgroud)
为什么 Rust 程序员使用“if let”?
如果在理解为什么我在 repl.it 上运行下面的代码不会停止时遇到问题。
-- The second argument is meant to be an infinite ascending list, don't bother yourself with any other case
isin :: Int -> [Int] -> (Bool, [Int])
isin n [] = (False, []) -- This case is unnecessary because the list is infinite, but just for completion
isin n l@(x:xs) =
case n `compare` x of
LT -> (False, l)
EQ -> (True, xs)
GT -> isin n xs
>>> isin 2 [1..]
-- prints nothing --Edit …Run Code Online (Sandbox Code Playgroud) 我试图编写一个类型为forall n, option (n = 1).
我选择option作为reflect避免提供否定案例证明的替代方法。所以Some扮演角色ReflectT并持有证明,None而不持有否定证明。
Definition is_1 n: bool:=
match n with
1 => true |
_ => false
end.
Lemma is_1_complete : forall n, is_1 n = true -> n = 1.
intros.
destruct n. simpl in H. discriminate.
destruct n. reflexivity.
simpl in H. discriminate.
Qed.
Lemma a_nat_is_1_or_not: forall n, option (n = 1).
intros.
cut (is_1 n = true -> n = 1). …Run Code Online (Sandbox Code Playgroud) pattern-matching ×10
enums ×2
haskell ×2
rust ×2
algorithm ×1
arrays ×1
clisp ×1
common-lisp ×1
coq ×1
hash ×1
lua ×1
r ×1
recursion ×1
reflect ×1
repl.it ×1
ruby ×1
swift ×1
unification ×1
where-clause ×1