【发布时间】:2014-09-05 04:59:02
【问题描述】:
我写了以下内容来帮助大孩子完成他们的家庭教育工作,并通过学习如何编程来保持头脑工作(我认为 haskell 听起来很棒)。
main :: IO ()
main = do
putStrLn "Please enter the dividend :"
inputx <- getLine
putStrLn "Please enter the divisor :"
inputy <- getLine
let x = (read inputx) :: Int
let y = (read inputy) :: Int
let z = x `div` y
let remain = x `mod` y
putStrLn ( "Result: " ++ show x ++ " / " ++ show y ++ " = " ++ show z ++ " remainder " ++ show remain )
putStrLn ( "Proof: (" ++ show y ++ " x " ++ show z ++ ") = " ++ show (y * z) ++ " + " ++ show remain ++ " = " ++ show ((y * z) + remain))
putStrLn ( "Is this what you had? ")
他们这样做是不是更整洁/更好/更好/更紧凑的方式?
【问题讨论】:
-
风格主要是一种偏好,但我会做以下事情:
input <- putStrLn "prompt" >> getLine;顺序的 let 语句不需要在每一行上都有一个 let(即用 3 个空格替换第一个之后的所有 let),而不是按顺序排列putStrLn,我会写putStrLn $ "line 1" ++ "line 2" ++ "line 3"(每个字符串可以在自己的行上) - 只要以下行缩进,haskell 就知道它在同一语句中的部分)。 -
@user2407038 你的
putStrLn会漏掉一些换行符 ;) -
既然你没有验证输入(看看它是否真的可以被解析为字符串),你不妨直接使用 readLn 而不是 getLine。
-
您可能会在codereview.stackexchange.com 上得到更全面的回复,这是一个专门用于代码审查的网站。
标签: haskell integer-arithmetic