如何将函数放入模块中?
我已经有了这个功能:
isSortedUp x y z = if x>y && y>z then True else False
并且我想将它放入模块 UP 中。
我想将此函数放入模块中:
isSortedDown x y z = if x<y && y<z then True else False
然后在主程序中调用它们:
import System.Environment
import Up
import Down
main = do
args<-getArgs
let a = args !! 0
let b = args !! 1
let c = args !! 2
if (isSortedUp a b c) || (isSortedDown a b c) then return (True) else return(False)
如何放置和调用此函数?
新代码 Main.hs
import System.Environment
import Up
import Down
main = do
args<-getArgs
let a = args !! 0
let b = args !! 1
let c = args !! 2
if (isSortedUp a b c) || (isSortedDown a b c) then return(True) else return(False)
Up.hs
module Up (isSortedUp) where
isSortedUp x y z = if x>y && y>z then return(True) else return(False)
Down.hs
module Down (isSortedDown) where
isSortedDown x y z = if x<y && y<z then return(True) else return(False)
I've got the function:
isSortedUp x y z = if x>y && y>z then True else False
and I want to put it into module UP.
This function I want to put into module down:
isSortedDown x y z = if x<y && y<z then True else False
And then call them in the main program:
import System.Environment
import Up
import Down
main = do
args<-getArgs
let a = args !! 0
let b = args !! 1
let c = args !! 2
if (isSortedUp a b c) || (isSortedDown a b c) then return (True) else return(False)
How to put and call this functions?
New code
Main.hs
import System.Environment
import Up
import Down
main = do
args<-getArgs
let a = args !! 0
let b = args !! 1
let c = args !! 2
if (isSortedUp a b c) || (isSortedDown a b c) then return(True) else return(False)
Up.hs
module Up (isSortedUp) where
isSortedUp x y z = if x>y && y>z then return(True) else return(False)
Down.hs
module Down (isSortedDown) where
isSortedDown x y z = if x<y && y<z then return(True) else return(False)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Haskell 中的模块按文件分解。因此,要将
isSortedDown
放入其自己的模块Down
中,您需要创建一个新文件Down.hs
并将其内容与一起放入其中>module
声明:然后,如果您的
Main
模块可以访问此模块(例如在同一目录中),则它应该导入并可访问。有关 Haskell 中模块的更多信息,请阅读:
自己的模块”部分——靠近底部)
Modules in Haskell are broken up by file. So to put
isSortedDown
in it's own moduleDown
, you'd create a new fileDown.hs
and drop it's contents inside with themodule
declaration:Then, providing your
Main
module has access to this module (in the same directory, for instance), it should import and be accessible.For more information on modules in Haskell, read:
own modules" section -- near the bottom)
请注意,您可以简单地写:
Note that you can write simply: