因为是刚开始看
haskell
所以很多概念不是很清楚,比如何时用
do
、“
<-
”和
let
。
Exerceise3.10
算是做出来,但是肯定不是一个好办法。希望给我改改吧,顺便解答一下我的疑问。不得不说的是编译信息相当地没有用啊,这真是太遗憾了。
module Main
where
import IO
main = do
list <- askForNumber
let
sAdd = show (foldl (+) 0 list)
sMul = show (foldl (*) 1 list)
putStrLn ("The sum is " ++ sAdd)
putStrLn ("The production is " ++ sMul)
putFactorial list
askForNumber = do
putStrLn "Give me a number (or 0 to top):"
sNum <- getLine
let num = read sNum
if num == 0
then return []
else do
rest <- askForNumber
return (num : rest)
putFactorial [] = do
putStr ""
putFactorial (x : rest) = do
let fact = factorial x
putStrLn ((show x) ++ " factorial is " ++ (show fact))
putFactorial(rest)
factorial 1 = 1
factorial x = x * factorial (x-1)