作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图通过编写一个简单的数学学习程序来同时学习数学和 Haskell。
它开始于:
funPercentOfNumberToAnother :: IO ()
funPercentOfNumberToAnother = do
(percentDec, percentStr) <- getPercent
ofNum <- getDecimal (200 :: Decimal)
let isNum = percentDec * ofNum
if uglyAnswers ([ofNum, isNum], [percentDec])
then do
putStrLn ("ofNum: " ++ show ofNum)
putStrLn ("isNum: " ++ show isNum)
putStrLn "___________________________"
funPercentOfNumberToAnother
else do
putStrLn ("ofNum: " ++ show ofNum)
putStrLn ("isNum: " ++ show isNum)
putStrLn "Percents"
-- putStrLn "\n\n"
putStrLn (show isNum ++ " is what percent of " ++ show ofNum ++ "?\n" )
putStr "> "
ans <- getLine
submitStringAnswer (ans, percentStr ++ "%")
submitStringAnswer :: (String, String) -> IO ()
submitStringAnswer (myAns, correctAns) = do
if myAns == correctAns
then putStrLn "Congratz!"
else do
putStrLn ("Sorry the correct answer is: " ++ show correctAns)
pause
pause :: IO ()
pause = do
x <- getLine
putStrLn ""
__ __ _ _ _ _
| \/ | __ _| |_| |__ ___ _ __ ___ __ _| |_(_) ___ ___
| |\/| |/ _` | __| '_ \ / _ \ '_ ` _ \ / _` | __| |/ __/ __|
| | | | (_| | |_| | | | __/ | | | | | (_| | |_| | (__\__
|_| |_|\__,_|\__|_| |_|\___|_| |_| |_|\__,_|\__|_|\___|___/
1.) Learn Math
2.) Math Lookup
3.) Quit Excolo
1
ofNum: 35
isNum: 15.75
___________________________
ofNum: 120
isNum: 102
Percents
102 is what percent of 120?
>
Sorry the correct answer is: "85%"
15.75 is what percent of 35?
>
Sorry the correct answer is: "45%"
__ __ _ _ _ _
| \/ | __ _| |_| |__ ___ _ __ ___ __ _| |_(_) ___ ___
| |\/| |/ _` | __| '_ \ / _ \ '_ ` _ \ / _` | __| |/ __/ __|
| | | | (_| | |_| | | | __/ | | | | | (_| | |_| | (__\__
|_| |_|\__,_|\__|_| |_|\___|_| |_| |_|\__,_|\__|_|\___|___/
1.) Learn Math
2.) Math Lookup
3.) Quit Excolo
1
ofNum: 80
isNum: 44
Percents
44 is what percent of 80?
>
Sorry the correct answer is: "55%"
__ __ _ _ _ _
| \/ | __ _| |_| |__ ___ _ __ ___ __ _| |_(_) ___ ___
| |\/| |/ _` | __| '_ \ / _ \ '_ ` _ \ / _` | __| |/ __/ __|
| | | | (_| | |_| | | | __/ | | | | | (_| | |_| | (__\__
|_| |_|\__,_|\__|_| |_|\___|_| |_| |_|\__,_|\__|_|\___|___/
1.) Learn Math
2.) Math Lookup
3.) Quit Excolo
1
ofNum: 15
isNum: 2.25
___________________________
ofNum: 60
isNum: 0.6
___________________________
ofNum: 40
isNum: 30
Percents
30 is what percent of 40?
>
Sorry the correct answer is: "75%"
0.6 is what percent of 60?
>
Sorry the correct answer is: "1%"
2.25 is what percent of 15?
>
Sorry the correct answer is: "15%"
最佳答案
在 funPercentOfNumberToAnother
,你有这个条款:
if uglyAnswers ([ofNum, isNum], [percentDec])
then do
putStrLn ("ofNum: " ++ show ofNum)
putStrLn ("isNum: " ++ show isNum)
putStrLn "___________________________"
funPercentOfNumberToAnother
else do
putStrLn ("ofNum: " ++ show ofNum)
putStrLn ("isNum: " ++ show isNum)
putStrLn "Percents"
if
条款?不在
then
中的东西或
else
无论如何都会执行分支,一旦
then
或
else
执行完毕。
funPercentOfNumberToAnother
中使用它。功能。或者,您可以将函数主体的其余部分(向用户显示数字的部分)拉入
else
if
的一部分,这样你就不会对丑陋的数字这样做。
关于haskell - 为什么程序流控制会随机执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45849782/
我是一名优秀的程序员,十分优秀!