自己写的练习题(yaht exercise 3.10)
只看了头三章
花了好长时间才写的这段代码
贴出来大家批评
- yaht exercise 3.10:
- Write a program that will repeatedly ask the user for numbers until she
- types in zero, at which point it will tell her the sum of all the numbers, the product of
- all the numbers, and, for each number, its factorial. For instance, a session might look
- like:
- Give me a number (or 0 to stop):
- 5
- Give me a number (or 0 to stop):
- 8
- Give me a number (or 0 to stop):
- 2
- Give me a number (or 0 to stop):
- 0
- The sum is 15
- The product is 80
- 5 factorial is 120
- 8 factorial is 40320
- 2 factorial is 2
复制代码
module Main where import IO factorial 0 = 1 toNum s = read s + 0 toLine s = s ++ " factorial is " ++ show (factorial (read s)) getList = do showFacLines (f:r) = do main = do |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
呵呵,習慣先設計函數接口后做實現。
倒是和 C 的代码布局有点神似
感觉这样挺好的
[ 本帖最后由 izhier 于 2009-3-25 20:45 编辑 ]
为什么要把所有函数的类型签名都放在开头啊?
这个习惯看上去和大多数 Haskell 代码的风格不符哦。
楼上代码很是漂亮
我要继续努力呀
改成尾递归形式。
复制代码
[ 本帖最后由 Magicloud 于 2009-3-26 09:24 编辑 ]
缩进小了,果然好看 !
你还是用空格吧。
别用 TAB 了。
我就用空格,两个空格。
Haskell 里面对齐和缩进都有,用 TAB 非常容易导致问题。
比 Python 都容易出问题。
vim 中设置 tab 是占四格的
没想到论坛中把 tab 转换成八个格了
这缩进……
我的经验是,Haskell 代码还是用两个空格的缩进吧。
最多不能超过四个空格。