Binary digits: Difference between revisions

Content added Content deleted
(→‎{{header|AppleScript}}: Normalised argument sequence of showIntAtBase, updated primitives)
Line 220: Line 220:




<lang AppleScript>on run
<lang AppleScript>-- binaryString :: Int -> String
intercalate(linefeed, ¬
map(binaryString, [5, 50, 9000]))
end run

-- binaryString :: Int -> String
on binaryString(n)
on binaryString(n)
showIntAtBase(n, 2)
showIntAtBase(2, n)
end binaryString
end binaryString
Line 236: Line 229:


-- showIntAtBase :: Int -> Int -> String
-- showIntAtBase :: Int -> Int -> String
on showIntAtBase(n, base)
on showIntAtBase(base, n)
if base > 1 then
if base > 1 then
if n > 0 then
if n > 0 then
Line 242: Line 235:
set r to n - m
set r to n - m
if r > 0 then
if r > 0 then
set prefix to showIntAtBase(r div base, base)
set prefix to showIntAtBase(base, r div base)
else
else
set prefix to ""
set prefix to ""
Line 261: Line 254:
end if
end if
end showIntAtBase
end showIntAtBase


-- TEST
on run
intercalate(linefeed, ¬
map(binaryString, [5, 50, 9000]))
end run





Line 268: Line 271:
-- map :: (a -> b) -> [a] -> [b]
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
on map(f, xs)
script mf
tell mReturn(f)
property lambda : f
set lng to length of xs
end script
set lst to {}
repeat with i from 1 to lng
set lng to length of xs
set end of lst to lambda(item i of xs, i, xs)
set lst to {}
end repeat
repeat with i from 1 to lng
return lst
end tell
set end of lst to mf's lambda(item i of xs, i, xs)
end repeat
return lst
end map
end map


Line 288: Line 289:
end intercalate
end intercalate


-- Lift 2nd class handler function into 1st class script wrapper
</lang>
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property lambda : f
end script
end if
end mReturn</lang>