Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
108
Task/Identity-matrix/AppleScript/identity-matrix-1.applescript
Normal file
108
Task/Identity-matrix/AppleScript/identity-matrix-1.applescript
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
--------------------- IDENTITY MATRIX ----------------------
|
||||
|
||||
-- identityMatrix :: Int -> [(0|1)]
|
||||
on identityMatrix(n)
|
||||
set xs to enumFromTo(1, n)
|
||||
|
||||
script row
|
||||
on |λ|(x)
|
||||
script col
|
||||
on |λ|(i)
|
||||
if i = x then
|
||||
1
|
||||
else
|
||||
0
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
map(col, xs)
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
map(row, xs)
|
||||
end identityMatrix
|
||||
|
||||
|
||||
--------------------------- TEST ---------------------------
|
||||
on run
|
||||
|
||||
unlines(map(showList, ¬
|
||||
identityMatrix(5)))
|
||||
|
||||
end run
|
||||
|
||||
|
||||
-------------------- GENERIC FUNCTIONS ---------------------
|
||||
|
||||
-- enumFromTo :: Int -> Int -> [Int]
|
||||
on enumFromTo(m, n)
|
||||
if m ≤ n then
|
||||
set lst to {}
|
||||
repeat with i from m to n
|
||||
set end of lst to i
|
||||
end repeat
|
||||
lst
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end enumFromTo
|
||||
|
||||
|
||||
-- intercalate :: String -> [String] -> String
|
||||
on intercalate(delim, xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, delim}
|
||||
set s to xs as text
|
||||
set my text item delimiters to dlm
|
||||
s
|
||||
end intercalate
|
||||
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
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
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
|
||||
-- showList :: [a] -> String
|
||||
on showList(xs)
|
||||
"[" & intercalate(", ", map(my str, xs)) & "]"
|
||||
end showList
|
||||
|
||||
|
||||
-- str :: a -> String
|
||||
on str(x)
|
||||
x as string
|
||||
end str
|
||||
|
||||
|
||||
-- unlines :: [String] -> String
|
||||
on unlines(xs)
|
||||
-- A single string formed by the intercalation
|
||||
-- of a list of strings with the newline character.
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, linefeed}
|
||||
set s to xs as text
|
||||
set my text item delimiters to dlm
|
||||
s
|
||||
end unlines
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
on indentityMatrix(n)
|
||||
set digits to {}
|
||||
set m to n - 1
|
||||
repeat (n + m) times
|
||||
set end of digits to 0
|
||||
end repeat
|
||||
set item n of digits to 1
|
||||
|
||||
set matrix to {}
|
||||
repeat with i from n to 1 by -1
|
||||
set end of matrix to items i thru (i + m) of digits
|
||||
end repeat
|
||||
|
||||
return matrix
|
||||
end indentityMatrix
|
||||
|
||||
return indentityMatrix(5)
|
||||
|
|
@ -0,0 +1 @@
|
|||
{{1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 0, 1, 0}, {0, 0, 0, 0, 1}}
|
||||
Loading…
Add table
Add a link
Reference in a new issue