How to improve the source I wrote in "Bingo Card Creation Problem"

Asked 2 years ago, Updated 2 years ago, 57 views

Sorry for the vague question, but
I'd appreciate it if you could help me

初心Ruby Beginners must see it!?I'll show you a refactoring scene of "Bingo Card Creation Problem" #codeiq
http://blog.jnito.com/entry/2015/03/06/090106
I tried the bingo card creation problem in Haskell

=================
■Rules ■
①The values in each column must meet the following conditions:
 B—One of the following:
 I—One of 16 to 30
 N:31 to 45
 G:46 to 60
 O: Any of 61 to 75
②Generate a different card each time
③None of the numbers must be duplicated
④Separate each column with a pipe (|)
⑤Print numbers and BINGO characters in right-aligned format
⑥Print a space in the middle (where it will be FREE)
=================

 {-#OPTOINS-Wall-Werror#-}
{-# LANGUAGE OverloadedStrings, ViewPatterns#-}

import qualified Data.List as L
import qualified Data.Text as T
import qualified System.Random as R

typeRangeUnit=(Int, Int)

header="BINGO"
bingSize = T.length header
numrange = 15
rangeUnit = take bingSize $zip [1, numrange+1..] [numrange, numrange*2..]

body=do
    nums<-mapM(createRondomRangeBingSize) rangeUnit
    return $formatCell22nums

viewBingo=do
    b<-body
    return$map formatline$map appendSeparatedWord$headerList:b
    where
        headerList=map(\x->justifyRightSpace$T.pack[x])$T.unpack header
        appendSeparatedWord=L.intersperspe $T.pack"|"
        formatline = fold T.append T.empty

createRondomRange::Int->RangeUnit->IO [Int]
createRondomRange (start, end) = do
    gen<-R.newStdGen
    return$takerange.L.nub$R.randomRs(start, end)gen

formatCell::Int->Int->[[Int]]->[[T.Text]]
formatCellrowIndex colIndex values= 
    L.transpose$mmap(justifyRightSpace.centerReplaced)vals
    where
        centerReplaced x = if centerVal == x then " else T.pack $show x
        centerVal=vals!!rowIndex!!colIndex

justifyRightSpace=T.justifyRight2'' 

mmap f = map(map f)

main=do
    b<-viewBingo
    mapM_printb

■Contents of questions
·This is the best way to write like Haskell
·With this convenient function, you don't have to go around like this
·In the first place, the way of thinking about writing is bad (ry

)

I would like you to help me improve the sauce.

Also, regarding the range of values in の
import qualified Data.List.Split as S
ranges=S.splitEvery15[1..75]

by shuffling the values in the ranges case list created in I wanted to make it, but I didn't know how.
Is there a function that randomly replaces elements in the list?

haskell

2022-09-30 19:28

1 Answers

To randomly replace elements in a list, use the random-shuffle package.

The process of creating the bingo itself and the process of displaying it are mixed together, making it difficult to read overall.In particular, when formatting each cell, you check whether it is in the middle every time.

I'm a little lazy, but how about the following?

The bingo itself is represented by [Maybe Int]] and generates a new bingo with the bingo function.Place Nothing because there is no value in the middle.The indexer replaces the elements in the list using the lens package.

After you create a bingo, use the showBingo function to convert it into a string.

 {-#LANGUAGE FlexibleContexts, TypeFamilies#-}

import Control.Lens(.~), (&), ix)
import Control.Monad.Random.Class (MonadRandom)
import Data.List (intercalate, translate)
import Data.List.Split(chunksOf)
import System.Random.Shuffle(shuffleM)

type Bingo=[[Maybe Int]]

bingo ::MonadRandom m=>m Bingo
bingo=replaceCenter<$>mapM(fmap(map Just.take5).shuffleM) (chunksOf15[1..75])
  where
    replaceCenter l=l&ix2.ix2.~Nothing

showBingo::Bingo->String
showBingo=unlines$"B|I|N|G|O"—map showRow (transpose bingo)
  where
    showRow=intercalate"|".map showCell
    showCell (Just n) | n<10 = ': show n
                      | otherwise=show n
    showCell Nothing=""

main=bingo>>=putStr.showBingo

As checked in GHC-7.10.1, you may need to add some import in earlier versions.


2022-09-30 19:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.