Binary digits: Difference between revisions
→{{header|AppleScript}}: Updated primitives - a showIntAtBase allowing a custom ( Int -> Char) function
(→JS ES5: Applied JS Beautify to adjust the spacing of an existing ES5 example) |
(→{{header|AppleScript}}: Updated primitives - a showIntAtBase allowing a custom ( Int -> Char) function) |
||
Line 333:
=={{header|AppleScript}}==
{{Trans|JavaScript}}
ES6
<lang AppleScript>--
on showBin(n)
showIntAtBase(n, 2)▼
script binaryChar
end script
end showBin
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- showIntAtBase :: Int -> Int -> String▼
on showIntAtBase(n, base)▼
▲-- showIntAtBase :: Int -> (Int -> Char) -> Int -> String -> String
if base > 1 then▼
script showIt
▲ set m to n mod base
on
|λ|(quotRem(n, base), r_)
else
end if
end
end script
if base ≤ 1
"error: showIntAtBase
else if n < 0
"error: showIntAtBase applied to negative number"
▲ "0"
▲ end if
else
end if
end showIntAtBase
-- quotRem :: Integral a => a -> a -> (a, a)
on quotRem(m, n)
{m div n, m mod n}
end quotRem
-- TEST -----------------------------------------------------------------------
on run
script
intercalate(" -> ", {n as string, showBin(n)})
end script
return unlines(map(
end run
Line 412 ⟶ 425:
end unlines</lang>
{{Out}}
<pre>5 -> 101
50 -> 110010
9000 -> 10001100101000</pre>
=={{header|Applesoft BASIC}}==
|