How to resolve the algorithm Sierpinski carpet step by step in the F# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sierpinski carpet step by step in the F# programming language

Table of Contents

Problem Statement

Produce a graphical or ASCII-art representation of a Sierpinski carpet of order   N.

For example, the Sierpinski carpet of order   3   should look like this: The use of the   #   character is not rigidly required for ASCII art. The important requirement is the placement of whitespace and non-whitespace characters.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sierpinski carpet step by step in the F# programming language

Source code in the fsharp programming language

open System

let blank x = new String(' ', String.length x)

let nextCarpet carpet = 
  List.map (fun x -> x + x + x) carpet @
  List.map (fun x -> x + (blank x) + x) carpet @
  List.map (fun x -> x + x + x) carpet
 
let rec sierpinskiCarpet n =
  let rec aux n carpet =
    if n = 0 then carpet
             else aux (n-1) (nextCarpet carpet)
  aux n ["#"]
 
List.iter (printfn "%s") (sierpinskiCarpet 3)


  

You may also check:How to resolve the algorithm JortSort step by step in the Java programming language
You may also check:How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Dot product step by step in the 11l programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the D programming language
You may also check:How to resolve the algorithm Sierpinski arrowhead curve step by step in the 11l programming language