找回密码
 欢迎注册
楼主: 无心人

[分享] Haskell自我娱乐帖

[复制链接]
 楼主| 发表于 2008-6-20 16:46:11 | 显示全部楼层
7, 11
结论:
养马人不在黄房子
let filter7_11 (x1, x2, x3, x4, x5, x6) = not ((x4 == 'H') && (x2 == 'Y'))

let l14 = filter filter7_11 l13
let l24 = filter filter7_11 l23
let l34 = filter filter7_11 l33
let l44 = filter filter7_11 l43
let l54 = filter filter7_11 l53

没削去任何条目
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-6-20 19:00:03 | 显示全部楼层
看了那个hakell解题的过程
不得不暂时停止了

有点超出自己现在学的了

呵呵
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-10-9 22:06:50 | 显示全部楼层
ctnt <- readFile "./abc.txt"
let l = read . concat . lines \$ ctnt :: [Integer]
print l

读取文件,生成list
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-10-10 20:16:48 | 显示全部楼层
写一个List
假设 a是list
writeFile fileName (show a)
fileName类似于"d:\\ghc\\demo.txt"形式
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-10-10 20:21:35 | 显示全部楼层
appendFile
File: /tmp/foo.txt :
AAA

Program source:

main = appendFile "/tmp/foo.txt" aaa
aaa  = "BBB" ++ "CCC"
File: /tmp/foo.txt :
AAABBBCCC
=====================
readFile
////demo 1
File: /tmp/foo :
BBBCCC

Program source:

main = do x <- readFile "/tmp/foo.txt"
          putStr x

Output: BBBCCC
////Demo2
File: /tmp/foo.txt :
[1,2,4,6,7]

Program source:

main = do x <- readFile "/tmp/foo.txt"
          y <- rList x
          print (sum y)

rList :: String -> IO [Int]          
rList = readIO

Output: 20
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-10-10 21:36:45 | 显示全部楼层
在交互里使用
let rList :: String -> IO [Int]; rList = readIO
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-10-11 12:35:18 | 显示全部楼层
呵呵
真的是自我娱乐啊
诗人常自我娱乐么?
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-10-11 13:40:15 | 显示全部楼层
呵呵
遇到新东西就记录在这里
Blog那东西不知道什么时候就会玩消失
论坛还有数据备份呢
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-11-13 15:26:59 | 显示全部楼层
两个 Int 相除,得到一个 Double 的结果

divInt2Double :: Int -> Int -> Double
divInt2Double a b = (fromIntegral a) / (fromIntegral b)
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-11-21 19:45:20 | 显示全部楼层
代码块写法

  if C then do {...} else do {...}
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-3-29 14:12 , Processed in 0.041414 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表