Four sides of square: Difference between revisions

m
syntax highlighting fixup automation
(Initial Haskell version.)
m (syntax highlighting fixup automation)
Line 8:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Command_Line;
 
Line 44:
when others =>
Ada.Text_Io.Put_Line ("Usage: ./four_sides <length>");
end Four_Sides;</langsyntaxhighlight>
{{out}}
<pre>
Line 64:
 
=={{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:
draw square( 7 )
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 98:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">drawSquare: function [side][
loop 1..side 'x ->
print map 1..side 'y [
Line 107:
drawSquare 4
print ""
drawSquare 6</langsyntaxhighlight>
 
{{out}}
Line 124:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FOUR_SIDES_OF_SQUARE.AWK
BEGIN {
Line 139:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 159:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
void hollowMatrix(unsigned int n) {
Line 180:
hollowMatrix(11);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 209:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <concepts>
#include <iostream>
 
Line 239:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre> 1 1 1 1 1 1 1 1
Line 262:
 
=={{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 286:
=={{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 296:
 
hollowMatrix(9)
Sleep</langsyntaxhighlight>
{{out}}
<pre>1 1 1 1 1 1 1 1 1
Line 309:
 
===Graphical===
<langsyntaxhighlight lang="freebasic">Dim As Integer n = 9, size = 60 * n + 70
Screenres size, size, 24
Cls
Line 330:
Next x
Bsave "hollowMatrix.bmp",0
Sleep</langsyntaxhighlight>
{{out}}
https://www.dropbox.com/s/9g5ahfzw1muuzgm/hollowMatrix.bmp?dl=0
Line 336:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 357:
fmt.Println()
hollowMatrix(9)
}</langsyntaxhighlight>
 
{{out}}
Line 382:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (intercalate, intersperse)
import Data.List.Split (chunksOf)
import System.Environment (getArgs)
Line 397:
main = do
sizes <- map read <$> getArgs
putStrLn $ intercalate "\n\n" $ map square sizes</langsyntaxhighlight>
{{out}}
<pre>
Line 427:
 
Implementation:
<langsyntaxhighlight Jlang="j">fsosq=: {{+./~(+.|.)y{.1}}</langsyntaxhighlight>
 
Some examples:
 
<langsyntaxhighlight Jlang="j"> fsosq 0
fsosq 1
1
Line 456:
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'''
<langsyntaxhighlight lang="jq">def square_perimeter_matrix:
[range(0; .) | 1] as $top
| [1, (range(0; .-2) | 0), 1] as $two
Line 472:
 
def display:
map(join(" ")) | join("\n");</langsyntaxhighlight>
'''Example''':
<langsyntaxhighlight lang="jq">9|square_perimeter_matrix|display
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 491:
=={{header|Julia}}==
Gtk graphical version.
<langsyntaxhighlight lang="julia">using Gtk
 
function set_gtk_style!(widget::Gtk.GtkWidget, style::String, value::Int)
Line 518:
 
squareonesapp(8)
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Manipulate[ArrayPad[ConstantArray[0, {1, 1} n - 1], 1, 1] // Grid, {n, 2, 20, 1}]</langsyntaxhighlight>
 
=={{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 541:
 
=={{header|Processing}}==
<langsyntaxhighlight lang="java">
//Aamrun, 27th June 2022
 
Line 561:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight lang="python">size = 9
for row in range(size):
for col in range(size):
Line 572:
else:
print("0", end=" ")
print()</langsyntaxhighlight>
 
{{out}}
Line 580:
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.
 
<langsyntaxhighlight lang="python">def square(size=9):
 
def is_at_border(row, col):
Line 594:
 
suqare()
</syntaxhighlight>
</lang>
 
===Functional===
<langsyntaxhighlight lang="python">'''Four sides of a square'''
 
 
Line 649:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>1 1 1 1 1 1 1
Line 672:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ 0 over 2 - of
1 tuck join join nested
over 2 - of
Line 684:
9 four-sides
witheach
[ witheach [ echo sp ] cr ]</langsyntaxhighlight>
 
{{out}}
Line 711:
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 739:
 
=={{header|Red}}==
<langsyntaxhighlight lang="rebol">Red[]
 
view-square: function [size][
Line 761:
]
 
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 832:
next
next
</syntaxhighlight>
</lang>
Output image:
<br>
Line 838:
 
=={{header|Sidef}}==
<langsyntaxhighlight 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(' ')
}</langsyntaxhighlight>
{{out}}
<pre>
Line 854:
=={{header|Wren}}==
===Text based===
<langsyntaxhighlight lang="ecmascript">var hollowMatrix = Fn.new { |n|
for (i in 0...n) {
for (j in 0...n) {
Line 863:
}
 
hollowMatrix.call(9)</langsyntaxhighlight>
 
{{out}}
Line 881:
{{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 lang="ecmascript">import "dome" for Window
import "graphics" for Canvas, Color, Font
class Main {
Line 916:
}
 
var Game = Main.new(9)</langsyntaxhighlight>
 
{{out}}
Line 924:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">proc DrawMat(S);
int S, I, J;
[for I:= 0 to S-1 do
Line 934:
[DrawMat(6); CrLf(0);
DrawMat(7); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
10,333

edits