模式匹配可变大小的Haskell列表 [英] Pattern Matching a Haskell list of variable size
问题描述
编辑:当这个累了的时候,我不应该编码。我正在编译与运行时不同的程序副本。
我有以下代码可以在我的程序启动时使单个参数成为可选项。
main = do
args< - getArgs
handleArgs args
handleArgs :: [String] - > IO()
handleArgs(server:nick:channel:[])= IRC.startIRC server 6667 nick channel
handleArgs(server:port:nick:channel:[])= IRC.startIRC server port :: Int)nick channel
handleArgs _ = putStrLn给出的参数不正确。
如果我像 ./程序运行它irc.freenode.net 6667 somenick #somechannel
,它运行。但是,如果我像 ./ program irc.freenode.net somenick #somechannel
运行它,这应该使args成为irc.freenode列表。 net:somenick:#somechannel:[]
如果我理解正确,
会给出一个模式匹配错误,指向 args< - getArgs
行,当我尝试用ghc编译后运行它时。
更准确地说,错误是:
<$ c $
检查你的shell是否以#
开头的部分作为注释来解释,例如(shopt)上的 interactive_comments
。 #somechannel
可能会被解释为注释。
EDIT: I shouldn't be coding when this tired. I was compiling a different copy of the program than I was running. Sorry for wasting your time.
I have the following code to make a single argument optional in my program's startup.
main = do
args <- getArgs
handleArgs args
handleArgs :: [String] -> IO ()
handleArgs (server:nick:channel:[]) = IRC.startIRC server 6667 nick channel
handleArgs (server:port:nick:channel:[]) = IRC.startIRC server (read port :: Int) nick channel
handleArgs _ = putStrLn "Incorrect arguments given."
If I run it like ./program irc.freenode.net 6667 somenick #somechannel
, it runs. However, if I run it like ./program irc.freenode.net somenick #somechannel
, which should make args be a list "irc.freenode.net":"somenick":"#somechannel":[]
if I understand it correctly,
it gives a pattern match error pointing to the args <- getArgs
line when I try to run it after compiling with ghc.
More precisely, the error is:
mbot: user error (Pattern match failure in do expression at core.hs:9:4-32)
Check that your shell is not interpreting the part starting with #
as a comment, e.g. bash with interactive_comments
on (shopt). #somechannel
may be interpreted as a comment.
这篇关于模式匹配可变大小的Haskell列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!