Thue-Morse: Difference between revisions

Line 72:
{{Trans|JavaScript}}
 
<lang AppleScript>-- thueMorseTHUE :: IntMORSE -> String---------------------------------------------------------------
 
-- thueMorse :: Int -> String
on thueMorse(nCycles)
script concatBinaryInverse
on lambda|λ|(xs)
script binaryInverse
on lambda|λ|(x)
1 - x
end lambda|λ|
end script
xs & map(binaryInverse, xs)
end lambda|λ|
end script
intercalate("", ¬
foldl(concatBinaryInverse, [0], rangeenumFromTo(1, nCycles)))
end thueMorse
 
 
-- TEST ----------------------------------------------------------------------
-- TEST
on run
Line 102 ⟶ 104:
-- GENERIC LIBRARY FUNCTIONS
 
-- mapenumFromTo :: (aInt -> b)Int -> [a] -> [bInt]
on mapenumFromTo(fm, xsn)
tellif mReturn(f)m > n then
set lngd to length of xs-1
else
set lst to {}
repeatset withd i fromto 1 to lng
end if
set end of lst to lambda(item i of xs, i, xs)
set lst to end repeat{}
repeat with i from returnm lstto n by d
set end tellof lst to i
end maprepeat
return lst
end enumFromTo
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
Line 120 ⟶ 124:
set lng to length of xs
repeat with i from 1 to lng
set v to lambda|λ|(v, item i of xs, i, xs)
end repeat
return v
Line 134 ⟶ 138:
end intercalate
 
-- rangemap :: Int(a -> Intb) -> [Inta] -> [b]
on rangemap(mf, nxs)
iftell n < m thenmReturn(f)
set dlng to -1length of xs
set lst to {}
else
setrepeat dwith toi from 1 to lng
set end of lst to lambda|λ|(item i of xs, i, xs)
end if
set lst to {} end repeat
repeat with i from mreturn to n by dlst
set end of lst to itell
end repeatmap
return lst
end range
 
-- Lift 2nd class handler function into 1st class script wrapper
Line 155 ⟶ 157:
else
script
property lambda|λ| : f
end script
end if
end mReturn</lang>
 
<pre>"0110100110010110100101100110100110010110011010010110100110010110"</pre>
 
9,659

edits