Four sides of square: Difference between revisions

m
(→‎{{header|Python}}: Normalized from tabs to space multiples in Calmosoft's contribution.)
 
(38 intermediate revisions by 20 users not shown)
Line 1:
{{Draft task|Matrices}}
 
;Task:
Line 6:
<br>
[http://keptarhely.eu/view.php?file=20220218v00x6hugz.jpeg Four sides of square - image]
 
===See also===
* [[Matrix with two diagonals]]
* [[Mosaic matrix]]
 
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">
V size = 9
L(row) 0 .< size
L(col) 0 .< size
I row == 0 | row == size - 1 | col == 0 | col == size - 1
print(‘1’, end' ‘ ’)
E
print(‘0’, end' ‘ ’)
print()
</syntaxhighlight>
 
{{out}}
<pre>
1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">
;;; draw a matrix with 1s on the edges and 0s elsewhere
 
;;; draws a matrix with height and width = n with 1s on the edges
PROC drawSquare( INT n )
CARD i, j
FOR i = 1 TO n DO
FOR j = 1 TO n DO
Put(' )
IF i = 1 OR i = n OR j = 1 OR j = n THEN Put('1) ELSE Put('0) FI
OD
PutE()
OD
RETURN
 
PROC Main()
drawSquare( 6 )
PutE()
drawSquare( 7 )
RETURN
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
 
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Command_Line;
 
Line 44 ⟶ 114:
when others =>
Ada.Text_Io.Put_Line ("Usage: ./four_sides <length>");
end Four_Sides;</langsyntaxhighlight>
{{out}}
<pre>
Line 61 ⟶ 131:
Usage: ./four_sides <length>
</pre>
 
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # draw a matrix with 1s on the edges and 0s elsewhere #
# draws a matrix with height and width = n with 1s on the edges #
PROC draw square = ( INT n )VOID:
Line 78 ⟶ 147:
draw square( 7 )
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 96 ⟶ 165:
1 1 1 1 1 1 1
</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">
begin % draw a matrix with 1s on the edges and 0s elsewhere %
% draws a matrix with height and width = n with 1s on the edges %
procedure drawSquare ( integer value n ) ;
for i := 1 until n do begin
for j := 1 until n do begin
writeon( s_w := 0, if i = 1 or i = n or j = 1 or j = n then " 1" else " 0" )
end for_j ;
write()
end drawSquare ;
% test the draw square procedure %
drawSquare( 6 );
write();
drawSquare( 7 )
end.
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
 
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
</pre>
 
=={{header|AppleScript}}==
Defined in terms of a generic matrix function:
<syntaxhighlight lang="applescript">------------------- FOUR SIDES OF SQUARE -----------------
 
 
-- fourSides :: Int -> [[Int]]
on fourSides(n)
-- A matrix of dimension n in which edge values are 1,
-- and other values are zero.
script go
on |λ|(i, j)
if {1, n} contains i or {1, n} contains j then
1
else
0
end if
end |λ|
end script
matrix(n, n, go)
end fourSides
 
 
--------------------------- TEST -------------------------
on run
-- Matrices of dimension 1 .. 6
script test
on |λ|(n)
showMatrix(fourSides(n)) & linefeed & linefeed
end |λ|
end script
unlines(map(test, enumFromTo(1, 6)))
end run
 
 
------------------------- MATRICES -----------------------
 
-- matrix :: Int -> Int -> ((Int, Int) -> a) -> [[a]]
on matrix(nRows, nCols, f)
-- A matrix of a given number of columns and rows,
-- in which each value is a given function of its
-- (zero-based) column and row indices.
script go
property g : mReturn(f)'s |λ|
on |λ|(iRow)
set xs to {}
repeat with iCol from 1 to nCols
set end of xs to g(iRow, iCol)
end repeat
xs
end |λ|
end script
map(go, enumFromTo(1, nRows))
end matrix
 
 
-- showMatrix :: [[Maybe a]] -> String
on showMatrix(rows)
-- String representation of rows
-- as a matrix.
script showRow
on |λ|(a, row)
set {maxWidth, prevRows} to a
script showCell
on |λ|(acc, cell)
set {w, xs} to acc
if missing value is cell then
{w, xs & ""}
else
set s to cell as string
{max(w, length of s), xs & s}
end if
end |λ|
end script
set {rowMax, cells} to foldl(showCell, {0, {}}, row)
{max(maxWidth, rowMax), prevRows & {cells}}
end |λ|
end script
set {w, stringRows} to foldl(showRow, {0, {}}, rows)
script go
on |λ|(row)
unwords(map(justifyRight(w, space), row))
end |λ|
end script
unlines(map(go, stringRows))
end showMatrix
 
 
------------------------- GENERIC ------------------------
 
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m ≤ n then
set xs to {}
repeat with i from m to n
set end of xs to i
end repeat
xs
else
{}
end if
end enumFromTo
 
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
 
 
-- justifyRight :: Int -> Char -> String -> String
on justifyRight(n, cFiller)
script
on |λ|(s)
if n > length of s then
text -n thru -1 of ((replicate(n, cFiller) as text) & s)
else
s
end if
end |λ|
end script
end justifyRight
 
 
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
 
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of 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
 
 
-- max :: Ord a => a -> a -> a
on max(x, y)
if x > y then
x
else
y
end if
end max
 
 
-- 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
 
 
-- unwords :: [String] -> String
on unwords(xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, space}
set s to xs as text
set my text item delimiters to dlm
return s
end unwords</syntaxhighlight>
{{Out}}
<pre>1
 
 
1 1
1 1
 
 
1 1 1
1 0 1
1 1 1
 
 
1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1
 
 
1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1
 
 
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">drawSquare: function [side][
loop 1..side 'x ->
print map 1..side 'y [
(any? @[x=1 y=1 x=side y=side])? -> 1 -> 0
]
]
 
drawSquare 4
print ""
drawSquare 6</syntaxhighlight>
 
{{out}}
 
<pre>1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1
 
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FOUR_SIDES_OF_SQUARE.AWK
BEGIN {
Line 112 ⟶ 474:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 130 ⟶ 492:
1 1 1 1 1 1 1
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Square ← ∨⌜˜1⌽↑⟜1‿1
 
Square 5</syntaxhighlight>
{{out}}
<pre>┌─
╵ 1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1
┘</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
void hollowMatrix(unsigned int n) {
Line 153 ⟶ 528:
hollowMatrix(11);
return 0;
}</langsyntaxhighlight>
 
{{out}}
<pre>
Line 182 ⟶ 556:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <concepts>
#include <iostream>
 
Line 212 ⟶ 586:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre> 1 1 1 1 1 1 1 1
Line 233 ⟶ 607:
1 1 1 1 1 1 1 1 1
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Classes,SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
procedure FillSquare(Memo: TMemo; Size: integer);
var X,Y: integer;
var S: string;
begin
S:='';
for Y:=1 to Size do
begin
for X:=1 to Size do
begin
if (X=1) or (X=Size) or
(Y=1) or (Y=Size) then S:=S+' 1'
else S:=S+' 0';
end;
S:=S+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
 
procedure ShowFillSquare(Memo: TMemo);
begin
FillSquare(Memo, 6);
FillSquare(Memo, 7);
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
 
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc holmat n . .
for i to n
for j to n
if i = 1 or i = n or j = 1 or j = n
write "1 "
else
write "0 "
.
.
print ""
.
.
holmat 10
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1
</pre>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Four sides of square. Nigel Galloway: February 18th., 2022
let m11 m=Array2D.init m m (fun n g->if n=0 || g=0 || g=m-1 || n=m-1 then 1 else 0)
printfn "%A\n\n%A" (m11 5) (m11 6)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 255 ⟶ 709:
[1; 1; 1; 1; 1; 1]]
</pre>
 
 
=={{header|FreeBASIC}}==
===Text based===
<langsyntaxhighlight lang="freebasic">Sub hollowMatrix(n As Integer)
For i As Integer = 0 To n
For j As Integer = 0 To n
Line 269 ⟶ 722:
 
hollowMatrix(9)
Sleep</langsyntaxhighlight>
{{out}}
<pre>1 1 1 1 1 1 1 1 1
Line 282 ⟶ 735:
 
===Graphical===
<langsyntaxhighlight lang="freebasic">Dim As Integer n = 9, size = 60 * n + 70
Screenres size, size, 24
Cls
Line 303 ⟶ 756:
Next x
Bsave "hollowMatrix.bmp",0
Sleep</langsyntaxhighlight>
{{out}}
https://www.dropbox.com/s/9g5ahfzw1muuzgm/hollowMatrix.bmp?dl=0
 
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 330 ⟶ 782:
fmt.Println()
hollowMatrix(9)
}</langsyntaxhighlight>
 
{{out}}
Line 354 ⟶ 806:
</pre>
 
=={{header|JHaskell}}==
<syntaxhighlight lang="haskell">import Data.List (intercalate, intersperse)
import Data.List.Split (chunksOf)
import System.Environment (getArgs)
 
-- An n by n square of characters, having 1s on the borders and 0s in the
-- interior. We assume n ≥ 0.
square :: Int -> String
square n = intercalate "\n" $ map (intersperse ' ') $ chunksOf n sq
where sq = [sqChar r c | r <- [0..n-1], c <- [0..n-1]]
sqChar r c = if isBorder r c then '1' else '0'
isBorder r c = r == 0 || r == n-1 || c == 0 || c == n-1
 
main :: IO ()
main = do
sizes <- map read <$> getArgs
putStrLn $ intercalate "\n\n" $ map square sizes</syntaxhighlight>
{{out}}
<pre>$ four_sides 0 1 2 3 4 5
 
 
1
 
1 1
1 1
 
1 1 1
1 0 1
1 1 1
 
1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1
 
1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1</pre>
 
 
Or, expressed in terms of Data.Matrix:
<syntaxhighlight lang="haskell">import Data.Matrix
 
------------------ FOUR SIDES OF A SQUARE ----------------
 
fourSides :: Int -> Matrix Int
fourSides n = matrix n n
(\(i, j) -> (fromEnum . or) ((==) <$> [1, n] <*> [i, j]))
 
 
--------------------------- TEST -------------------------
main :: IO ()
main = mapM_ print $ fourSides <$> [0 .. 5]</syntaxhighlight>
{{Out}}
<pre>┌ ┐
└ ┘
┌ ┐
│ 1 │
└ ┘
┌ ┐
│ 1 1 │
│ 1 1 │
└ ┘
┌ ┐
│ 1 1 1 │
│ 1 0 1 │
│ 1 1 1 │
└ ┘
┌ ┐
│ 1 1 1 1 │
│ 1 0 0 1 │
│ 1 0 0 1 │
│ 1 1 1 1 │
└ ┘
┌ ┐
│ 1 1 1 1 1 │
│ 1 0 0 0 1 │
│ 1 0 0 0 1 │
│ 1 0 0 0 1 │
│ 1 1 1 1 1 │
└ ┘</pre>
 
=={{header|J}}==
Implementation:
<langsyntaxhighlight Jlang="j">fsosq=: {{+./~(+.|.)y{.1}}</langsyntaxhighlight>
 
Some examples:
 
<langsyntaxhighlight Jlang="j"> fsosq 0
fsosq 1
1
Line 386 ⟶ 921:
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1</langsyntaxhighlight>
 
Gui examples are not visible here, but, for example:
<langsyntaxhighlight Jlang="j"> require'viewmat'
viewmat fsosq 20
viewmat fsosq 5</langsyntaxhighlight>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">def square_perimeter_matrix:
[range(0; .) | 1] as $top
| [1, (range(0; .-2) | 0), 1] as $two
| [$top, (range(0; .-2)|$two), $top];
 
def display:
map(join(" ")) | join("\n");</syntaxhighlight>
'''Example''':
<syntaxhighlight lang="jq">9|square_perimeter_matrix|display
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1
</pre>
 
=={{header|Julia}}==
Gtk graphical version.
<langsyntaxhighlight lang="julia">using Gtk
 
function set_gtk_style!(widget::Gtk.GtkWidget, style::String, value::Int)
Line 422 ⟶ 983:
 
squareonesapp(8)
</syntaxhighlight>
</lang>
 
=={{header|K}}==
<syntaxhighlight lang="k">square: {x|/:x}{x||x}@~-':!:
 
square 5</syntaxhighlight>
{{out}}
<pre>(1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1)</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Manipulate[ArrayPad[ConstantArray[0, {1, 1} n - 1], 1, 1] // Grid, {n, 2, 20, 1}]</syntaxhighlight>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that returns a square matrix with square pattern in their entries */
square(n):=genmatrix(lambda([x,y],if x=1 or y=1 or x=n or y=n then 1 else 0),n,n)$
 
/* Example */
square(6);
</syntaxhighlight>
{{out}}
<pre>
matrix(
[1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1]
)
</pre>
 
=={{header|Nim}}==
There are several ways to draw the square. Here is one of them:
<syntaxhighlight lang="Nim">import std/[sequtils, strutils]
 
proc drawSquare(n: Positive) =
let s1 = repeat(1, n).join(" ")
let s2 = (1 & repeat(0, n - 2) & 1).join(" ")
echo s1
for i in 2..<n: echo s2
echo s1
 
drawSquare(7)
</syntaxhighlight>
 
{{out}}
<pre>1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
 
my $n = 5;
say join ' ', @$_ for ([(1)x$n], (map { [1, (0)x($n-2), 1] } 0..$n-3), [(1)x$n]);</langsyntaxhighlight>
{{out}}
<pre>1 1 1 1 1
Line 440 ⟶ 1,059:
=={{header|Phix}}==
See [[Matrix_with_two_diagonals#GUI.2Fonline|Matrix_with_two_diagonals#Phix]] and press 'O'.
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* DRAW SOME SQUARES WITH 1S ON THE EDGES and 0S ELSEWHERE */
 
/* CP/M SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
 
/* TASK */
 
DRAW$SQUARE: PROCEDURE( N );
DECLARE N BYTE;
DECLARE ( I, J ) BYTE;
DO I = 1 TO N;
DO J = 1 TO N;
CALL PR$CHAR( ' ' );
IF I = 1 OR I = N OR J = 1 OR J = N THEN CALL PR$CHAR( '1' );
ELSE CALL PR$CHAR( '0' );
END;
CALL PR$NL;
END;
END DRAW$SQUARE ;
 
CALL DRAW$SQUARE( 6 );
CALL PR$NL;
CALL DRAW$SQUARE( 7 );
 
EOF
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
 
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
</pre>
 
=={{header|Processing}}==
<syntaxhighlight lang="java">
//Aamrun, 27th June 2022
 
size(1000,1000);
 
textSize(50);
 
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
noFill();
square(i*100,j*100,100);
fill(#000000);
if(i==0||i==9||j==0||j==9){
text("1",i*100+50,j*100+50);
}
else{
text("0",i*100+50,j*100+50);
}
}
}
</syntaxhighlight>
 
=={{header|Python}}==
===Procedural===
<lang python>size = 9
<syntaxhighlight lang="python">size = 9
for Row in range(size):
for Colrow in range(size):
for col in range(size):
if (Row == 0 or Row == size-1 or Col == 0 or Col == size-1):
if row == 0 or row == size-1 or col == 0 or col == size-1:
print("1", end=" ")
else:
print("0", end=" ")
print()</langsyntaxhighlight>
 
{{out}}
See Raku output.
 
===Elaborate procedural===
The following version illustrates several features of Python, such as default arguments, nested functions with lexical scoping, generators, and convenient syntax for creating sets and performing set operations such as intersection.
 
<syntaxhighlight lang="python">def square(size=9):
 
def is_at_border(row, col):
# `&` is set intersection: if the set {row, col} intersects the set
# {0, size-1}, then at least one of (row, col) is either 0 or size-1
return {row, col} & {0, size-1}
 
for row in range(size):
print(' '.join(
'1' if is_at_border(row, col) else '0'
for col in range(size)
))
 
suqare()
</syntaxhighlight>
 
===Functional===
<syntaxhighlight lang="python">'''Four sides of a square'''
 
 
# fourSides :: Int -> [[Int]]
def fourSides(n):
'''A square grid with ones in all edge values
and zeros elsewhere.
'''
edge = [1, n]
return matrix(
n, n, lambda row, col: int(
row in edge or col in edge
)
)
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Square grids of dimension 7 and 10'''
for n in [7, 10]:
print(
showMatrix(
fourSides(n)
) + '\n'
)
 
 
# ----------------------- GENERIC ------------------------
 
# matrix :: Int -> Int -> ((Int, Int) -> a) -> [[a]]
def matrix(nRows, nCols, f):
'''A matrix of a given number of columns and rows,
in which each value is a given function over the
tuple of its (one-based) row and column indices.
'''
return [
[f(y, x) for x in range(1, 1 + nCols)]
for y in range(1, 1 + nRows)
]
 
 
# showMatrix :: [[a]] -> String
def showMatrix(rows):
'''String representation of a matrix'''
return '\n'.join([
' '.join([str(x) for x in y]) for y in rows
])
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
{{Out}}
<pre>1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
 
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1</pre>
 
=={{header|Quackery}}==
<syntaxhighlight lang="quackery"> [ 0 over 2 - of
1 tuck join join nested
over 2 - of
1 rot of
nested tuck join join ] is four-sides ( n --> [ )
 
8 four-sides
witheach
[ witheach [ echo sp ] cr ]
cr
9 four-sides
witheach
[ witheach [ echo sp ] cr ]</syntaxhighlight>
 
{{out}}
 
<pre>1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1
 
1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1
</pre>
 
=={{header|Raku}}==
This isn't a matrix, especially if it is supposed to be graphical; it's a very small (or extremely low resolution) bitmap.
 
<syntaxhighlight lang="raku" perl6line>sub hollow ($n) { [1 xx $n], |(0 ^..^ $n).map( { [flat 1, 0 xx $n - 2, 1] } ), [1 xx $n] }
 
.put for hollow 7;
put '';
.put for hollow 10;</langsyntaxhighlight>
{{out}}
<pre>1 1 1 1 1 1 1
Line 484 ⟶ 1,307:
 
=={{header|Red}}==
<langsyntaxhighlight lang="rebol">Red[]
 
view-square: function [size][
Line 506 ⟶ 1,329:
]
 
view-square 9</langsyntaxhighlight>
{{out}}
https://commons.wikimedia.org/wiki/File:Hollow_matrix_gui.png
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Identity Matrix
# Date : 2022/16/02
Line 577 ⟶ 1,400:
next
next
</syntaxhighlight>
</lang>
Output image:
<br>
[http://keptarhely.eu/view.php?file=20220218v00x6hugz.jpeg Four sides of square]
 
=={{header|RPL}}==
≪ → n
≪ "" 1 n '''START''' "1 " + '''NEXT''' DUP
2 n 1 - '''START'''
"" 1 n '''FOR''' j j 1 == j n == OR →STR + " " + '''NEXT NEXT'''
n ROLL 1 n '''FOR''' j j DISP '''NEXT'''
≫ ≫ ''''TASK'''' STO
 
4 '''TASK'''
{{out}}
<pre>
1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">def square_sides(size = 9)
Array.new(size){|n| n==0 || n==size-1 ? [1]*size : [1]+[0]*(size-2)+[1]}
end
 
puts square_sides.map{|line| line.join (" ") }
</syntaxhighlight>
{{out}}
<pre>1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var n = 5
 
[n.of(1), (n-2).of([1, (n-2).of(0)..., 1])..., n.of(1)].each {|row|
say row.join(' ')
}</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1
</pre>
 
=={{header|Wren}}==
===Text based===
<langsyntaxhighlight ecmascriptlang="wren">var hollowMatrix = Fn.new { |n|
for (i in 0...n) {
for (j in 0...n) {
Line 593 ⟶ 1,467:
}
 
hollowMatrix.call(9)</langsyntaxhighlight>
 
{{out}}
Line 611 ⟶ 1,485:
{{libheader|Go-fonts}}
This is designed to look as close as possible to the Red entry's image so that we don't have to fill up Wikimedia Commons with similar looking images.
<langsyntaxhighlight ecmascriptlang="wren">import "dome" for Window
import "graphics" for Canvas, Color, Font
class Main {
Line 646 ⟶ 1,520:
}
 
var Game = Main.new(9)</langsyntaxhighlight>
 
{{out}}
Line 654 ⟶ 1,528:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">proc DrawMat(S);
int S, I, J;
[for I:= 0 to S-1 do
Line 664 ⟶ 1,538:
[DrawMat(6); CrLf(0);
DrawMat(7); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
1,973

edits