【发布时间】:2011-10-20 22:06:38
【问题描述】:
我有这个功能:
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)
向上.hs
module Up (isSortedUp) where
isSortedUp x y z = if x>y && y>z then return(True) else return(False)
下来.hs
module Down (isSortedDown) where
isSortedDown x y z = if x<y && y<z then return(True) else return(False)
【问题讨论】:
-
作为一个小评论:不要写
if P then True else False,只写P。同样在 monad 中:if P then return True else return False和return P一样简单。最后,注意isSortedDown a b c = isSortedUp c b a.