y, z when x = z -> printf "%A" (x, y) | _ -> printf "Match error" FSharp (1, 2, 3) match { case (x, y, z) if (x == z) => print((x, y)) case _ => print("Match error") } f (x, y, z) |x==z = print (x, y) |otherwise = print "Matching error" scala haskell
= 1; 1, = 0; ↑−1 ↑ − 1 , 𝑜 arrow :: Int -> Int -> Int -> Int arrow n 0 a = 1 arrow 1 b a = a*(arrow 1 (b-1) a) arrow n b a = arrow (n-1) (arrow n (b-1) a) a main = print (arrow 2 2 3) Haskell
‘a List = Nil | Node of ‘a * ‘a List type ‘a BinTree = Leaf of ‘a | Node of ‘a * ‘a BinTree * ‘a BinTree let printValue = function Leaf(x) -> printf "%A" x | Node (x, _, _) -> printf "%A" x FSharp
(Double, Double) => Double, a: Double, b: Double, i: Double): Double = { if (a > b) i else f(iter(f,a+1,b,i), a) } Scala let rec iter f a b i = if a>b then i else f (iter f (a+1) b i) a FSharp
n = iter (fun y i -> y*x) 1 n 1.0 let factorial n = iter (fun y i -> y*(float i)) 1 n 1.0 let taylor_e_step x n = pow x n / fact n let taylor_e x = iter (fun y n -> y+taylor x n) 0 15 0.0 FSharp
n = if n=0 then acc else factorial0 (float(n) * acc) (n - 1) let factorial = factorial0 1.0 FSharp ~60ms FSharp let rec power0 acc x n = match n with 0 -> acc |n -> power0 (acc*x) x (n-1) let power = power0 1.0 FSharp
then 1 else x*fact(x-1) fact = (λf.λx. if x=1 then 1 else x*f(x-1)) fact fact = F fact Y F = F (Y F) Y = (λh.(λx. h (x x)) (λx. h (x x))) Y F = (λh.(λx. h (x x)) (λx. h (x x))) F = = (λx. F (x x)) (λx. F (x x)) = = F ((λx. F (x x)) (λx. F (x x))) = F (Y F) FSharp
(Y<'T> -> 'T) (*let y mk = let f' (Rec f as g) = mk (f g) f' (Rec f') *) // Y Combinator let y mk = let f' (Rec f as g) = mk (fun y -> f g y) f' (Rec f') FSharp
= Dictionary<'a, 'b> () let yMem mk = let dic = createDic defaultof<'a> defaultof<'b> let f' (Rec f as g) = mk (fun y -> f g y) let f'' = f' (Rec f') fun x -> if dic.ContainsKey(x) then dic.[x] else let answer = f'' x dic.[x] <- answer answer FSharp http://fssnip.net/1q
k m = if n = 0 then k 1.0 else m (n - 1) (fun r1 -> let r = r1 * (float n) in k r) let powtrc v k m = let x, n = v if n = 0 then k 1.0 else m (x, n - 1) (fun r1 -> let r = r1 * x in k r) FSharp
= let dic = createDic defaultof<'TArg1> defaultof<'TResult> let rec f' a k = match dic.TryGetValue(a) with | true, r -> k r | _ -> f a (fun r -> dic.[a] <- r k r) f' (fun a -> f' a id) FSharp
-> B) -> A+ -> B+ (A -> B -> A) -> A -> B+ -> A let rec map f = function [] -> [] | h::t -> (f h)::(map f t) let rec fold f acc = function [] -> acc | h::t -> fold f (f acc h) t
concat $ ifSumSimple 2 4 where l t = do print $ length $ show $ take 100 t l $ drop 100 t haskell main = do l a print a where a = concat $ ifSumSimple 2 4 l t = do print $ length $ show $ take 100 t l $ drop 100 t haskell
concat $ ifSumSimple 2 4 where l t = do print $ length $ show $ take 100 t l $ drop 100 t haskell main = do l a print a where a = concat $ ifSumSimple 2 4 l t = do print $ length $ show $ take 100 t l $ drop 100 t haskell 131648 kB 6716 kB