RWH CH01 Notes

Haskell懒惰求值与练习解答
本文介绍Haskell中的懒惰求值概念,并通过示例展示其工作原理。此外,还提供了两个关于文件处理的练习题解答,分别演示了如何计算文件中的单词数及字符数。

 

Haskell is lazy evaluation!

 

 

Prelude> :show bindings 

Prelude> let x = 2010
Prelude> :show bindings 
x :: Integer = _
Prelude> x
2010
Prelude> :show bindings 
it :: Integer = 2010
x :: Integer = 2010
Prelude>

 

Exercises Answers:

 

3: The words function counts the number of words in a string. Modify the WC.hs example to count the number of words in a file.

 

 

main = interact wordCount
    where wordCount input = show (length (words input)) ++ "\n"

 

4: Modify the WC.hs example again, to print the number of characters in a file.

 

main = interact wordCount
    where wordCount input = show (length input) ++ "\n"
 
Useful command
:set +t
:unset +t
:m Data.Radio 
it
<dahua>dir all etc dir: argv[0] = 'all', argv[1] = 'etc' Directory of flash:/etc 0 -rw- 9449472 Jan 01 1970 00:00:53 CML_DB.db 1 -rw- 2442 Jan 01 1970 00:14:25 nos.conf 2 -rw- 2442 Jan 01 1970 00:43:25 nos.conf.back 3 drw- - Jan 01 1970 00:00:22 ssh 4 -rwh 2442 Jan 01 1970 00:02:38 .nos.conf 436148 KB total (340572 KB free) <dahua> <dahua>dir f 【cmlsh_completion】text = f [MATCHES] Called with text='f' [COMP] Entering: text='f', state=0, last_state=7 000000000000000 111111111111111111 [generate] Scanning directory: '/mnt/switch/' with prefix: '' 跳过软链接: main_uImage [generate] Found 9 matches. generate_current_dir_paths: matches = cfg/, ea/, etc/, ew/, home/, installers/, logfile/, 1.txt, ew.txt, Generated 9 matches 22222222222222222 flash_path = flash:/ [MATCHES] Returning single match: 'flash:/' lash:/ 【cmlsh_completion】text = flash:/ [MATCHES] Called with text='flash:/' [COMP] Entering: text='flash:/', state=0, last_state=-1 000000000000000 3333333333333333 444444444444444 flash_path = flash:/cfg/ [MATCHES] Returning single match: 'flash:/cfg/' cfg/ 【cmlsh_completion】text = flash:/cfg/ [MATCHES] Called with text='flash:/cfg/' [COMP] Entering: text='flash:/cfg/', state=0, last_state=0 000000000000000 3333333333333333 444444444444444 flash_path = flash:/ea/ [MATCHES] Returning single match: 'flash:/ea/' ea/ 【cmlsh_completion】text = flash:/ea/ [MATCHES] Called with text='flash:/ea/' [COMP] Entering: text='flash:/ea/', state=0, last_state=1 000000000000000 3333333333333333 444444444444444 flash_path = flash:/etc/ [MATCHES] Returning single match: 'flash:/etc/' tc/s 【cmlsh_completion】text = flash:/etc/s [MATCHES] Called with text='flash:/etc/s' [COMP] Entering: text='flash:/etc/s', state=0, last_state=2 000000000000000 3333333333333333 444444444444444 flash_path = flash:/ew/ [MATCHES] Returning single match: 'flash:/ew/' w/ 还有个另外的问题,就是比如根目录下有etc目录,然后etc目录下还有上边打印的一些文件,包括ssh子目录,当我dir f连续补全成dir flash:/etc/后,我在后边加个s字符,正常的逻辑是要遍历etc/ssh/下的文件了,但是tab后还是遍历根目录下的文件,问题就是没有父目录与子目录的无缝切换,在上边的基础上如何优化?但是不要改原来的逻辑
08-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值