Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell F#歧视联合中的函数_Haskell_F# - Fatal编程技术网

Haskell F#歧视联合中的函数

Haskell F#歧视联合中的函数,haskell,f#,Haskell,F#,有没有办法在受歧视的工会中使用函数?我希望做这样的事情: 型式试验箱 我知道这在Haskell中是可能的,我想知道F#中的等价物是什么 谢谢。型式试验箱) type Test<'A> = Test of ('A -> bool) 作为对desco答案的扩展,您可以应用模式匹配测试中的功能: type Test<'a> = Test of ('a -> bool) // let applyTest T x = match T with Test(f) -

有没有办法在受歧视的工会中使用函数?我希望做这样的事情:


型式试验箱

我知道这在Haskell中是可能的,我想知道F#中的等价物是什么

谢谢。

型式试验箱)
type Test<'A> = Test of ('A -> bool)

作为对desco答案的扩展,您可以应用模式匹配测试中的功能:

type Test<'a> = Test of ('a -> bool)

// let applyTest T x = match T with Test(f) -> f x
// better: (as per kvb's comment) pattern match the function argument
let applyTest (Test f) x = f x

快速提问。如何从这样的绑定中实际应用函数?使用类似于
let(Test f)=Test(fun\uu->true)的方法将函数重新剥离出来
将其绑定到
f
。请注意,由于受歧视的联合中只有一种情况,您可以在
applyTest
的左侧安全地进行模式匹配,这样您就不会有无意义的标识符
t
把事情弄得乱七八糟:
让applyTest(Test f)x=fx
@kvb:nice!我忘了函数参数也是模式。我更新了我的帖子。令人困惑的是F#需要在
'a->bool
函数类型周围加上多余的括号。
// A Test<string>
let upperCaseTest = Test (fun (s:string) -> s.ToUpper() = s)

// A Test<int>
let primeTest =
    Test (fun n ->
        let upper = int (sqrt (float n))
        n > 1 && (n = 2 || [2..upper] |> List.forall (fun d -> n%d <> 0)) 
    )
> applyTest upperCaseTest "PIGSMIGHTFLY";;
val it : bool = true
> applyTest upperCaseTest "PIGSMIgHTFLY";;
val it : bool = false
> [1..30] |> List.filter (applyTest primeTest);;
val it : int list = [2; 3; 5; 7; 11; 13; 17; 19; 23; 29]