Non-decimal radices/Convert: Difference between revisions

Content added Content deleted
(→‎{{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 lambda(residue)
on |λ|(residue)
set {divided, remainder} to quotRem(residue, intBase)
set {divided, remainder} to quotRem(residue, intBase)
if divided > 0 then
{just:(item (remainder + 1) of strDigits), new:divided, nothing:false}
else
{nothing:true}
end if
end |λ|
{valid:divided > 0, value:(item (remainder + 1) of strDigits), new:divided}
end lambda
end script
end script
Line 317: Line 318:
end inBaseDigits
end inBaseDigits


-- OTHER FUNCTIONS DERIVABLE FROM inBaseDigits -------------------------------


-- OTHER FUNCTIONS DERIVABLE FROM inBaseDigits


-- inUpperHex :: Int -> String
-- inUpperHex :: Int -> String
Line 331: Line 330:
end inDevanagariDecimal
end inDevanagariDecimal


-- TEST ----------------------------------------------------------------------
-- TEST
on run
on run
script
script
on lambda(x)
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 lambda
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 mf to mReturn(f)
set lst to {}
set lst to {}
set recM to mf's lambda(v)
set recM to {nothing:false, new:v}
tell mReturn(f)
repeat while (valid of recM) is true
set end of lst to value of recM
repeat while (not (nothing of recM))
set recM to mf's lambda(new of recM)
set recM to |λ|(new of recM)
if not nothing of recM then set end of lst to just of recM
end repeat
lst & value of recM
end repeat
end tell
lst
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 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
Line 382: Line 382:
else
else
script
script
property lambda : f
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)
set v to lambda(v)
end repeat
end tell
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:"२५५"}},