Binary digits: Difference between revisions

Content added Content deleted
No edit summary
(→‎{{header|AppleScript}}: updated primitives)
Line 333: Line 333:
=={{header|AppleScript}}==
=={{header|AppleScript}}==
{{Trans|JavaScript}}
{{Trans|JavaScript}}


<lang AppleScript>-- binaryString :: Int -> String
<lang AppleScript>-- binaryString :: Int -> String
on binaryString(n)
on binaryString(n)
showIntAtBase(n, 2)
showIntAtBase(2, n)
end binaryString
end binaryString



-- showIntAtBase :: Int -> Int -> String
-- showIntAtBase :: Int -> Int -> String
on showIntAtBase(base, n)
on showIntAtBase(n, base)
if base > 1 then
if base > 1 then
if n > 0 then
if n > 0 then
Line 350: Line 345:
set r to n - m
set r to n - m
if r > 0 then
if r > 0 then
set prefix to showIntAtBase(base, r div base)
set prefix to showIntAtBase(r div base, base)
else
else
set prefix to ""
set prefix to ""
Line 370: Line 365:
end showIntAtBase
end showIntAtBase


-- TEST -----------------------------------------------------------------------

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




-- GENERIC FUNCTIONS FOR TEST -------------------------------------------------


-- intercalate :: Text -> [Text] -> Text

on intercalate(strText, lstText)
-- GENERIC FUNCTIONS FOR TESTING
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate


-- map :: (a -> b) -> [a] -> [b]
-- map :: (a -> b) -> [a] -> [b]
Line 390: Line 389:
set lst to {}
set lst to {}
repeat with i from 1 to lng
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
set end of lst to |λ|(item i of xs, i, xs)
end repeat
end repeat
return lst
return lst
end tell
end tell
end map
end map

-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate


-- Lift 2nd class handler function into 1st class script wrapper
-- Lift 2nd class handler function into 1st class script wrapper
Line 411: Line 402:
else
else
script
script
property lambda : f
property |λ| : f
end script
end script
end if
end if
end mReturn</lang>
end mReturn



-- unlines :: [String] -> String
on unlines(xs)
intercalate(linefeed, xs)
end unlines</lang>
{{Out}}
{{Out}}
<pre>101
<pre>101