在 Haskell 中的程序之间传递数据
Passing data between programs in Haskell
我在 Haskell 中做两个程序,其中一个提供了一个充满值的树。
另一个程序现在必须填充同一棵树。我搜索了它,但我还没有找到关于如何做类似事情的信息。
例如,我执行 ./Generate 并用值保存树。然后我执行 ./Work 并且它使用树的值。有人可以帮我吗?
最简单的方法可能是
data MyData = ... deriving (Read, Show)
制作人
makeMyData :: MyData
makeMyData = ....
main = writeFile "output.data" (show makeMyData)
消费者
ioUseMyData :: MyData -> IO ()
ioUseMyData myData = ....
main = readFile "output.data" >>= ioUseMyData . read
您可以使用标准 input/output 使用 getContents
和 putStrLn
。
完整示例:
-- probably as module
data Tree = Node { value :: Int
, left :: Tree
, right :: Tree
}
| Empty
deriving (Read, Show)
-- your producer program
producerProgram = do
let makeTree = Node 3 (Node 5 Empty Empty) (Node 7 Empty Empty)
writeFile "output.data" (show makeTree)
-- your consumer program
consumerProgram = do
let ioUseTree t = do
let countNodes Empty = 0
countNodes (Node _ l r) = 1 + countNodes l + countNodes r
putStrLn $ "Tree with " ++ show (countNodes t) ++ " nodes"
readFile "output.data" >>= ioUseTree . read
-- simulate call both
main = do
-- produce
producerProgram
-- consume
consumerProgram
结果
Tree with 3 nodes
正在替换
writeFile "output.data" (show makeTree)
来自
print makeTree
和
readFile "output.data" >>= ioUseTree . read
来自
getContents >>= ioUseTree . read
您可以使用管道 (bash
、cmd.exe
、...)
$ ./producer | ./consumer
Tree with 3 nodes
最简单的方法是:不要使用 2 个单独的程序。如果您真的需要 2 个独立的程序,那么您需要选择 3 个项目:
- 一种将树转换为
String
或 ByteString
的方法
- 一种将字符串或字节串发送到其他程序的方法
- 一种将
String
或 ByteString
转换回树的方法
对于 #1 和 #3:使用 show
和 read
可能是最简单的,但如果您愿意,您也可以定义自己的函数,只要它们都定义相同的格式。
对于 #2:如果这 2 个程序要分开 运行 那么您别无选择:您必须使用一个文件。最简单的方法是使用 writeFile
和 readFile
函数。如果这两个程序要同时 运行 你也可以使用 Network.Socket
中的功能
我在 Haskell 中做两个程序,其中一个提供了一个充满值的树。
另一个程序现在必须填充同一棵树。我搜索了它,但我还没有找到关于如何做类似事情的信息。
例如,我执行 ./Generate 并用值保存树。然后我执行 ./Work 并且它使用树的值。有人可以帮我吗?
最简单的方法可能是
data MyData = ... deriving (Read, Show)
制作人
makeMyData :: MyData
makeMyData = ....
main = writeFile "output.data" (show makeMyData)
消费者
ioUseMyData :: MyData -> IO ()
ioUseMyData myData = ....
main = readFile "output.data" >>= ioUseMyData . read
您可以使用标准 input/output 使用 getContents
和 putStrLn
。
完整示例:
-- probably as module
data Tree = Node { value :: Int
, left :: Tree
, right :: Tree
}
| Empty
deriving (Read, Show)
-- your producer program
producerProgram = do
let makeTree = Node 3 (Node 5 Empty Empty) (Node 7 Empty Empty)
writeFile "output.data" (show makeTree)
-- your consumer program
consumerProgram = do
let ioUseTree t = do
let countNodes Empty = 0
countNodes (Node _ l r) = 1 + countNodes l + countNodes r
putStrLn $ "Tree with " ++ show (countNodes t) ++ " nodes"
readFile "output.data" >>= ioUseTree . read
-- simulate call both
main = do
-- produce
producerProgram
-- consume
consumerProgram
结果
Tree with 3 nodes
正在替换
writeFile "output.data" (show makeTree)
来自
print makeTree
和
readFile "output.data" >>= ioUseTree . read
来自
getContents >>= ioUseTree . read
您可以使用管道 (bash
、cmd.exe
、...)
$ ./producer | ./consumer
Tree with 3 nodes
最简单的方法是:不要使用 2 个单独的程序。如果您真的需要 2 个独立的程序,那么您需要选择 3 个项目:
- 一种将树转换为
String
或ByteString
的方法
- 一种将字符串或字节串发送到其他程序的方法
- 一种将
String
或ByteString
转换回树的方法
对于 #1 和 #3:使用 show
和 read
可能是最简单的,但如果您愿意,您也可以定义自己的函数,只要它们都定义相同的格式。
对于 #2:如果这 2 个程序要分开 运行 那么您别无选择:您必须使用一个文件。最简单的方法是使用 writeFile
和 readFile
函数。如果这两个程序要同时 运行 你也可以使用 Network.Socket