Evaluate binomial coefficients: Difference between revisions
Content added Content deleted
PatGarrett (talk | contribs) (→{{header|Batch File}}: Rename the section) |
(→{{header|AppleScript}}: Added a functionally composed variant) |
||
Line 220: | Line 220: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
===Imperative=== |
|||
<lang AppleScript>set n to 5 |
<lang AppleScript>set n to 5 |
||
set k to 3 |
set k to 3 |
||
Line 238: | Line 239: | ||
return n_factorial / (n_minus_k_factorial) * 1 / (k_factorial) as integer |
return n_factorial / (n_minus_k_factorial) * 1 / (k_factorial) as integer |
||
</lang> |
</lang> |
||
===Functional=== |
|||
Using a little more abstraction, for readability and ease of refactoring: |
|||
<lang applescript>-- factorial :: Int -> Int |
|||
on factorial(n) |
|||
product(enumFromTo(1, n)) |
|||
end factorial |
|||
-- binomialCoefficient :: Int -> Int -> Int |
|||
on binomialCoefficient(n, k) |
|||
factorial(n) div (factorial(n - k) * (factorial(k))) |
|||
end binomialCoefficient |
|||
-- TEST ----------------------------------------------------- |
|||
on run |
|||
binomialCoefficient(5, 3) |
|||
--> 10 |
|||
end run |
|||
-- GENERAL ------------------------------------------------- |
|||
-- enumFromTo :: Int -> Int -> [Int] |
|||
on enumFromTo(m, n) |
|||
if m ≤ n then |
|||
set lst to {} |
|||
repeat with i from m to n |
|||
set end of lst to i |
|||
end repeat |
|||
return lst |
|||
else |
|||
return {} |
|||
end if |
|||
end enumFromTo |
|||
-- foldl :: (a -> b -> a) -> a -> [b] -> a |
|||
on foldl(f, startValue, xs) |
|||
tell mReturn(f) |
|||
set v to startValue |
|||
set lng to length of xs |
|||
repeat with i from 1 to lng |
|||
set v to |λ|(v, item i of xs, i, xs) |
|||
end repeat |
|||
return v |
|||
end tell |
|||
end foldl |
|||
-- Lift 2nd class handler function into 1st class script wrapper |
|||
-- mReturn :: First-class m => (a -> b) -> m (a -> b) |
|||
on mReturn(f) |
|||
if script is class of f then |
|||
f |
|||
else |
|||
script |
|||
property |λ| : f |
|||
end script |
|||
end if |
|||
end mReturn |
|||
-- product :: [Num] -> Num |
|||
on product(xs) |
|||
script multiply |
|||
on |λ|(a, b) |
|||
a * b |
|||
end |λ| |
|||
end script |
|||
foldl(multiply, 1, xs) |
|||
end product</lang> |
|||
{{Out}} |
|||
<pre>10</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<lang autohotkey>MsgBox, % Round(BinomialCoefficient(5, 3)) |
<lang autohotkey>MsgBox, % Round(BinomialCoefficient(5, 3)) |