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>-- ID MATRIX -----------------------------------------------------------------
<lang AppleScript>--------------------- IDENTITY MATRIX ----------------------


-- idMatrix :: Int -> [(0|1)]
-- identityMatrix :: Int -> [(0|1)]
on idMatrix(n)
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 idMatrix
end identityMatrix




-- TEST ----------------------------------------------------------------------
--------------------------- TEST ---------------------------
on run
on run
unlines(map(showList, ¬
idMatrix(5)
identityMatrix(5)))
end run
end run




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


-- enumFromTo :: Int -> Int -> [Int]
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
on enumFromTo(m, n)
if n < m then
if m n then
set d to -1
set lst to {}
repeat with i from m to n
set end of lst to i
end repeat
lst
else
else
set d to 1
{}
end if
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
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 s to xs as text
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</lang>
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>{{1, 0, 0, 0, 0},
<pre>[1, 0, 0, 0, 0]
{0, 1, 0, 0, 0},
[0, 1, 0, 0, 0]
{0, 0, 1, 0, 0},
[0, 0, 1, 0, 0]
{0, 0, 0, 1, 0},
[0, 0, 0, 1, 0]
{0, 0, 0, 0, 1}}</pre>
[0, 0, 0, 0, 1]</pre>


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==