Cantor set: Difference between revisions
m
syntax highlighting fixup automation
(Added credits) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 10:
=={{header|11l}}==
{{trans|Python}}
<
V HEIGHT = 5
Line 31:
L(i) 0 .< HEIGHT
V beg = WIDTH * i
print((lines[beg .< beg + WIDTH]).join(‘’))</
{{out}}
<pre>
Line 42:
=={{header|Action!}}==
<
INT i
Line 93:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Cantor_set.png Screenshot from Atari 8-bit computer]
=={{header|Ada}}==
<
procedure Cantor_Set is
Line 121:
Ada.Text_IO.Put_Line (Image (L));
end loop;
end Cantor_Set;</
{{out}}
<pre>*********************************************************************************
Line 130:
=={{header|ALGOL 68}}==
<
# draw a Cantor Set using ASCII #
INT lines = 5; # number of lines for the set #
Line 156:
segment width OVERAB 3
OD
END</
{{out}}
<pre>
Line 168:
=={{header|ALGOL W}}==
Based on the Algol 68 sample.
<
% draw a Cantor Set using ASCII %
integer LINES; % number of lines for the set %
Line 201:
end for_l
end
end.</
{{out}}
<pre>
Line 214:
Using composable library functions whenever possible, for better productivity:
<
-- cantor :: [String] -> [String]
Line 376:
set my text item delimiters to dlm
str
end unlines</
{{Out}}
<pre>█████████████████████████████████████████████████████████████████████████████████
Line 386:
=={{header|Arturo}}==
<
height: 5
Line 406:
loop lines 'line
-> print join line</
{{out}}
Line 417:
=={{header|AWK}}==
<
# syntax: GAWK -f CANTOR_SET.AWK
# converted from C
Line 448:
cantor(start+seg*2,seg,indx+1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 459:
=={{header|BASIC}}==
<
20 N = 4
30 W = 3^(N-1)
Line 472:
120 MID$(L$,P,S) = SPACE$(S)
130 P = P+S
140 GOTO 100</
{{out}}
<pre>###########################
Line 481:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<
global ancho, alto, intervalo
ancho = 81 : alto = 5
Line 515:
next i
End
</syntaxhighlight>
{{out}}
<pre>
Line 522:
=={{header|BQN}}==
<
{{out}}
Line 539:
=={{header|C}}==
{{trans|Kotlin}}
<
#define WIDTH 81
Line 576:
print();
return 0;
}</
{{output}}
Line 589:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
namespace CantorSet {
Line 627:
}
}
}</
{{out}}
<pre>*********************************************************************************
Line 637:
=={{header|C++}}==
{{trans|D}}
<
const int WIDTH = 81;
Line 672:
return 0;
}</
{{out}}
<pre>*********************************************************************************
Line 681:
=={{header|CLU}}==
<
rep = null
ac = array[char]
Line 713:
cs: string := cantor$make(81, 5, '*')
stream$puts(po, cs)
end start_up</
{{out}}
<pre>*********************************************************************************
Line 722:
=={{header|COBOL}}==
<
PROGRAM-ID. CANTOR.
Line 769:
BLANK-CHAR.
MOVE SPACE TO CHAR(POS).
ADD 1 TO POS.</
{{out}}
<pre>#################################################################################
Line 779:
=={{header|D}}==
{{trans|C}}
<
enum WIDTH = 81;
Line 811:
writeln(lines[beg..beg+WIDTH]);
}
}</
{{out}}
<pre>*********************************************************************************
Line 821:
=={{header|Delphi}}==
{{Trans|Java}}
<
program Cantor_set;
Line 871:
Readln;
end.
</syntaxhighlight>
{{out}}
Line 883:
{{Works with|Office 365 betas 2021}}
<
=LAMBDA(n,
APPLYN(n)(
Line 972:
)
)
)</
and also assuming the following generic bindings in the Name Manager for the WorkBook:
<
=LAMBDA(xs,
LAMBDA(ys,
Line 1,132:
)
)
)</
{{Out}}
Line 1,147:
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
| style="text-align:right; " | SHOWCANTOR(CANTOR(0))
| style="text-align:left; font-weight:bold" | <syntaxhighlight lang=text>█</
|- style="text-align:right;"
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
| style="text-align:right; " | SHOWCANTOR(CANTOR(1))
| style="text-align:left; background-color:#cbcefb;" | <syntaxhighlight lang=text>███
█ █</
|- style="text-align:right;"
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
| style="text-align:right; " | SHOWCANTOR(CANTOR(2))
| style="text-align:left; font-weight:bold" | <syntaxhighlight lang=text>█████████
███ ███
█ █ █ █</
|- style="text-align:right;"
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
| style="text-align:right; " | SHOWCANTOR(CANTOR(3))
| style="text-align:left; font-weight:bold" | <syntaxhighlight lang=text>███████████████████████████
█████████ █████████
███ ███ ███ ███
█ █ █ █ █ █ █ █</
|}
Line 1,301:
=={{header|Factor}}==
<
sequences.repeating ;
IN: rosetta-code.cantor-set
Line 1,323:
concat print ;
depth <iota> [ print-cantor ] each</
{{out}}
<pre>
Line 1,335:
=={{header|Forth}}==
Where is says <code>[email protected]</code> it should say <code>c@</code>, but I'm not keen on writing it as <code>c&#64;</code> in the actual code.
<
4 \ iterations
Line 1,360:
: go print BEGIN step done? UNTIL ;
go bye</
Output:
<pre>#################################################################################
Line 1,369:
=={{header|FreeBASIC}}==
<
Const ancho = 81
Const alto = 5
Line 1,406:
Next i
End
</syntaxhighlight>
{{out}}
<pre>
Line 1,418:
=={{header|Go}}==
{{trans|Kotlin}}
<
import "fmt"
Line 1,456:
fmt.Println(string(line[:]))
}
}</
{{out}}
Line 1,469:
=={{header|Groovy}}==
{{trans|Java}}
<
private static final int WIDTH = 81
private static final int HEIGHT = 5
Line 1,504:
}
}
}</
{{out}}
<pre>*********************************************************************************
Line 1,515:
===Interval bars===
{{Trans|Python}} (Functional version)
<
cantor :: [(Bool, Int)] -> [(Bool, Int)]
Line 1,541:
where
c True = '*'
c False = ' '</
{{Out}}
<pre>*********************************************************************************
Line 1,551:
Or, using strings for the model as well as the display:
<
cantor :: [String] -> [String]
Line 1,580:
. pred
)
)</
{{Out}}
<pre>█████████████████████████████████████████████████████████████████████████████████
Line 1,590:
===Dual representation===
Intervals as fraction pairs, and intervals as graphic bars:
<
import Data.Bifunctor (bimap)
import Data.List (intercalate, mapAccumL, maximumBy)
Line 1,648:
go x
| 1 /= x = '/' : show x
| otherwise = []</
{{Out}}
<pre>(0, 1)
Line 1,661:
=={{header|IS-BASIC}}==
<
110 GRAPHICS HIRES 2
120 SET PALETTE BLACK,WHITE
Line 1,671:
180 CALL CANTOR(X+2*L/3,Y-HEIGHT,L/3,HEIGHT)
190 END IF
200 END DEF</
=={{header|J}}==
The argument to the cantor_dust monad is an integer that describes the depth of the dust. Shown here are results for cantor_dust 2 and for cantor_dust 3 . It works by checking for 1 digits in the base 3 representation of the coordinates. These background coordinates are plotted with # character using ASCII art. 1j1 #"1 expands the lines to improve aspect ratio on character cell (console) display. }:"1 curtails the extra space character line by line. < draws a pretty box.
<syntaxhighlight lang=J>
odometer =: [: (4 $. $.) $&1
Line 1,684:
< (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a
)
</syntaxhighlight>
<pre>
Line 1,733:
</pre>
With an `x' argument cantor_dust generalizes to higher dimensions. Try 3 cantor_dust 2
<syntaxhighlight lang=J>
cantor_dust =: 2&$: :(dyad define)
shape =. x # 3 ^ y
Line 1,740:
< (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)} a
)
</syntaxhighlight>
=={{header|Java}}==
{{trans|Kotlin}}
<
private static final int WIDTH = 81;
private static final int HEIGHT = 5;
Line 1,780:
}
}
</syntaxhighlight>
{{out}}
<pre>*********************************************************************************
Line 1,792:
{{Trans|Python}} (Functional version)
{{Trans|Haskell}}
<
"use strict";
Line 1,879:
// MAIN ---
return main();
})();</
{{Out}}
<pre>*********************************************************************************
Line 1,890:
Using strings for the model as well as the display:
{{Trans|Haskell}}
<
"use strict";
Line 1,961:
// MAIN ---
return main();
})();</
{{Out}}
<pre>█████████████████████████████████████████████████████████████████████████████████
Line 1,977:
In the case of languages like Javascript which lack a built-in Ratio type, or standard Fraction/Ratio library, rendering stages of the set elaboration as lists of fraction pairs may take more work than rendering them as graphic lines.
<
"use strict";
Line 2,363:
// MAIN ---
return main();
})();</
{{Out}}
<pre>(0, 1)
Line 2,376:
=={{header|jq}}==
<
def cantor($w; $h):
def init: [range(0; $h) | [range(0; $w) | "*"]];
Line 2,393:
cantor($width; $height)
| pp</
{{out}}
With the above in a file, cantor.jq, the following incantation
Line 2,402:
=={{header|Julia}}==
{{trans|AWK}}
<
const height = 5
Line 2,422:
print(Char(lines[i, j]), j == width ? "\n" : "")
end
</
<pre>
#################################################################################
Line 2,433:
=={{header|Kotlin}}==
Simple terminal drawing.
<
const val WIDTH = 81
Line 2,453:
cantor(0, WIDTH, 1)
lines.forEach { println(it) }
}</
{{output}}
Line 2,466:
=={{header|Lua}}==
{{trans|python}}
<
local HEIGHT = 5
local lines = {}
Line 2,511:
end
print()
end</
{{out}}
<pre>*********************************************************************************
Line 2,519:
* * * * * * * * * * * * * * * *</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{{out}}
A graphic of a Cantor set is shown
Line 2,525:
=={{header|Modula-2}}==
{{trans|Kotlin}}
<
FROM Terminal IMPORT Write,WriteLn,ReadChar;
Line 2,576:
ReadChar;
END Cantor.</
=={{header|Nim}}==
{{trans|Kotlin}}
<
const
Line 2,601:
cantor(0, Width, 1)
for line in lines:
echo line</
{{Out}}
Line 2,612:
=={{header|Objeck}}==
{{trans|Java}}
<
WIDTH : static : Int;
HEIGHT : static : Int;
Line 2,657:
};
}
}</
{{Output}}
Line 2,670:
=={{header|Perl}}==
{{trans|Raku}}
<
use feature 'say';
Line 2,697:
}
say for cantor(5);</
{{Out}}
Line 2,707:
===regex version===
<
use strict; # https://rosettacode.org/wiki/Cantor_set
Line 2,714:
$_ = '#' x 81;
1 while print, s/(#+)\1\1/ $1 . $1 =~ tr!#! !r . $1 /ge;</
{{out}}
<pre>
Line 2,726:
=={{header|Phix}}==
Based on Algol 68, but even simpler, shorter, and sweeter!
<!--<
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
Line 2,743:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</
{{out}}
<pre>
Line 2,754:
=={{header|Phixmonti}}==
<
5 >ps
Line 2,774:
cps
line ?
endfor</
Other solution
<
5 >ps
Line 2,792:
endfor
endfor
line ?</
{{out}}
<pre>#################################################################################
Line 2,803:
=={{header|Processing}}==
<
//Aamrun, 1st July 2022
Line 2,819:
cantorSet(100,10,900,10,1,10,5);
}
</syntaxhighlight>
=={{header|Python}}==
===Imperative===
<
HEIGHT = 5
Line 2,846:
for i in xrange(HEIGHT):
beg = WIDTH * i
print ''.join(lines[beg : beg+WIDTH])</
{{out}}
<pre>*********************************************************************************
Line 2,856:
Separating ''(Bool, Int)'' model from ''String'' display:
{{Works with|Python|3.7}}
<
from functools import (reduce)
Line 2,929:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>
Line 2,942:
{{Trans|Haskell}}
{{Works with|Python|3.7}}
<
from itertools import (chain, islice)
Line 3,012:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>█████████████████████████████████████████████████████████████████████████████████
Line 3,022:
===Dual representations – fractional and graphic===
{{Works with|Python|3.7}}
<
representations of its output.
'''
Line 3,147:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>(0, 1)
Line 3,164:
Note: Other languages will need to zero out the a() array. In QB64 all arrays are initialized to zero at program start.
<
Dim Shared As Integer sw, sh, wide, high
Line 3,201:
Next
Next
End Sub</
=={{header|Quackery}}==
Line 3,207:
===Using an L-System===
<
[ nested quackery join ] ] is expand ( $ --> $ )
Line 3,227:
dip [ 3 / ] ] ]
2drop
</syntaxhighlight>
{{out}}
Line 3,242:
[https://creativecommons.org/licenses/by-nc-sa/4.0 Attribution-NonCommercial-ShareAlike 4.0 International, CC BY-NC-SA 4.0] to neonira@gmail.com
<syntaxhighlight lang=R>
cantorSet <- function() {
depth <- 6L
Line 3,294:
list(graph = g, data = df, set = cs)
}
</syntaxhighlight>
{{out}}
Line 3,303:
{{trans|Kotlin}}
<
;; {trans|Kotlin}}
Line 3,327:
(module+ main
(for-each displayln (Cantor_set)))
</syntaxhighlight>
{{out}}
<pre>*********************************************************************************
Line 3,339:
(formerly Perl 6)
{{trans|Kotlin}}
<syntaxhighlight lang=raku
my $width = 3 ** ($height - 1);
Line 3,359:
}
.say for cantor(5);</
{{Out}}
Line 3,369:
=={{header|REXX}}==
<
w= linesize() /*obtain the width of the display term.*/
if w==0 then w= 81 /*Can't obtain width? Use the default.*/
Line 3,384:
say $ /*display a line of the Cantor Set. */
#= # % 3 /*the part (thirds) to be blanked out. */
end /*j*/ /*stick a fork in it, we're all done. */</
This REXX program makes use of '''linesize''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
Line 3,412:
=={{header|Ring}}==
<
# Project : Cantor set
Line 3,464:
cantor(x+floor(lens*2/3),y,floor(lens/3))
ok
</syntaxhighlight>
Output image:
Line 3,471:
=={{header|Ruby}}==
This works by numbering the segments (starting with 0) in base 3. Print whitespace if this number contains the digit 1; a black square otherwise.
<
(0..lines).each do |exp|
Line 3,478:
puts chars.map{ |c| c * seg_size }.join
end
</syntaxhighlight>
{{out}}
<pre>█████████████████████████████████████████████████████████████████████████████████
Line 3,489:
=={{header|Rust}}==
<
use convert_base::Convert;
use std::fmt;
Line 3,555:
}
</syntaxhighlight>
{{out}}
<pre>
Line 3,569:
=={{header|Scala}}==
===Imperative Programming (Q&D)===
<
val (width, height) = (81, 5)
Line 3,590:
cantor(0, width, 1)
lines.foreach(l => println(l.mkString))
}</
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/QrqaHeu/0 (JavaScript)]
or by [https://scastie.scala-lang.org/4JTi1zXzRq6H4yUlAYNTSw Scastie (JVM)].
===Functional Programming (Recommended)===
<
val (width, height) = (81, 5)
Line 3,626:
output.map(l => (0 to width).map(pos => if (l contains pos) '*' else ' ').mkString)
.mkString("\n"))
}</
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/szUjPWO/7 (JavaScript)]
or by [https://scastie.scala-lang.org/ZaNUtEOcStuBfJImOnEz5Q Scastie (JVM)].
Line 3,632:
=={{header|Sidef}}==
{{trans|Raku}}
<
var width = 3**(height - 1)
var lines = height.of { "\N{FULL BLOCK}" * width }
Line 3,652:
}
cantor(5).each { .say }</
{{out}}
<pre>
Line 3,665:
{{works with|GNU Smalltalk}}
Smalltalk represents Intervals' start and stop values as Fraction, so precision is kept for quilte a while.
<
| intervals |
Line 3,732:
]
TestCantor iterations: 4.</
Output:
<pre>#################################################################################
Line 3,742:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Const WIDTH = 81
Line 3,781:
End Sub
End Module</
{{out}}
<pre>*********************************************************************************
Line 3,791:
=={{header|Vlang}}==
{{trans|Go}}
<
width = 81
height = 5
Line 3,816:
println(line.bytestr())
}
}</
{{out}}
Line 3,830:
=={{header|Wren}}==
{{trans|Kotlin}}
<
var height = 5
Line 3,848:
cantor.call(0, width, 1)
for (i in 0...height) System.print(lines[i].join())</
{{out}}
Line 3,860:
=={{header|XPL0}}==
<
int N; char LineSeg; int Len, Third, I;
[if N>0 and Len>1 then
Line 3,878:
Text(0, LineSeg);
];
]</
{{out}}
Line 3,890:
=={{header|zkl}}==
<
var lines=HEIGHT.pump(List,List.createLong(WIDTH,"\U2588;").copy); // full block
Line 3,902:
}(0,WIDTH,1);
lines.pump(Console.println,"concat");</
{{out}}
<pre>
|