自己写的练习题(yaht exercise 3.10)

发布于 2022-08-11 22:03:52 字数 3492 浏览 15 评论 9

只看了头三章
花了好长时间才写的这段代码
贴出来大家批评

  1. yaht exercise 3.10:
  2. Write a program that will repeatedly ask the user for numbers until she
  3. types in zero, at which point it will tell her the sum of all the numbers, the product of
  4. all the numbers, and, for each number, its factorial. For instance, a session might look
  5. like:
  6. Give me a number (or 0 to stop):
  7. 5
  8. Give me a number (or 0 to stop):
  9. 8
  10. Give me a number (or 0 to stop):
  11. 2
  12. Give me a number (or 0 to stop):
  13. 0
  14. The sum is 15
  15. The product is 80
  16. 5 factorial is 120
  17. 8 factorial is 40320
  18. 2 factorial is 2

复制代码

module Main where

import IO

factorial  0 = 1
factorial  n = n * factorial (n - 1)

toNum s = read s + 0

toLine s =  s ++ " factorial is " ++ show (factorial (read s))

getList = do
        putStrLn "Give me a number (or 0 to stop):"
        word <- getLine
        if word == "0"
                then return []
                else do
                        rest <- getList
                        return (word : rest)

showFacLines (f:r) = do
        if r == []
                then do
                        putStrLn f
                else do
                        putStrLn f
                        showFacLines r

main = do
        list <- getList
        if list == []
                then do
                        putStrLn "You enter nothing!"
                else do
                        putStrLn ("The sum is " ++ show (foldl (+) 0 (map toNum list)))
                        putStrLn ("The product is " ++ show (foldl (*) 1 (map toNum list)))
                        showFacLines (map toLine list)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

写下不归期 2022-08-21 20:23:13

原帖由 flw 于 2009-3-25 19:57 发表
为什么要把所有函数的类型签名都放在开头啊?
这个习惯看上去和大多数 Haskell 代码的风格不符哦。

呵呵,習慣先設計函數接口后做實現。

冷清清 2022-08-21 20:22:45

倒是和 C 的代码布局有点神似
感觉这样挺好的

[ 本帖最后由 izhier 于 2009-3-25 20:45 编辑 ]

泪痕残 2022-08-21 20:22:02

为什么要把所有函数的类型签名都放在开头啊?
这个习惯看上去和大多数 Haskell 代码的风格不符哦。

蝶…霜飞 2022-08-21 19:42:04

楼上代码很是漂亮

我要继续努力呀

揽清风入怀 2022-08-21 12:51:41

改成尾递归形式。

  1. 1 import Control.Monad
  2. 2 import Data.List
  3. 3
  4. 4 main :: IO ()
  5. 5 factorial :: (Num t) => t -> t
  6. 6 listFromInput :: (Read a) =>        (String -> Bool) -> String -> [a] -> IO [a]
  7. 7
  8. 8 main = do
  9. 9   nums <- listFromInput ("0" ==) "Give me a number (or 0 to stop):" [0]
  10. 10   putStrLn $ (++) "The sum is " $ show $ sum $ drop 1 $ reverse nums
  11. 11   putStrLn $ (++) "The product is " $ show $ product $ drop 1 $ reverse nums
  12. 12   mapM_ (a ->
  13. 13            putStrLn ((show a) ++ " factorial is " ++ (show $ factorial a))
  14. 14         ) $ drop 1 $ reverse nums
  15. 15
  16. 16 factorial 1 = 1
  17. 17 factorial a = a * factorial (a - 1)
  18. 18
  19. 19 listFromInput until prompt result = do
  20. 20   putStrLn prompt
  21. 21   line <- getLine
  22. 22   if until line
  23. 23     then return result
  24. 24     else listFromInput until prompt ((read line) : result)

复制代码
[ 本帖最后由 Magicloud 于 2009-3-26 09:24 编辑 ]

卖梦商人 2022-08-21 12:16:21

缩进小了,果然好看 !

痕至 2022-08-19 04:44:11

原帖由 izhier 于 2009-3-25 13:18 发表
vim 中设置 tab 是占四格的
没想到论坛中把 tab 转换成八个格了

你还是用空格吧。
别用 TAB 了。

我就用空格,两个空格。
Haskell 里面对齐和缩进都有,用 TAB 非常容易导致问题。
比 Python 都容易出问题。

梦屿孤独相伴 2022-08-17 02:19:08

vim 中设置 tab 是占四格的
没想到论坛中把 tab 转换成八个格了

羁客 2022-08-13 13:42:31

这缩进……
我的经验是,Haskell 代码还是用两个空格的缩进吧。
最多不能超过四个空格。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文