免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: izhier
打印 上一主题 下一主题

自己写的练习题(yaht exercise 3.10) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-25 09:52 |显示全部楼层 |倒序浏览
只看了头三章
花了好长时间才写的这段代码
贴出来大家批评

  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)

论坛徽章:
0
2 [报告]
发表于 2009-03-25 13:18 |显示全部楼层

回复 #2 flw 的帖子

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

论坛徽章:
0
3 [报告]
发表于 2009-03-25 17:03 |显示全部楼层
缩进小了,果然好看 !

论坛徽章:
0
4 [报告]
发表于 2009-03-25 18:10 |显示全部楼层
楼上代码很是漂亮

我要继续努力呀

论坛徽章:
0
5 [报告]
发表于 2009-03-25 20:44 |显示全部楼层
倒是和 C 的代码布局有点神似
感觉这样挺好的

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

论坛徽章:
0
6 [报告]
发表于 2009-03-26 09:18 |显示全部楼层
看来
haskell 的代码布局有点讲究
另开一帖,讲述一下,讨论讨论吧
愿闻其详

论坛徽章:
0
7 [报告]
发表于 2009-04-09 15:14 |显示全部楼层
原来 yaht 后有答案!
贴出来答案的代码:

  1. module Main
  2.     where

  3. import IO

  4. main = do
  5.   nums <- getNums
  6.   putStrLn ("The sum is " ++ show (sum nums))
  7.   putStrLn ("The product is " ++ show (product nums))
  8.   showFactorials nums

  9. getNums = do
  10.   putStrLn "Give me a number (or 0 to stop):"
  11.   num <- getLine
  12.   if read num == 0
  13.     then return []
  14.     else do rest <- getNums
  15.             return ((read num :: Int):rest)

  16. showFactorials [] = return ()
  17. showFactorials (x:xs) = do
  18.   putStrLn (show x ++ " factorial is " ++
  19.             show (factorial x))
  20.   showFactorials xs

  21. factorial 1 = 1
  22. factorial n = n * factorial (n-1)
复制代码

论坛徽章:
0
8 [报告]
发表于 2009-04-09 15:18 |显示全部楼层
只是觉得答案的代码写的比较好

论坛徽章:
0
9 [报告]
发表于 2009-04-09 15:21 |显示全部楼层
原帖由 flw 于 2009-4-9 15:20 发表

好是相对的吧!
相对当时只看到那一节的人来说,也许确实不错。

:wink:
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP