Non-decimal radices/Convert: Difference between revisions

→‎{{header|AppleScript}}: Updated primitives
(→‎{{header|AppleScript}}: Updated primitives)
Line 289:
=={{header|AppleScript}}==
{{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:
<lang AppleScript>-- toBase :: Int -> Int -> String
Line 300 ⟶ 298:
end if
end toBase
 
 
-- inBaseDigits :: String -> Int -> [String]
Line 307 ⟶ 304:
script nextDigit
on lambda|λ|(residue)
set {divided, remainder} to quotRem(residue, intBase)
setif vdivided to> lambda(v)0 then
{valid:divided > 0, value {just:(item (remainder + 1) of strDigits), new:divided, nothing:false}
end lambda else
{nothing:true}
end repeatif
end tell|λ|
{valid:divided > 0, value:(item (remainder + 1) of strDigits), new:divided}
end lambda
end script
Line 317 ⟶ 318:
end inBaseDigits
 
-- OTHER FUNCTIONS DERIVABLE FROM inBaseDigits -------------------------------
 
 
-- OTHER FUNCTIONS DERIVABLE FROM inBaseDigits
 
-- inUpperHex :: Int -> String
Line 331 ⟶ 330:
end inDevanagariDecimal
 
-- TEST ----------------------------------------------------------------------
-- TEST
on run
script
on lambda|λ|(x)
{{binary:toBase(2, x), octal:toBase(8, x), hex:toBase(16, x)}, ¬
{upperHex:inUpperHex(x), dgDecimal:inDevanagariDecimal(x)}}
end lambda|λ|
end script
Line 344 ⟶ 343:
 
 
-- GENERIC FUNCTIONS ---------------------------------------------------------
 
-- unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
on unfoldr(f, v)
set mf to mReturn(f)
set lst to {}
set recM to mf's{nothing:false, lambda(new:v)}
set mf totell mReturn(f)
repeat while (valid of recM) is true
setrepeat endwhile of lst to(not value(nothing of recM))
set recM to mf's lambda|λ|(new of recM)
if not nothing of recM then set end of lst to just of recM
end repeat
lst & value of recMend repeat
end repeattell
lst
end unfoldr
 
Line 369:
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda|λ|(item i of xs, i, xs)
end repeat
return lst
Line 382:
else
script
property lambda|λ| : f
end script
end if
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}}
<lang AppleScript>{{{binary:"11111111", octal:"377", hex:"ff"}, {upperHex:"FF", dgDecimal:"२५५"}},
9,659

edits