免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 8214 | 回复: 20
打印 上一主题 下一主题

自己写的练习题(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)

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2009-03-25 10:45 |只看该作者
这缩进……
我的经验是,Haskell 代码还是用两个空格的缩进吧。
最多不能超过四个空格。

论坛徽章:
0
3 [报告]
发表于 2009-03-25 13:18 |只看该作者

回复 #2 flw 的帖子

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

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2009-03-25 16:13 |只看该作者
原帖由 izhier 于 2009-3-25 13:18 发表
vim 中设置 tab 是占四格的
没想到论坛中把 tab 转换成八个格了

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

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

论坛徽章:
0
5 [报告]
发表于 2009-03-25 17:03 |只看该作者
缩进小了,果然好看 !

论坛徽章:
0
6 [报告]
发表于 2009-03-25 17:39 |只看该作者
改成尾递归形式。

  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 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2009-03-25 18:10 |只看该作者
楼上代码很是漂亮

我要继续努力呀

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
8 [报告]
发表于 2009-03-25 19:57 |只看该作者

回复 #6 Magicloud 的帖子

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

论坛徽章:
0
9 [报告]
发表于 2009-03-25 20:44 |只看该作者
倒是和 C 的代码布局有点神似
感觉这样挺好的

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

论坛徽章:
0
10 [报告]
发表于 2009-03-25 22:26 |只看该作者
原帖由 flw 于 2009-3-25 19:57 发表
为什么要把所有函数的类型签名都放在开头啊?
这个习惯看上去和大多数 Haskell 代码的风格不符哦。

呵呵,習慣先設計函數接口后做實現。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP