Identity matrix: Difference between revisions

m
→‎{{header|AppleScript}}: tidied, updated output.
m (→‎{{header|Factor}}: update for new matrix vocabulary)
m (→‎{{header|AppleScript}}: tidied, updated output.)
Line 326:
=={{header|AppleScript}}==
 
<lang AppleScript>-- ID MATRIX ------------------------------------------- IDENTITY MATRIX ----------------------
 
-- idMatrixidentityMatrix :: Int -> [(0|1)]
on idMatrixidentityMatrix(n)
set xs to enumFromTo(1, n)
script row
on |λ|(x)
script col
on |λ|(i)
if i = x then
Line 343:
end |λ|
end script
map(col, xs)
map(result, xs)
end |λ|
end script
map(row, xs)
end idMatrixidentityMatrix
 
 
-- TEST ------------------------------------------- TEST ---------------------------
on run
unlines(map(showList, ¬
idMatrix(5)
identityMatrix(5)))
end run
 
 
-- GENERIC FUNCTIONS ------------------------------------ GENERIC FUNCTIONS ---------------------
 
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if nm < mn then
set dlst to -1{}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
else
set d to 1{}
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
 
 
-- intercalate :: String -> [String] -> String
on intercalate(delim, xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, delim}
set lsts to {}xs as text
set my text item delimiters to dlm
s
end intercalate
 
 
-- map :: (a -> b) -> [a] -> [b]
Line 387 ⟶ 397:
end tell
end map
 
 
-- Lift 2nd class handler function into 1st class script wrapper
Line 398 ⟶ 409:
end script
end if
end mReturn</lang>
 
 
-- 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}}
<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}}==
9,655

edits