Identity matrix: Difference between revisions
Content added Content deleted
m (→{{header|Factor}}: update for new matrix vocabulary) |
m (→{{header|AppleScript}}: tidied, updated output.) |
||
Line 326: | Line 326: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
<lang AppleScript> |
<lang AppleScript>--------------------- IDENTITY MATRIX ---------------------- |
||
-- |
-- identityMatrix :: Int -> [(0|1)] |
||
on |
on identityMatrix(n) |
||
set xs to enumFromTo(1, n) |
set xs to enumFromTo(1, n) |
||
script row |
script row |
||
on |λ|(x) |
on |λ|(x) |
||
script |
script col |
||
on |λ|(i) |
on |λ|(i) |
||
if i = x then |
if i = x then |
||
Line 343: | Line 343: | ||
end |λ| |
end |λ| |
||
end script |
end script |
||
map(col, xs) |
|||
map(result, xs) |
|||
end |λ| |
end |λ| |
||
end script |
end script |
||
map(row, xs) |
map(row, xs) |
||
end |
end identityMatrix |
||
--------------------------- TEST --------------------------- |
|||
on run |
on run |
||
unlines(map(showList, ¬ |
|||
idMatrix(5) |
|||
identityMatrix(5))) |
|||
end run |
end run |
||
-------------------- GENERIC FUNCTIONS --------------------- |
|||
-- enumFromTo :: Int -> Int -> [Int] |
-- enumFromTo :: Int -> Int -> [Int] |
||
on enumFromTo(m, n) |
on enumFromTo(m, n) |
||
if |
if m ≤ n then |
||
set |
set lst to {} |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
else |
else |
||
{} |
|||
end if |
end if |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end enumFromTo |
end enumFromTo |
||
-- intercalate :: String -> [String] -> String |
|||
on intercalate(delim, xs) |
|||
set {dlm, my text item delimiters} to ¬ |
|||
{my text item delimiters, delim} |
|||
⚫ | |||
set my text item delimiters to dlm |
|||
s |
|||
end intercalate |
|||
-- map :: (a -> b) -> [a] -> [b] |
-- map :: (a -> b) -> [a] -> [b] |
||
Line 387: | Line 397: | ||
end tell |
end tell |
||
end map |
end map |
||
-- Lift 2nd class handler function into 1st class script wrapper |
-- Lift 2nd class handler function into 1st class script wrapper |
||
Line 398: | Line 409: | ||
end script |
end script |
||
end if |
end if |
||
end mReturn |
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</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre>[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]</pre> |
|||
=={{header|Applesoft BASIC}}== |
=={{header|Applesoft BASIC}}== |