我尝试使用模式匹配在F#中实现斐波那契序列,如下所示:
let fibonacci m=
let rec fib = function
| (0, _, z) -> z
| (n, y, z) -> fib (n-1) z (y+z)
fib m 0 1
Run Code Online (Sandbox Code Playgroud)
在这里,我期望fib的第一个参数跟踪我们在序列中的距离,以及接下来的两个参数是序列中的连续项.
但是,我得到一个编译时错误fib (n-1) z (y+z):
Type mismatch. Expecting a
'a -> 'b -> 'c
but given a
'c
The resulting type would be infinite when unifying ''a' and ''b -> 'c -> 'a'
Run Code Online (Sandbox Code Playgroud)
我尝试指定类似的类型:
let fibonacci m=
let rec fib = function
| (0, _, z:int) -> z
| (n:int, …Run Code Online (Sandbox Code Playgroud) 对于关于元组对称相等的这个函数,
symEq :: Eq a => (a,a) -> (a,a) -> Bool
symEq (x,y) (u,v) = (x,y) == (u,v) || (x,y) == (v,u)
Run Code Online (Sandbox Code Playgroud)
我想用模式匹配重写它,如下所示,
symEq' :: Eq a => (a,a) -> (a,a) -> Bool
symEq' (x,y) (x,y) = True
symEq' (x,y) (y,x) = True
symEq' _ _ = False
Run Code Online (Sandbox Code Playgroud)
后者因为x和的冲突定义错误而失败y.如何重写symEq并利用模式匹配?
我有一堆条件返回列表,有没有一种方法来评估它们而不给每个条件一个名字?
简化示例:
conditions i j = c1 i j ++ c2 i j ++ c3 i j
where
c1 i j | i > 2 = ["x"]
| otherwise = []
c2 i j | j > 5 = ["y", "a"]
| otherwise = []
c3 i j | j > 10 = ["z"]
| otherwise = []
c4 i j | j > 10 && i > 1 = ["z", "c", "hello"]
| otherwise = []
Run Code Online (Sandbox Code Playgroud) haskell functional-programming if-statement pattern-matching
我正在阅读Real World Haskell,并试图理解as-pattern.
从这本书(第4章):
suffixes :: [a] -> [[a]]
suffixes xs@(_:xs') = xs : suffixes xs'
suffixes _ = []
Run Code Online (Sandbox Code Playgroud)
这本书解释了@符号,
"...将变量xs绑定到与@符号右侧匹配的值."
我无法理解这个解释.假设我打电话
suffixes "hello"
Run Code Online (Sandbox Code Playgroud)
明确地说,上面那行与@做了什么(在第一次迭代中)?我知道函数的结果是什么,但无法看到我们如何从上面的代码到达那里.
有没有办法在haxe中保存模式?我有几个开关函数,其中一些具有相同的模式,为了使代码更清晰,我想将它们保存到一个公共数组或其他东西.所以我有类似的东西
switch (field) {
case 'x' | 'y' | 'color' : doThis();
}
//...other function...
switch (field) {
case 'x' | 'y' | 'color' : doThat();
}
Run Code Online (Sandbox Code Playgroud)
我希望有类似的东西
myPattern = 'x' | 'y' | 'color';
switch (field) {
case myPattern : doThis();
}
//...other function...
switch (field) {
case myPattern : doThat();
}
Run Code Online (Sandbox Code Playgroud) 我有一种class Operator[T: TypeTag : ClassTag]方法,我想在参数化类型上使用Scala的模式匹配T。我知道模式匹配的工作原理,因此在Scala的repl中尝试了以下操作,并得到了一个错误。
scala> def matchTest() = T match {
| case x: String => "abcd"
| case _ => null
| }
<console>:11: error: not found: value T
def matchTest() = T match {
^
Run Code Online (Sandbox Code Playgroud) 我有这个
fn main() {
let args = os::args();
let first_args = args[1].to_string();
match first_args {
"test" => println!("Good!"),
_ => println!("No test ?!"),
}
}
Run Code Online (Sandbox Code Playgroud)
但在编译期间,我收到此错误:
error: mismatched types: expected `collections::string::String`, found `&'static str` (expected struct collections::string::String, found &-ptr)
src/command_line_arguments.rs:7 "test" => println!("Good!"),
^~~~~~
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我更好地理解这个吗?这会是一个更好的方法吗?
为什么下面的代码给我一个不完整的模式匹配警告和运行时异常.
type Left =
| L1 of int
| L2 of int
type Right =
| R1 of int
| R2 of int
type Directions =
| LeftDir of Left
| RightDir of Right
let unpack (LeftDir (L1 i)) = i
let x = unpack (LeftDir (L1 10))
let y = unpack (LeftDir (L2 20))
Run Code Online (Sandbox Code Playgroud)
显然答案是因为IT是一个不完整的模式匹配;-)
但是为什么F#不够聪明(或者F#语言规范的编写者)知道unpack只期待一个LeftDir containing a L1?为什么需要完整的模式匹配?
好的,好的 - 我知道这一般是个坏主意.然而,在一个非常具体的背景下 - 在我的测试用例中,我只是期望某个结果,而其他任何东西都会以任何方式出错.并且进行模式匹配只是模糊了我的测试代码.
这是一个例子
type Result =
| Success of int * int
| Error of String
let someFunc x : Result = // implementation not important here
// and then later in my test code
[<Test>]
member me.``Some Cool Test Method``() =
let (Success x, y) = someFunc "Foo"
equals x 1
equals y 2
Run Code Online (Sandbox Code Playgroud)
关于如何使这更愉快和编译友好的任何想法?
我有一个只有一列的数据框,如下所示:
>df
Sample_Name
1 GW16F1_A-1
2 GW16F1_A-10
3 GW16F1_A-12
4 GW16F2_A-2
5 GW16F2_A-3
6 GW16F2_A-5
7 GW16V1_A-6
8 GW16V1_A-7
9 GW16V2_A-8
10 GW16V2_A-9
Run Code Online (Sandbox Code Playgroud)
我想基于Sample_Name列的内容将第二列附加到此数据框,因此输出将如下所示:
>df
SampleName SampleGroup
1 GW16F1_A-1 F1
2 GW16F1_A-10 F1
3 GW16F1_A-12 F1
4 GW16F2_A-2 F2
5 GW16F2_A-3 F2
6 GW16F2_A-5 F2
7 GW16V1_A-6 V1
8 GW16V1_A-7 V1
9 GW16V2_A-8 V2
10 GW16V2_A-9 V2
Run Code Online (Sandbox Code Playgroud)
是否有一个函数可以读取列的内容并根据它输出新的向量?