简体   繁体   English

在Haskell中输出不带括号且在一定范围内的列表?

[英]Outputting a list in Haskell without the brackets, and over a range?

I have a function that returns a list, call it fn . 我有一个返回列表的函数,将其称为fn

If I print the list, it looks like [1,2,3,4] whereas I want it to look like 1 2 3 4 . 如果我打印列表,它看起来像[1,2,3,4]而我希望它看起来像1 2 3 4

Also I want to output many lists over a range of n , so f 1 on the first line, f 2 on the second line, ... fn on the nth line. 我也想在n的范围内输出许多列表,所以第一行上的f 1 ,第二行上的f 2 ,第n行上的... fn

I have my fn function working but cannot get the output to work for the life of me. 我的fn函数正常工作,但无法在我的一生fn输出正常工作。

Current attempt: 当前尝试:

main = do
    n <- readLn :: IO Int
    mapM_ putStrLn [f i | i <- [1..n]]

You can do something like 你可以做类似的事情

list_to_string = unwords . map show

to get the output format you want. 获得所需的输出格式。 Then instead of doing 然后,而不是做

print [1, 2, 3, 4]

you can do 你可以做

putStrLn $ list_to_string $ [1, 2, 3, 4]

which should display how you want. 它应该显示您想要的方式。

Edit: As for printing a range, try this: 编辑:至于打印范围,请尝试以下操作:

main = do
  n <- readLn :: IO Int
  mapM_ (putStrLn . list_to_string) [ f i | i <- [1..n] ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM