以下代码旨在生成Double或Integer.s被假定为negate或id; n整个部分; 和f小数部分或Nothing整数.
computeValue :: Num a => (a->a) -> Integer -> (Maybe Double) -> Either Double Integer
computeValue s n Nothing = Right $ s n
computeValue s n (Just a) = Left $ s (fromIntegral n + a)
Run Code Online (Sandbox Code Playgroud)
当我编译这个时,我得到:
test1.hs:2:28:
Couldn't match type `Integer' with `Double'
Expected type: Either Double Integer
Actual type: Either Double a
In the expression: Right $ s n
In an equation for …Run Code Online (Sandbox Code Playgroud) 鉴于以下计划:
{-# LANGUAGE DataKinds, GADTs #-}
{-# LANGUAGE TypeFamilies #-}
data Foo = A | B
type family IsA (foo :: Foo) :: Bool
type instance IsA A = True
type instance IsA B = False
data Bar (foo :: Foo) where
BarA :: (IsA foo ~ True) => Int -> Bar foo
BarB :: (IsA foo ~ False) => String -> Bar foo
f :: Bar A -> Int
f bar = case bar of
BarA x -> x …Run Code Online (Sandbox Code Playgroud) 如果还有另一种方法可以实现我在下面尝试做的事情,请告诉我.假设我有以下示例代码
type FooBar =
| Foo
| Bar
let foobars = [Bar;Foo;Bar]
let isFoo item =
match item with
| Foo _ -> true
| _ -> false
foobars |> Seq.filter isFoo
Run Code Online (Sandbox Code Playgroud)
我想编写isFoo的通用/高阶版本,它允许我根据所有其他类型的区分联合(在这种情况下为Bar)过滤我的列表.
像下面的东西,'可以是Foo或Bar
let is<'a> item =
match item with
| a _ -> true
| _ -> false
Run Code Online (Sandbox Code Playgroud)
但是,此尝试会产生以下错误:
错误FS0039:未定义模式鉴别器"a"
假设我有一个函数可以进行一些计算,有几种模式; 以模式匹配的形式实现.
大多数这些模式(以及其他不同的东西)对参数进行处理,我在let表达式中使用中间变量.但我发现let在许多模式上使用相同的方法确实是多余的,我想知道是否有一种方法可以let为多种模式定义?
这是我复制的一个例子let:
data MyType a = Something a | Another Int [a]
myFunc (Something x) = -- return something, this isn't the point here
myFunc (Another 0 xs) =
let intermediary = some $ treatment xs
in doSthg intermediary 1
myFunc (Another 1 (x:xs)) =
let intermediary = some $ treatment xs
in doSthg1 intermediary 1 x
myFunc (Another 2 (x:x':xs)) =
let intermediary = some $ treatment xs
in …Run Code Online (Sandbox Code Playgroud) 为什么链式模式匹配结构不可能?例如,以下是合法的,如果是荒谬的,
val a = ADT(5)
val b = a match {
case ADT(a) if a > 4 => ADT(a * 3)
case ADT(a) => ADT(a + 1)
}
b match {
case ADT(a) if a > 13 => doSomething(a)
case _ => {}
}
Run Code Online (Sandbox Code Playgroud)
但以下不是:
a match {
case ADT(a) if a > 4 => ADT(a * 3)
case ADT(a) => ADT(a + 1)
} match {
case ADT(a) if a > 13 => doSomething(a)
case _ => {}
} …Run Code Online (Sandbox Code Playgroud) 让我们假设我有一个简单的第三方(即我不能修改它)类定义如下:
class Price(var value: Int)
Run Code Online (Sandbox Code Playgroud)
是否可以将此类的实例与某些模式匹配?
例如,我想实现功能:
def printPrice(price: Price) = {
// implementation here
}
Run Code Online (Sandbox Code Playgroud)
...打印price is {some value}每个price有value <= 9000和price is over 9000所有其他情况.
例如,调用:
printPrice(new Price(10))
printPrice(new Price(9001))
Run Code Online (Sandbox Code Playgroud)
应该打印:
price is 10
price is over 9000
Run Code Online (Sandbox Code Playgroud)
如何printPrice使用模式匹配实现?
我想在和的类型的pattern match不同部分的和:listscalaheadtail
class Solution07 extends FlatSpec with ShouldMatchers {
"plain recursive flatten" should "flatten a list" in {
val list1 = List(List(1, 1), 2, List(3, List(5, 8)))
val list1Flattened = List(1, 1, 2, 3, 5, 8)
flattenRecur(list1) should be (list1Flattened)
}
def flattenRecur(ls: List[Any]): List[Int] = ls match {
case (head: Int) :: (tail: List[Any]) => head :: flattenRecur(tail)
case (head: List[Int]) :: (tail: List[Any]) => head.head :: flattenRecur(head.tail :: tail)
case (head: List[Any]) …Run Code Online (Sandbox Code Playgroud) 当我注意到/*和/**模式之间存在差异时,我试图为过滤器注册某些URL .
@Bean
public FilterRegistrationBean tokenAuthenticationFilterBean() {
FilterRegistrationBean registration = new FilterRegistrationBean(tokenAuthenticationFilter);
registration.addUrlPatterns("/api/**","/ping","/api/*");
return registration;
}
Run Code Online (Sandbox Code Playgroud)
这些模式有什么区别?
当对引用与不包含引用的模式进行模式匹配时会发生什么?
这是使用结构模式的示例:
fn main() {
struct S(u32);
let S(x) = &S(2);
// type of x is `&u32`
}
Run Code Online (Sandbox Code Playgroud)
这种行为令我感到惊讶,因为左侧的模式似乎与右侧的数据不匹配,这与s 排列的let &S(x) = &S(2)位置不同。&
看起来发生的情况是,当 RHS 是结构引用并且 lhs 是具有字段模式的结构模式时,字段模式中变量的类型就是&FwhereF是字段的类型。
我正在寻找的是:
let (x,) = &(2,);类型x为i32(更正:) &i32。我在《Rust Reference》或《Rust Book》中找不到任何有关此内容的信息,但我可能错过了。
我发现了这个 JS 代码片段:
function append(demo = "", file = "") {
const extra = "ctl=1&embed=1";
if (demo && file) return `/${demo}/?file=${file}&${extra}`;
if (!demo && file) return `/?file=${file}&${extra}`;
if (demo && !file) return `/${demo}&${extra}`;
return `?${extra}`;
}
Run Code Online (Sandbox Code Playgroud)
我可以使用ifs 轻松地将其移植到 Raku,但我认为使用given/when将是展示该功能的好方法。我想出了以下几点:
sub append(Str :$demo, Str :$file) {
my $extra = "ctl=1&embed=1";
given ($demo, $file) {
when $demo.so, $file.so { "/$demo/?file=$file&$extra" }
when $demo.not, $file.so { "/?file=$file&$extra" }
when $demo.so, $file.not { "/$demo?$extra" }
default { "?$extra" …Run Code Online (Sandbox Code Playgroud) pattern-matching ×10
haskell ×3
scala ×3
f# ×1
ghc ×1
let ×1
polymorphism ×1
raku ×1
rust ×1
spring-boot ×1
warnings ×1