How to resolve the algorithm Latin Squares in reduced form step by step in the Haskell programming language
How to resolve the algorithm Latin Squares in reduced form step by step in the Haskell programming language
Table of Contents
Problem Statement
A Latin Square is in its reduced form if the first row and first column contain items in their natural order. The order n is the number of items. For any given n there is a set of reduced Latin Squares whose size increases rapidly with n. g is a number which identifies a unique element within the set of reduced Latin Squares of order n. The objective of this task is to construct the set of all Latin Squares of a given order and to provide a means which given suitable values for g any element within the set may be obtained. For a reduced Latin Square the first row is always 1 to n. The second row is all Permutations/Derangements of 1 to n starting with 2. The third row is all Permutations/Derangements of 1 to n starting with 3 which do not clash (do not have the same item in any column) with row 2. The fourth row is all Permutations/Derangements of 1 to n starting with 4 which do not clash with rows 2 or 3. Likewise continuing to the nth row. Demonstrate by:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Latin Squares in reduced form step by step in the Haskell programming language
The given Haskell code defines functions to generate and print Latin squares of various orders. A Latin square is an n×n square filled with n distinct symbols, each of which occurs exactly once in each row and column of the square.
Here's a breakdown of the code:
-
Importing Modules:
import Data.List (permutations, (\\)) import Control.Monad (foldM, forM_)
This line imports necessary modules from the Haskell standard library.
-
latinSquares
Function:latinSquares :: Eq a => [a] -> [[[a]]]
This function generates a list of Latin squares of order
n
, wheren
is the number of symbols in the set provided as the input. It returns a list of lists of lists, representing all possible Latin squares of ordern
.- If the input set is empty, it returns an empty list.
- Otherwise, it generates a list of
perm
(tail of grouped permutations of the set) and applies theaddRow
function to each permutation, accumulating the results insquares
.
-
addRow
Function:addRow tbl rows = [ zipWith (:) row tbl | row <- rows , and $ different (tail row) (tail tbl) ]
This function takes a current Latin square
tbl
, a list of rowsrows
, and generates new Latin squares by adding each row fromrows
totbl
. It checks that the resulting square is still a Latin square by ensuring that the tail of the new row (excluding the first element) is different from the tails of all rows intbl
. -
different
Function:different = zipWith $ (not .) . elem
This function checks if two lists are different. It uses the
zipWith
function to combine the elements of two lists into pairs and then applies the(not .) . elem
function to each pair. This function returnsTrue
if the second element is not present in the first element (andFalse
otherwise). -
groupedPermutations
Function:groupedPermutations :: Eq a => [a] -> [[[a]]]
This function generates a list of lists of permutations of the input set. Each sublist represents a possible order in which the symbols can appear in a Latin square.
-
printTable
Function:printTable :: Show a => [[a]] -> IO ()
This function is used to print a Latin square as a table.
-
task1
andtask2
: These are two tasks defined in the code.task1
generates and prints Latin squares of order 4.task2
calculates and prints the number of possible Latin squares for different orders.
Overall, this code provides a way to generate and analyze Latin squares of various orders.
Source code in the haskell programming language
import Data.List (permutations, (\\))
import Control.Monad (foldM, forM_)
latinSquares :: Eq a => [a] -> [[[a]]]
latinSquares [] = []
latinSquares set = map reverse <$> squares
where
squares = foldM addRow firstRow perm
perm = tail (groupedPermutations set)
firstRow = pure <$> set
addRow tbl rows = [ zipWith (:) row tbl
| row <- rows
, and $ different (tail row) (tail tbl) ]
different = zipWith $ (not .) . elem
groupedPermutations :: Eq a => [a] -> [[[a]]]
groupedPermutations lst = map (\x -> (x :) <$> permutations (lst \\ [x])) lst
printTable :: Show a => [[a]] -> IO ()
printTable tbl = putStrLn $ unlines $ unwords . map show <$> tbl
task1 = do
putStrLn "Latin squares of order 4:"
mapM_ printTable $ latinSquares [1..4]
task2 = do
putStrLn "Sizes of latin squares sets for different orders:"
forM_ [1..6] $ \n ->
let size = length $ latinSquares [1..n]
total = fact n * fact (n-1) * size
fact i = product [1..i]
in printf "Order %v: %v*%v!*%v!=%v\n" n size n (n-1) total
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the REXX programming language
You may also check:How to resolve the algorithm Create a file step by step in the Stata programming language
You may also check:How to resolve the algorithm System time step by step in the TI-89 BASIC programming language
You may also check:How to resolve the algorithm Draw a rotating cube step by step in the Python programming language
You may also check:How to resolve the algorithm Population count step by step in the 8086 Assembly programming language