Cantor set: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 7:
See details at this Wikipedia webpage:   [https://en.wikipedia.org/wiki/Cantor_set Cantor set]
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">V WIDTH = 81
V HEIGHT = 5
 
Line 40 ⟶ 39:
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang=Action"action!">PROC FillRect(INT x,y,w,h)
INT i
 
Line 96 ⟶ 94:
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Cantor_set.png Screenshot from Atari 8-bit computer]
 
=={{header|Ada}}==
<syntaxhighlight lang=Ada"ada">with Ada.Text_IO;
 
procedure Cantor_Set is
Line 128 ⟶ 125:
*** *** *** *** *** *** *** ***
* * * * * * * * * * * * * * * *</pre>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">BEGIN
# draw a Cantor Set using ASCII #
INT lines = 5; # number of lines for the set #
Line 165 ⟶ 161:
# # # # # # # # # # # # # # # #
</pre>
 
=={{header|ALGOL W}}==
Based on the Algol 68 sample.
<syntaxhighlight lang="algolw">begin
% draw a Cantor Set using ASCII %
integer LINES; % number of lines for the set %
Line 210 ⟶ 205:
# # # # # # # # # # # # # # # #
</pre>
 
=={{header|AppleScript}}==
 
Using composable library functions whenever possible, for better productivity:
<syntaxhighlight lang="applescript">------------------------- CANTOR SET -----------------------
 
-- cantor :: [String] -> [String]
Line 383 ⟶ 377:
███ ███ ███ ███ ███ ███ ███ ███
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">width: 81
height: 5
 
Line 415 ⟶ 408:
*** *** *** *** *** *** *** ***
* * * * * * * * * * * * * * * *</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang=AWK"awk">
# syntax: GAWK -f CANTOR_SET.AWK
# converted from C
Line 457 ⟶ 449:
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|BASIC}}==
<syntaxhighlight lang="gwbasic">10 DEFINT A-Z
20 N = 4
30 W = 3^(N-1)
Line 478 ⟶ 469:
### ### ### ###
# # # # # # # #</pre>
 
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang=BASIC256"basic256">
global ancho, alto, intervalo
ancho = 81 : alto = 5
Line 520 ⟶ 510:
Igual que la entrada de FreeBASIC.
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Cantor ← {" •" ⊏˜ >⥊¨(¯1⊸⊏⊢¨¨⊢)1‿0‿1∧⌜⍟(↕𝕩)1}</syntaxhighlight>
 
{{out}}
Line 536 ⟶ 525:
</pre>
 
=={{header|C}}==
{{trans|Kotlin}}
<syntaxhighlight lang="c">#include <stdio.h>
 
#define WIDTH 81
Line 586 ⟶ 574:
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|C sharp|C#}}==
{{trans|Java}}
<syntaxhighlight lang="csharp">using System;
 
