How to resolve the algorithm Permutations step by step in the Tailspin programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Permutations step by step in the Tailspin programming language

Table of Contents

Problem Statement

Write a program that generates all   permutations   of   n   different objects.   (Practically numerals!)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Permutations step by step in the Tailspin programming language

Source code in the tailspin programming language

templates permutations
  when <=1> do [1] !
  otherwise
    def n: $;
    templates expand
      def p: $;
      1..$n -> \(def k: $;
        [$p(1..$k-1)..., $n, $p($k..last)...] !\) !
    end expand
    $n - 1 -> permutations -> expand !
end permutations

def alpha: ['ABCD'...];
[ $alpha::length -> permutations -> '$alpha($)...;' ] -> !OUT::write

templates lexicalPermutations
  when <=1> do [1] !
  otherwise
    def n: $;
    def p: [ $n - 1 -> lexicalPermutations ];
    1..$n -> \(def k: $;
      $p... -> [ $k, $... -> \(when <$k..> do $+1! otherwise $!\)] !\) !
end lexicalPermutations

def alpha: ['ABCD'...];
[ $alpha::length -> lexicalPermutations -> '$alpha($)...;' ] -> !OUT::write

templates lexicalPermutations2
  def N: $;
  [[1]] -> #
  when <[<[]($N)>]> do $... !
  otherwise
    def tails: $;
    [1..$tails(1)::length+1 -> \(
      def first: $;
      $tails... -> [$first, $... -> \(when <$first..> do $+1! otherwise $!\)] !
    \)] -> #
end lexicalPermutations2

def alpha: ['ABCD'...];
[ $alpha::length -> lexicalPermutations2 -> '$alpha($)...;' ] -> !OUT::write

templates perms
  templates findPerms
    when <$@perms::length..> do $@perms !
    otherwise
      def index: $;
      $index..$@perms::length
      -> \(
          @perms([$, $index]): $@perms([$index, $])...;
          $index + 1 -> findPerms !
      \) !
      @perms([last, $index..last-1]): $@perms($index..last)...;
  end findPerms
  @: [1..$];
  1 -> findPerms !
end perms

def alpha: ['ABCD'...];
[4 -> perms -> '$alpha($)...;' ] -> !OUT::write

  

You may also check:How to resolve the algorithm Hello world/Standard error step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Julia set step by step in the Delphi programming language
You may also check:How to resolve the algorithm Assertions step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Substring step by step in the Apex programming language
You may also check:How to resolve the algorithm MD4 step by step in the Java programming language