Magic squares of doubly even order: Difference between revisions

Content added Content deleted
(Added Julia language)
(→‎{{header|AppleScript}}: Updated primitives)
Line 116: Line 116:
=={{header|AppleScript}}==
=={{header|AppleScript}}==
{{Trans|JavaScript}}
{{Trans|JavaScript}}
<lang AppleScript>-- magicSquare :: Int -> [[Int]]
<lang AppleScript>-- MAGIC SQUARE OF DOUBLY EVEN ORDER -----------------------------------------

-- magicSquare :: Int -> [[Int]]
on magicSquare(n)
on magicSquare(n)
if n mod 4 > 0 then
if n mod 4 > 0 then
Line 134: Line 136:
script scale
script scale
on lambda(x)
on |λ|(x)
replicate(n / 4, x)
replicate(n / 4, x)
end lambda
end |λ|
end script
end script
set truthSeries to ¬
set truthSeries to ¬
flatten(scale's lambda(map(scale, splitEvery(4, magicSeries(4)))))
flatten(scale's |λ|(map(scale, splitEvery(4, magicSeries(4)))))
end if
end if
set limit to sqr + 1
set limit to sqr + 1
script inOrderOrReversed
script inOrderOrReversed
on lambda(x, i)
on |λ|(x, i)
cond(x, i, limit - i)
cond(x, i, limit - i)
end lambda
end |λ|
end script
end script
Line 155: Line 157:
end if
end if
end magicSquare
end magicSquare



-- magicSeries :: Int -> [Bool]
-- magicSeries :: Int -> [Bool]
on magicSeries(n)
on magicSeries(n)
script boolToggle
script boolToggle
on lambda(x)
on |λ|(x)
not x
not x
end lambda
end |λ|
end script
end script
Line 174: Line 175:




-- TEST ---------------------------------------------------------------------------
-- TEST ----------------------------------------------------------------------
on run
on run
Line 193: Line 194:
on wikiTable(lstRows, blnHdr, strStyle)
on wikiTable(lstRows, blnHdr, strStyle)
script fWikiRows
script fWikiRows
on lambda(lstRow, iRow)
on |λ|(lstRow, iRow)
set strDelim to cond(blnHdr and (iRow = 0), "!", "|")
set strDelim to cond(blnHdr and (iRow = 0), "!", "|")
set strDbl to strDelim & strDelim
set strDbl to strDelim & strDelim
linefeed & "|-" & linefeed & strDelim & space & ¬
linefeed & "|-" & linefeed & strDelim & space & ¬
intercalate(space & strDbl & space, lstRow)
intercalate(space & strDbl & space, lstRow)
end lambda
end |λ|
end script
end script
Line 208: Line 209:




-- GENERIC FUNCTIONS ---------------------------------------------------------------------------
-- GENERIC FUNCTIONS ---------------------------------------------------------

-- splitEvery :: Int -> [a] -> [[a]]
on splitEvery(n, xs)
if length of xs ≤ n then
{xs}
else
set {gp, t} to splitAt(n, xs)
{gp} & splitEvery(n, t)
end if
end splitEvery

-- isPowerOf :: Int -> Int -> Bool
on isPowerOf(k, n)
set v to k
script remLeft
on lambda(x)
x mod v is not 0
end lambda
end script
script integerDiv
on lambda(x)
x div v
end lambda
end script
|until|(remLeft, integerDiv, n) = 1
end isPowerOf


-- asPowerOfTwo :: Int -> maybe Int
-- asPowerOfTwo :: Int -> maybe Int
Line 248: Line 221:
end asPowerOfTwo
end asPowerOfTwo


-- map :: (a -> b) -> [a] -> [b]
-- concatMap :: (a -> [b]) -> [a] -> [b]
on map(f, xs)
on concatMap(f, xs)
tell mReturn(f)
script append
set lng to length of xs
on |λ|(a, b)
set lst to {}
a & b
repeat with i from 1 to lng
end |λ|
end script
set end of lst to lambda(item i of xs, i, xs)
end repeat
foldl(append, {}, map(f, xs))
return lst
end tell
end concatMap
end map


-- cond :: Bool -> a -> a -> a
-- cond :: Bool -> a -> a -> a
Line 277: Line 249:
end if
end if
end flatten
end flatten

-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
script append
on lambda(a, b)
a & b
end lambda
end script
foldl(append, {}, map(f, xs))
end concatMap


-- foldl :: (a -> b -> a) -> a -> [b] -> a
-- foldl :: (a -> b -> a) -> a -> [b] -> a
Line 295: Line 256:
set lng to length of xs
set lng to length of xs
repeat with i from 1 to lng
repeat with i from 1 to lng
set v to lambda(v, item i of xs, i, xs)
set v to |λ|(v, item i of xs, i, xs)
end repeat
end repeat
return v
return v
Line 301: Line 262:
end foldl
end foldl


-- until :: (a -> Bool) -> (a -> a) -> a -> a
-- intercalate :: Text -> [Text] -> Text
on |until|(p, f, x)
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set mp to mReturn(p)
set v to x
set strJoined to lstText as text
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)
tell mReturn(f)
repeat until mp's lambda(v)
set lng to length of xs
set v to lambda(v)
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
end repeat
return lst
end tell
end tell
end map
return v

end |until|


-- Lift 2nd class handler function into 1st class script wrapper
-- Lift 2nd class handler function into 1st class script wrapper
Line 321: Line 308:
else
else
script
script
property lambda : f
property |λ| : f
end script
end script
end if
end if
Line 361: Line 348:
end splitAt
end splitAt


-- intercalate :: Text -> [Text] -> Text
-- splitEvery :: Int -> [a] -> [[a]]
on intercalate(strText, lstText)
on splitEvery(n, xs)
if length of xs ≤ n then
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
{xs}
else
set my text item delimiters to dlm
set {gp, t} to splitAt(n, xs)
return strJoined
{gp} & splitEvery(n, t)
end intercalate</lang>
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}}
{{Out}}
magic(8)
magic(8)