Non-decimal radices/Convert: Difference between revisions
Content added Content deleted
m (→{{header|jq}}: typo) |
(→{{header|AppleScript}}: Updated primitives) |
||
Line 289: | Line 289: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
{{Trans|JavaScript}} |
{{Trans|JavaScript}} |
||
For more flexibility with digit variants (upper and lower case hex, digits in other languages/scripts etc) we can define '''toBase'''(intBase, n) in terms of a more general '''inBaseDigits'''(strDigits, n) which derives the base from the number of digits to be used: |
For more flexibility with digit variants (upper and lower case hex, digits in other languages/scripts etc) we can define '''toBase'''(intBase, n) in terms of a more general '''inBaseDigits'''(strDigits, n) which derives the base from the number of digits to be used: |
||
<lang AppleScript>-- toBase :: Int -> Int -> String |
<lang AppleScript>-- toBase :: Int -> Int -> String |
||
Line 300: | Line 298: | ||
end if |
end if |
||
end toBase |
end toBase |
||
-- inBaseDigits :: String -> Int -> [String] |
-- inBaseDigits :: String -> Int -> [String] |
||
Line 307: | Line 304: | ||
script nextDigit |
script nextDigit |
||
on |
on |λ|(residue) |
||
set {divided, remainder} to quotRem(residue, intBase) |
set {divided, remainder} to quotRem(residue, intBase) |
||
⚫ | |||
⚫ | |||
⚫ | |||
{nothing:true} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end script |
end script |
||
Line 317: | Line 318: | ||
end inBaseDigits |
end inBaseDigits |
||
⚫ | |||
⚫ | |||
-- inUpperHex :: Int -> String |
-- inUpperHex :: Int -> String |
||
Line 331: | Line 330: | ||
end inDevanagariDecimal |
end inDevanagariDecimal |
||
-- TEST ---------------------------------------------------------------------- |
|||
-- TEST |
|||
on run |
on run |
||
script |
script |
||
on |
on |λ|(x) |
||
{{binary:toBase(2, x), octal:toBase(8, x), hex:toBase(16, x)}, ¬ |
{{binary:toBase(2, x), octal:toBase(8, x), hex:toBase(16, x)}, ¬ |
||
{upperHex:inUpperHex(x), dgDecimal:inDevanagariDecimal(x)}} |
{upperHex:inUpperHex(x), dgDecimal:inDevanagariDecimal(x)}} |
||
end |
end |λ| |
||
end script |
end script |
||
Line 344: | Line 343: | ||
-- GENERIC FUNCTIONS |
-- GENERIC FUNCTIONS --------------------------------------------------------- |
||
-- unfoldr :: (b -> Maybe (a, b)) -> b -> [a] |
-- unfoldr :: (b -> Maybe (a, b)) -> b -> [a] |
||
on unfoldr(f, v) |
on unfoldr(f, v) |
||
⚫ | |||
set lst to {} |
set lst to {} |
||
set recM to |
set recM to {nothing:false, new:v} |
||
⚫ | |||
repeat while (valid of recM) is true |
|||
repeat while (not (nothing of recM)) |
|||
set recM to |
set recM to |λ|(new of recM) |
||
if not nothing of recM then set end of lst to just of recM |
|||
⚫ | |||
end repeat |
|||
⚫ | |||
⚫ | |||
end unfoldr |
end unfoldr |
||
Line 369: | Line 369: | ||
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 |
set end of lst to |λ|(item i of xs, i, xs) |
||
end repeat |
end repeat |
||
return lst |
return lst |
||
Line 382: | Line 382: | ||
else |
else |
||
script |
script |
||
property |
property |λ| : f |
||
end script |
end script |
||
end if |
end if |
||
end mReturn |
end mReturn</lang> |
||
-- until :: (a -> Bool) -> (a -> a) -> a -> a |
|||
on |until|(p, f, x) |
|||
set mp to mReturn(p) |
|||
set v to x |
|||
⚫ | |||
tell mReturn(f) |
|||
repeat until mp's lambda(v) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
return v |
|||
end |until|</lang> |
|||
{{Out}} |
{{Out}} |
||
<lang AppleScript>{{{binary:"11111111", octal:"377", hex:"ff"}, {upperHex:"FF", dgDecimal:"२५५"}}, |
<lang AppleScript>{{{binary:"11111111", octal:"377", hex:"ff"}, {upperHex:"FF", dgDecimal:"२५५"}}, |