let pair a b = fun select -> select a b;
let isPair(x) = x.GetType().BaseType.Name = "FSharpFunc`2"
exception OuterError of string
let first(p) =
if isPair(p) then p(fun a b -> a)
else raise (OuterError("Not a pair"))
let second(p) =
if isPair(p) then p(fun a b -> b)
else raise (OuterError("Not a pair"))
let p1 = fun f -> pair 2 (pair 4 5) f
second(p1);
I want get the "(pair 4 5)", but it has someting wrong:
error FS0030: Value restriction. The value 'it' has been inferred to have generic type val it: ((int -> int -> '_a) -> '_a)
Either make the arguments to 'it' explicit or, if you do not intend for it to be generic, add a type annotation.
let pair a b = fun select -> select a b;
let first(p) = p(fun a b -> a)
let second(p) = p(fun a b -> b)
let p1 = fun f -> pair 2 (pair 4 5) f
second(p1);
I have deleted the isPair
function, but it's not getting any better.
I can write pair in javascript like this:
var pair = (a, b) => select => select(a, b)
var first = p => p((a, b) => a)
var second = p => p((a, b) => b)
var p1 = pair(2, pair(4, 5))
second(p1)
And I can use second(p1)
's value pair(4, 5)
.
var p2 = second(p1)
first(p2)
second(p2)
But it's not work in F#, because of F# is statically typed, I guess.
isPair
function is nonsensical. – Fyodor Soikin