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>-- binaryStringshowBin :: Int -> String
on binaryString(n)
on showBin(n)
showIntAtBase(n, 2)
script binaryChar
end binaryString
on "0"|λ|(n)
settext m toitem (n mod+ 1) of base"01"
end if|λ|
end script
showIntAtBase(2, binaryChar, n, 2"")
end showBin
 
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- showIntAtBase :: Int -> Int -> String
 
on showIntAtBase(n, base)
-- showIntAtBase :: Int -> (Int -> Char) -> Int -> String -> String
if base > 1 then
on showIntAtBase(base, toChr, n, basers)
if n > 0 then
script showIt
set m to n mod base
on set|λ|(nd_, r to n - m)
ifset r{n, >d} 0to thennd_
set prefixr_ to showIntAtBasetoChr's |λ|(rd) div& base, base)r
if basen > 10 then
|λ|(quotRem(n, base), r_)
else
set prefix to ""r_
end if
end |λ|
end script
if m < 10 then
set baseCode to 48 -- "0"
if base 1 elsethen
"error: showIntAtBase set baseCodeapplied to 55 --unsupported base"A" - 10
else if n < 0 end ifthen
"error: showIntAtBase applied to negative number"
prefix & character id (baseCode + m)
else
"0"
end if
else
missingshowIt's value|λ|(quotRem(n, base), rs)
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
elseon |λ|(n)
intercalate(" -> ", {n as string, showBin(n)})
ifend n > 0 then|λ|
end script
return unlines(map(binaryStringresult, [{5, 50, 9000]}))
end run
 
Line 412 ⟶ 425:
end unlines</lang>
{{Out}}
<pre>5 -> 101
50 -> 110010
9000 -> 10001100101000</pre>
 
=={{header|Applesoft BASIC}}==
9,659

edits