如何在 Haskell 中打印列表的各个元素?
How to print individual elements of a list in Haskell?
我是 Haskell 的初学者,我想知道是否可以打印给定列表的各个元素。我尝试解决这个问题但失败了。这是代码:
main :: IO()
main = do
let list = [1,2,3]
let size = length list
let temp = print_elem list size
print temp
print_elem :: [Int] -> Int -> Int
print_elem xs x = do
let size = length xs
let idx = size - x
let element = xs !! idx
putStr (show element)
putStr(" ")
let dummy = print_elem (xs (x-1))
return " "
我想打印这样的东西
1 2 3
如果我简单地使用 putStr (show list)
它将显示 [1,2,3]
而我不想要那个。
但是当我运行这段代码出现多个错误
printelem.hs:14:5: error:
* Couldn't match expected type `Int' with actual type `IO b0'
* In a stmt of a 'do' block: putStr (show element)
In the expression:
do let size = length xs
let idx = size - x
let element = xs !! idx
putStr (show element)
....
In an equation for `print_elem':
print_elem xs x
= do let size = ...
let idx = ...
let element = ...
....
|
14 | putStr (show element)
| ^^^^^^^^^^^^^^^^^^^^^
printelem.hs:16:29: error:
* Couldn't match expected type `Int -> [Int]'
with actual type `[Int]'
* The function `xs' is applied to one argument,
but its type `[Int]' has none
In the first argument of `print_elem', namely `(xs (x - 1))'
In the expression: print_elem (xs (x - 1))
|
16 | let dummy = print_elem (xs (x-1))
| ^^^^^^^^
你如何解决这个问题?
你想得太急迫了。首先,您需要一个字符串列表,而不是一个整数列表。那是 map
:
> map show [1,2,3]
["1","2","3"]
接下来,您想将它们连接成一个单独的 space 字符串。那是 Data.List.intercalate
:
> import Data.List
> intercalate " " (map show [1,2,3])
"1 2 3"
然后您可以将其传递给 putStrLn
(print
将为您提供已有字符串的字符串表示形式):
import Data.List
main :: IO()
main = do
let list = [1,2,3]
putStrLn (intercalate " " (map show list))
一种方法是
> mapM_ putStr (intersperse " " (map show [1,2,3])) >> putStrLn ""
1 2 3
it :: ()
这会逐一打印元素,通过 show
转换为字符串,中间穿插空格。
> intersperse " " (map show [1,2,3])
["1"," ","2"," ","3"]
it :: [[Char]]
mapM_
在每个元素上映射一个 IO
动作构造函数 putStr :: String -> IO ()
,并将它们作为一个组合的动作序列执行。
最后这段代码打印换行符。
我是 Haskell 的初学者,我想知道是否可以打印给定列表的各个元素。我尝试解决这个问题但失败了。这是代码:
main :: IO()
main = do
let list = [1,2,3]
let size = length list
let temp = print_elem list size
print temp
print_elem :: [Int] -> Int -> Int
print_elem xs x = do
let size = length xs
let idx = size - x
let element = xs !! idx
putStr (show element)
putStr(" ")
let dummy = print_elem (xs (x-1))
return " "
我想打印这样的东西
1 2 3
如果我简单地使用 putStr (show list)
它将显示 [1,2,3]
而我不想要那个。
但是当我运行这段代码出现多个错误
printelem.hs:14:5: error:
* Couldn't match expected type `Int' with actual type `IO b0'
* In a stmt of a 'do' block: putStr (show element)
In the expression:
do let size = length xs
let idx = size - x
let element = xs !! idx
putStr (show element)
....
In an equation for `print_elem':
print_elem xs x
= do let size = ...
let idx = ...
let element = ...
....
|
14 | putStr (show element)
| ^^^^^^^^^^^^^^^^^^^^^
printelem.hs:16:29: error:
* Couldn't match expected type `Int -> [Int]'
with actual type `[Int]'
* The function `xs' is applied to one argument,
but its type `[Int]' has none
In the first argument of `print_elem', namely `(xs (x - 1))'
In the expression: print_elem (xs (x - 1))
|
16 | let dummy = print_elem (xs (x-1))
| ^^^^^^^^
你如何解决这个问题?
你想得太急迫了。首先,您需要一个字符串列表,而不是一个整数列表。那是 map
:
> map show [1,2,3]
["1","2","3"]
接下来,您想将它们连接成一个单独的 space 字符串。那是 Data.List.intercalate
:
> import Data.List
> intercalate " " (map show [1,2,3])
"1 2 3"
然后您可以将其传递给 putStrLn
(print
将为您提供已有字符串的字符串表示形式):
import Data.List
main :: IO()
main = do
let list = [1,2,3]
putStrLn (intercalate " " (map show list))
一种方法是
> mapM_ putStr (intersperse " " (map show [1,2,3])) >> putStrLn ""
1 2 3
it :: ()
这会逐一打印元素,通过 show
转换为字符串,中间穿插空格。
> intersperse " " (map show [1,2,3])
["1"," ","2"," ","3"]
it :: [[Char]]
mapM_
在每个元素上映射一个 IO
动作构造函数 putStr :: String -> IO ()
,并将它们作为一个组合的动作序列执行。
最后这段代码打印换行符。