[英]How do I print out numbers in ascending order in Haskell?
我试图打印出这样的东西:
1: *****
2: ****
3: ***
4: ******
我已经拥有的代码打印出星号和冒号,但我不知道如何让它打印出左侧数字的升序。当我输入数字列表时,代码会打印出星号的数量基于列表中的每个元素。 我不能做的是打印出左边的数字,这基本上是说列表中星号数量的 position 是什么。 我尝试打印列表[1..10]
的头部,然后使用xs
of (x:xs)
对其进行递归,但它仍然给我错误。
这是我已经拥有的代码:
printPattern :: Int -> Char -> [String]
printPattern len a = map (take len . cycle) [[a]]
printStars :: Int -> IO()
printStars x = mapM_ putStrLn (printPattern x '*')
displayGame :: [Int] -> IO(Int)
displayGame (x:xs) = do putStr " : "
printStars x
displayGame xs
displayGame :: [Int] -> IO ()
displayGame xs = for_ (zip [1..] xs) $ \(i, x) ->
putStr $ show i ++ ": " ++ replicate x '*' ++ "\n\n"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.