Help us understand the problem. What is going on with this article?

2と0の列から「2020」を見つける

ランダムに並んだ「2」と「0」の列から「2020」を見つけてハイライトする。

年賀状のためにPostScriptで書いたものを、Haskellで書き直した。もっといろいろな書き方ができそう。シェルスクリプトとかでも書けそう。

module Main where

import System.Environment (getArgs)
import System.Random
import Data.List

scan2020 :: [Int] -> [[Int]]
scan2020 [] = []
scan2020 ls@(x:xs)
  | isPrefixOf the2020 ls = the2020 : (scan2020 $ drop 4 ls)
  | otherwise = [x] : scan2020 xs
  where the2020 = [2, 0, 2, 0]

hl2020 :: [[Int]] -> [String]
hl2020 [] = []
hl2020 (x:xs)
  | length x == 4 =
      concat ["\ESC[31m", intercalate " " $ map show x, "\ESC[m"] : hl2020 xs
  | otherwise = concatMap show x : hl2020 xs

main :: IO ()
main = do
  a <- fmap (read . head) getArgs
  s <- getStdGen
  putStrLn $ intercalate " " $ hl2020 $ take a $ scan2020 $ twoandzero s
  where
    twoandzero s = map ((*2) . flip mod 2) (randoms s :: [Int])

image.png

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away