Magic squares of doubly even order: Difference between revisions
Content deleted Content added
Added Julia language |
→{{header|AppleScript}}: Updated primitives |
||
Line 116:
=={{header|AppleScript}}==
{{Trans|JavaScript}}
<lang AppleScript>--
-- magicSquare :: Int -> [[Int]]
on magicSquare(n)
if n mod 4 > 0 then
Line 134 ⟶ 136:
script scale
on
replicate(n / 4, x)
end
end script
set truthSeries to ¬
flatten(scale's
end if
set limit to sqr + 1
script inOrderOrReversed
on
cond(x, i, limit - i)
end
end script
Line 155 ⟶ 157:
end if
end magicSquare
-- magicSeries :: Int -> [Bool]
on magicSeries(n)
script boolToggle
on
not x
end
end script
Line 174 ⟶ 175:
-- TEST
on run
Line 193 ⟶ 194:
on wikiTable(lstRows, blnHdr, strStyle)
script fWikiRows
on
set strDelim to cond(blnHdr and (iRow = 0), "!", "|")
set strDbl to strDelim & strDelim
linefeed & "|-" & linefeed & strDelim & space & ¬
intercalate(space & strDbl & space, lstRow)
end
end script
Line 208 ⟶ 209:
-- GENERIC FUNCTIONS
-- asPowerOfTwo :: Int -> maybe Int
Line 248 ⟶ 221:
end asPowerOfTwo
--
on
end script
foldl(append, {}, map(f, xs))
-- cond :: Bool -> a -> a -> a
Line 277 ⟶ 249:
end if
end flatten
-- foldl :: (a -> b -> a) -> a -> [b] -> a
Line 295 ⟶ 256:
set lng to length of xs
repeat with i from 1 to lng
set v to
end repeat
return v
Line 301 ⟶ 262:
end foldl
--
on
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set
set my text item delimiters to dlm
return strJoined
end intercalate
-- isPowerOf :: Int -> Int -> Bool
on isPowerOf(k, n)
set v to k
script remLeft
on |λ|(x)
x mod v is not 0
end |λ|
end script
script integerDiv
on |λ|(x)
x div v
end |λ|
end script
|until|(remLeft, integerDiv, n) = 1
end isPowerOf
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
Line 321 ⟶ 308:
else
script
property
end script
end if
Line 361 ⟶ 348:
end splitAt
--
on
if length of xs ≤ n then
else
set {gp, t} to splitAt(n, xs)
{gp} & splitEvery(n, t)
end if
end splitEvery
-- 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 |λ|(v)
set v to |λ|(v)
end repeat
end tell
return v
end |until|</lang>
{{Out}}
magic(8)
|