为什么这段代码不起作用?
xMal 0 _ = return () xMal n text = do text xMal (n-1) text main = xMal (putStrLn t <- getLine) (n <- readLn)
我希望能够得到一个数字和字符串作为输入.
Haskell不会那样工作,使用你的<-
表达式,你进行monadic调用,这些应该放在正确的顺序上.你可能想要:
xMal 0 _ = [] xMal n text = text : xMal (n-1) text main = do n <- readLn t <- getLine putStrLn $ show $ xMal n t
这xMal
是一个纯函数:xMal :: (Eq a, Num a) => a -> t -> [t]
复制给定的t
对象a
时间.
main
是一个IO
从stdin读取一行并将其作为整数解析的monad,然后它将一行读作一个字符串,最后它显示复制的字符串.就像是:
*Main> main 15 Hello ["Hello","Hello","Hello","Hello","Hello","Hello","Hello","Hello","Hello","Hello","Hello","Hello","Hello","Hello","Hello"]
但请注意,您xMal
已经存在:replicate :: Int -> a -> [a]
已经这样做了.