namespace CantorSet {
Line 634 ⟶ 621:
*** *** *** *** *** *** *** ***
* * * * * * * * * * * * * * * *</pre>
 
=={{header|C++}}==
{{trans|D}}
<syntaxhighlight lang="cpp">#include <iostream>
 
const int WIDTH = 81;
Line 679 ⟶ 665:
*** *** *** *** *** *** *** ***
* * * * * * * * * * * * * * * *</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">cantor = cluster is make
rep = null
ac = array[char]
Line 720 ⟶ 705:
*** *** *** *** *** *** *** ***
* * * * * * * * * * * * * * * *</pre>
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. CANTOR.
Line 776 ⟶ 760:
### ### ### ### ### ### ### ###
# # # # # # # # # # # # # # # #</pre>
 
=={{header|D}}==
{{trans|C}}
<syntaxhighlight lang="d">import std.stdio;
 
enum WIDTH = 81;
Line 818 ⟶ 801:
*** *** *** *** *** *** *** ***
* * * * * * * * * * * * * * * *</pre>
 
=={{header|Delphi}}==
{{Trans|Java}}
<syntaxhighlight lang=Delphi"delphi">
program Cantor_set;
 
Line 875 ⟶ 857:
{{out}}
Same result of Java
 
=={{header|Excel}}==
===LAMBDA===
Line 883 ⟶ 864:
 
{{Works with|Office 365 betas 2021}}
<syntaxhighlight lang="lisp">CANTOR
=LAMBDA(n,
APPLYN(n)(
Line 976 ⟶ 957:
and also assuming the following generic bindings in the Name Manager for the WorkBook:
 
<syntaxhighlight lang="lisp">APPENDCOLS
=LAMBDA(xs,
LAMBDA(ys,
Line 1,147 ⟶ 1,128:
| 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">█</syntaxhighlight>
 
|- 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">███
█ █</syntaxhighlight>
 
Line 1,158 ⟶ 1,139:
| 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">█████████
███ ███
█ █ █ █</syntaxhighlight>
Line 1,165 ⟶ 1,146:
| 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,299 ⟶ 1,280:
| style="text-align:left" | 1
|}
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: grouping.extras io kernel math sequences
sequences.repeating ;
IN: rosetta-code.cantor-set
Line 1,332 ⟶ 1,312:
# # # # # # # # # # # # # # # #
</pre>
 
=={{header|Forth}}==
Where is says <code>[email protected]</code> it should say <code>c&#64;</code>, but I'm not keen on writing it as <code>c&amp;#64;</code> in the actual code.
<syntaxhighlight lang=Forth"forth">warnings off
 
4 \ iterations
Line 1,367 ⟶ 1,346:
### ### ### ### ### ### ### ###
# # # # # # # # # # # # # # # #</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
Const ancho = 81
Const alto = 5
Line 1,415 ⟶ 1,393:
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
</pre>
 
=={{header|Go}}==
{{trans|Kotlin}}
<syntaxhighlight lang="go">package main
 
import "fmt"
Line 1,466 ⟶ 1,443:
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">class App {
private static final int WIDTH = 81
private static final int HEIGHT = 5
Line 1,511 ⟶ 1,487:
*** *** *** *** *** *** *** ***
* * * * * * * * * * * * * * * *</pre>
 
=={{header|Haskell}}==
===Interval bars===
{{Trans|Python}} (Functional version)
<syntaxhighlight lang="haskell">-------------------------- CANTOR ------------------------
 
cantor :: [(Bool, Int)] -> [(Bool, Int)]
Line 1,551 ⟶ 1,526:
Or, using strings for the model as well as the display:
 
<syntaxhighlight lang="haskell">-------------------------- CANTOR ------------------------
 
cantor :: [String] -> [String]
Line 1,590 ⟶ 1,565:
===Dual representation===
Intervals as fraction pairs, and intervals as graphic bars:
<syntaxhighlight lang="haskell">import Control.Monad (join)
import Data.Bifunctor (bimap)
import Data.List (intercalate, mapAccumL, maximumBy)
Line 1,659 ⟶ 1,634:
███ ███ ███ ███
█ █ █ █ █ █ █ █</pre>
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang=IS"is-BASICbasic">100 PROGRAM "Cantor.bas"
110 GRAPHICS HIRES 2
120 SET PALETTE BLACK,WHITE
Line 1,672 ⟶ 1,646:
190 END IF
200 END DEF</syntaxhighlight>
 
=={{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"j">
odometer =: [: (4 $. $.) $&1
 
Line 1,733 ⟶ 1,706:
</pre>
With an `x' argument cantor_dust generalizes to higher dimensions. Try 3 cantor_dust 2
<syntaxhighlight lang=J"j">
cantor_dust =: 2&$: :(dyad define)
shape =. x # 3 ^ y
Line 1,741 ⟶ 1,714:
)
</syntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<syntaxhighlight lang="java">public class App {
private static final int WIDTH = 81;
private static final int HEIGHT = 5;
Line 1,787 ⟶ 1,759:
*** *** *** *** *** *** *** ***
* * * * * * * * * * * * * * * *</pre>
 
=={{header|JavaScript}}==
===Cantor: (Bool, Int) pairs===
{{Trans|Python}} (Functional version)
{{Trans|Haskell}}
<syntaxhighlight lang=JavaScript"javascript">(() => {
"use strict";
 
Line 1,890 ⟶ 1,861:
Using strings for the model as well as the display:
{{Trans|Haskell}}
<syntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 1,977 ⟶ 1,948:
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.
 
<syntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 2,374 ⟶ 2,345:
███ ███ ███ ███
█ █ █ █ █ █ █ █</pre>
 
=={{header|jq}}==
<syntaxhighlight lang="jq"># cantor(width; height)
def cantor($w; $h):
def init: [range(0; $h) | [range(0; $w) | "*"]];
Line 2,398 ⟶ 2,368:
yields the same output as shown e.g. under `awk`, `julia`, etc.
jq -nr --argjson width 81 --argjson height 5 -f cantor.jq
 
 
=={{header|Julia}}==
{{trans|AWK}}
<syntaxhighlight lang="julia">const width = 81
const height = 5
 
Line 2,430 ⟶ 2,398:
# # # # # # # # # # # # # # # #
</pre>
 
=={{header|Kotlin}}==
Simple terminal drawing.
<syntaxhighlight lang="scala">// Version 1.2.31
 
const val WIDTH = 81
Line 2,463 ⟶ 2,430:
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|Lua}}==
{{trans|python}}
<syntaxhighlight lang="lua">local WIDTH = 81
local HEIGHT = 5
local lines = {}
Line 2,519 ⟶ 2,485:
* * * * * * * * * * * * * * * *</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]</syntaxhighlight>
{{out}}
A graphic of a Cantor set is shown
 
=={{header|Modula-2}}==
{{trans|Kotlin}}
<syntaxhighlight lang="modula2">MODULE Cantor;
FROM Terminal IMPORT Write,WriteLn,ReadChar;
 
Line 2,577 ⟶ 2,542:
ReadChar;
END Cantor.</syntaxhighlight>
 
=={{header|Nim}}==
{{trans|Kotlin}}
 
<syntaxhighlight lang=Nim"nim">import strutils
 
const
Line 2,609 ⟶ 2,573:
*** *** *** *** *** *** *** ***
* * * * * * * * * * * * * * * *</pre>
 
=={{header|Objeck}}==
{{trans|Java}}
<syntaxhighlight lang="objeck">class CantorSet {
WIDTH : static : Int;
HEIGHT : static : Int;
Line 2,667 ⟶ 2,630:
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang=Perl"perl">use strict;
use feature 'say';
 
Line 2,707 ⟶ 2,669:
 
===regex version===
<syntaxhighlight lang="perl">#!/usr/bin/perl -l
 
use strict; # https://rosettacode.org/wiki/Cantor_set
Line 2,723 ⟶ 2,685:
# # # # # # # # # # # # # # # #
</pre>
 
=={{header|Phix}}==
Based on Algol 68, but even simpler, shorter, and sweeter!
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<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,752 ⟶ 2,713:
# # # # # # # # # # # # # # # #
</pre>
 
=={{header|Phixmonti}}==
<syntaxhighlight lang=Phixmonti"phixmonti">include ..\Utilitys.pmt
 
5 >ps
Line 2,776 ⟶ 2,736:
endfor</syntaxhighlight>
Other solution
<syntaxhighlight lang=Phixmonti"phixmonti">include ..\Utilitys.pmt
 
5 >ps
Line 2,801 ⟶ 2,761:
 
=== Press any key to exit ===</pre>
 
=={{header|Processing}}==
<syntaxhighlight lang="java">
//Aamrun, 1st July 2022
 
Line 2,820 ⟶ 2,779:
}
</syntaxhighlight>
 
=={{header|Python}}==
===Imperative===
<syntaxhighlight lang="python">WIDTH = 81
HEIGHT = 5
 
Line 2,856 ⟶ 2,814:
Separating ''(Bool, Int)'' model from ''String'' display:
{{Works with|Python|3.7}}
<syntaxhighlight lang="python">'''Cantor set – separating model from display'''
 
from functools import (reduce)
Line 2,942 ⟶ 2,900:
{{Trans|Haskell}}
{{Works with|Python|3.7}}
<syntaxhighlight lang="python">'''Cantor set – strings as both model and display.'''
 
from itertools import (chain, islice)
Line 3,022 ⟶ 2,980:
===Dual representations – fractional and graphic===
{{Works with|Python|3.7}}
<syntaxhighlight lang="python">'''A Cantor set generator, and two different
representations of its output.
'''
Line 3,158 ⟶ 3,116:
███ ███ ███ ███
█ █ █ █ █ █ █ █</pre>
 
=={{header|QB64}}==
{{trans|FreeBASIC}}
Line 3,164 ⟶ 3,121:
Note: Other languages will need to zero out the a() array. In QB64 all arrays are initialized to zero at program start.
 
<syntaxhighlight lang="qb64">_Title "Cantor Set"
 
Dim Shared As Integer sw, sh, wide, high
Line 3,202 ⟶ 3,159:
Next
End Sub</syntaxhighlight>
 
=={{header|Quackery}}==
 
===Using an L-System===
 
<syntaxhighlight lang=Quackery"quackery"> [ $ "" swap witheach
[ nested quackery join ] ] is expand ( $ --> $ )
Line 3,237 ⟶ 3,193:
q q q q q q q q q q q q q q q q
</pre>
 
 
=={{header|R}}==
[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"r">
cantorSet <- function() {
depth <- 6L
Line 3,299 ⟶ 3,253:
Currently, RosettaCode does not seem to accept image upload anymore.
So, you will have to run the program under R or RStudio IDE to get the diagram result.
 
=={{header|Racket}}==
{{trans|Kotlin}}
 
<syntaxhighlight lang="racket">#lang racket/base
;; {trans|Kotlin}}
 
Line 3,335 ⟶ 3,288:
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Kotlin}}
<syntaxhighlight lang="raku" line>sub cantor ( Int $height ) {
my $width = 3 ** ($height - 1);
 
Line 3,367 ⟶ 3,319:
███ ███ ███ ███ ███ ███ ███ ███
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █</pre>
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program displays an ASCII diagram of a Canter Set as a set of (character) lines. */
w= linesize() /*obtain the width of the display term.*/
if w==0 then w= 81 /*Can't obtain width? Use the default.*/
Line 3,410 ⟶ 3,361:
■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Cantor set
 
Line 3,468 ⟶ 3,418:
 
[https://www.dropbox.com/s/ap7c3301i0syh4e/CantorSet.jpg?dl=0 Cantor set]
 
=={{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.
<syntaxhighlight lang="ruby">lines = 5
 
(0..lines).each do |exp|
Line 3,487 ⟶ 3,436:
 
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
use convert_base::Convert;
use std::fmt;
Line 3,566 ⟶ 3,514:
 
</pre>
 
=={{header|Scala}}==
===Imperative Programming (Q&D)===
<syntaxhighlight lang=Scala"scala">object CantorSetQD extends App {
val (width, height) = (81, 5)
 
Line 3,595 ⟶ 3,542:
 
===Functional Programming (Recommended)===
<syntaxhighlight lang=Scala"scala">object CantorSetFP extends App {
val (width, height) = (81, 5)
 
Line 3,629 ⟶ 3,576:
{{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)].
 
=={{header|Sidef}}==
{{trans|Raku}}
<syntaxhighlight lang="ruby">func cantor (height) {
var width = 3**(height - 1)
var lines = height.of { "\N{FULL BLOCK}" * width }
Line 3,661 ⟶ 3,607:
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
</pre>
 
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
Smalltalk represents Intervals' start and stop values as Fraction, so precision is kept for quilte a while.
<syntaxhighlight lang=Smalltalk"smalltalk">Object subclass: CantorSet [
 
| intervals |
Line 3,739 ⟶ 3,684:
### ### ### ### ### ### ### ###
# # # # # # # # # # # # # # # #</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Const WIDTH = 81
Line 3,788 ⟶ 3,732:
*** *** *** *** *** *** *** ***
* * * * * * * * * * * * * * * *</pre>
 
=={{header|Vlang}}==
{{trans|Go}}
<syntaxhighlight lang="vlang">const (
width = 81
height = 5
Line 3,826 ⟶ 3,769:
* * * * * * * * * * * * * * * *
</pre>
 
 
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascript">var width = 81
var height = 5
 
Line 3,858 ⟶ 3,799:
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">proc Cantor(N, LineSeg, Len); \Delete middle third of LineSeg
int N; char LineSeg; int Len, Third, I;
[if N>0 and Len>1 then
Line 3,888 ⟶ 3,828:
# # # # # # # # # # # # # # # #
</pre>
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">const WIDTH=81, HEIGHT=5;
var lines=HEIGHT.pump(List,List.createLong(WIDTH,"\U2588;").copy); // full block
10,333

edits