How to resolve the algorithm Knuth shuffle step by step in the Egel programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Knuth shuffle step by step in the Egel programming language
Table of Contents
Problem Statement
The Knuth shuffle (a.k.a. the Fisher-Yates shuffle) is an algorithm for randomly shuffling the elements of an array.
Implement the Knuth shuffle for an integer array (or, if possible, an array of any type).
Given an array items with indices ranging from 0 to last, the algorithm can be defined as follows (pseudo-code):
(These are listed here just for your convenience; no need to demonstrate them on the page.)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Knuth shuffle step by step in the Egel programming language
Source code in the egel programming language
import "prelude.eg"
import "random.ego"
using System
using List
using Math
def swap =
[ I J XX -> insert I (nth J XX) (insert J (nth I XX) XX) ]
def shuffle =
[ XX ->
let INDICES = reverse (fromto 0 ((length XX) - 1)) in
let SWAPS = map [ I -> I (between 0 I) ] INDICES in
foldr [I J -> swap I J] XX SWAPS ]
def main = shuffle (fromto 1 9)
You may also check:How to resolve the algorithm Arrays step by step in the Picat programming language
You may also check:How to resolve the algorithm Maze generation step by step in the Node.js programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Simple turtle graphics step by step in the J programming language
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the NetRexx programming language