找回密码
 欢迎注册
查看: 75951|回复: 51

[分享] Haskell自我娱乐帖

[复制链接]
发表于 2008-5-9 23:44:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?欢迎注册

×
来自网友教导 均为论坛上某些问题的部分算法的haskell的实现问题 来自QQ群热心网友的指点 放在这里以做记录 ===================================== 几个链接 函数和例子查询http://www.zvon.org/other/haskell/Outputglobal/index.html Hoogle http://www.haskell.org/hoogle/
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-5-9 23:53:40 | 显示全部楼层
base=[1,3,7,9] 生成n位数字,所有数字来自base let base=[1,3,7,9] let foo _ [] = []; foo 0, _ = []; foo 1 x = [x]; foo n x = [a * 10 + b | a <- foo (n-1) x, b <- x] 假设生成8位的 foo 8 base ======================================================= 由一维列表生成二维的 let base=[1..4] 不排除相等的 let base2=[ (x, y) | x <- base, y<- base ] (1, 1)也在里面 排除相等的 let base2=[ (x, y) | x <- base, y<- base, x /= y ] (1, 1)类的排除 排除相等且对称的,即(1, 2) (2, 1)只要一个 let inner x [ ] = [ ]; inner x (y:ys) = (x, y):inner x ys; let outer [ ] = [ ]; outer (x:xs) = inner x xs ++ outer xs; 然后 outer base 或者 let a = [1..4] let b [] = []; b (x:xs) = map ((,) x) xs ++ b xs; b a 显示 [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)] 另外一种情况 将[1, 2, 3, 4]连接成[...(x, y)...] 1 <= x<= y <= 4的 let a = [1..4] let b [] = []; b (xs@(x:xe)) = map ((,) x) xs ++ b xe; b a 显示 [(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),(3,4),(4,4)]
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-5-10 22:58:58 | 显示全部楼层
刚才无聊 去生成8位的全由1379组成的素数 结果发现haskell的惰性计算比较好玩 得到结果,很快的 但如果显示结果,就要计算了 中间我给Cancel掉了 但我重新启动显示结果 则刚才Cancel之前的很快滚动过去,证明是早已计算了 然后从上次取消的位置开始又重新变慢,是在继续计算了 共得到9177个结果 得到结果的时间确实要比C代码慢些 但如果算上写程序和调试 则比C快很多了 哈
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-5-11 19:15:52 | 显示全部楼层
等幂和问题7次幂一个例子 左面 [1,4,6,7,10,11,13,16,18,19,21,24,25,28,30,31,34,35,37,40,41,44,46,47,49,52,54,55 ,58,59,61,64,66,67,69,72,73,76,78,79,81,84,86,87,90,91,93,96,97,100,102,103,106, 107,109,112,114,115,117,120,121,124,126,127,130,131,133,136,137,140,142,143,145, 148,150,151,154,155,157,160,161,164,166,167,170,171,173,176,178,179,181,184,185, 188,190,191,193,196,198,199,202,203,205,208,210,211,213,216,217,220,222,223,226, 227,229,232,233,236,238,239,241,244,246,247,250,251,253,256] 右面 [2,3,5,8,9,12,14,15,17,20,22,23,26,27,29,32,33,36,38,39,42,43,45,48,50,51,53,56, 57,60,62,63,65,68,70,71,74,75,77,80,82,83,85,88,89,92,94,95,98,99,101,104,105,10 8,110,111,113,116,118,119,122,123,125,128,129,132,134,135,138,139,141,144,146,14 7,149,152,153,156,158,159,162,163,165,168,169,172,174,175,177,180,182,183,186,18 7,189,192,194,195,197,200,201,204,206,207,209,212,214,215,218,219,221,224,225,22 8,230,231,234,235,237,240,242,243,245,248,249,252,254,255] 方幂和 1171017999358189568 程序 let l 1 = [1,4];l n = l (n-1) ++ map (+2^n) (r (n-1)) let r 1 = [2,3];r n = r (n-1) ++ map (+2^n) (l (n-1)) l 7 r 7 方幂和 sum (map (^7) (l 7)) sum (map (^7) (r 7))
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-5-12 07:41:40 | 显示全部楼层
程序确实够简洁, 估计语法规则也很多吧,恐一时半会难掌握。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-5-12 08:48:55 | 显示全部楼层
语法属于那种小规则,大模式的语言 该语言源代码除了部分核心是C/C++写的 大部分都是用自身语言写的 另外,你看到的都是函数 而不是语法
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-5-12 09:30:53 | 显示全部楼层
GxQ5次48个数字等幂和验证 Prelude> let left = [1, 2, 7, 10, 11, 12, 13, 14, 16, 17, 21, 22, 27, 28, 32, 33 , 35, 36, 37, 38, 39, 42, 47, 48] Prelude> let right = [3,4,5,6,8,9,15,18,19,20,23,24,25,26,29,30,31,34,40,41,43,4 4,45,46] Prelude> length left 24 Prelude> length right 24 Prelude> sum (map (^5) left) 1084022688 Prelude> sum (map (^5) right) 1084022688 Prelude>
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-5-12 14:42:33 | 显示全部楼层
一个函数 对List求固定数量元素的List let selectDistinct 0 _ = [[]];selectDistinct _ [] = [];selectDistinct n xs = [ y ++ [x] | (xss,x:_) <- (drop (n-1) . init) (zip (inits xs) (tails xs)), y <- selectDistinct (n-1) xss ] let test = [1,2,3,4] selectDistinct 2 test [[1,2],[1,3],[2,3],[1,4],[2,4],[3,4]]
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-5-12 20:03:30 | 显示全部楼层
Prelude List> sum (map (^3) n16) 18496 Prelude List> div 18496 2 9248 Prelude List> let f1 x = (sum (map (^3) x)) == 9248 Prelude List> filter f1 (selectDistinct 8 n16) [[2,3,5,8,9,12,14,15],[1,4,6,7,10,11,13,16]] Prelude List> n16 [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] 证明前16正整数的3次幂划分仅有一个 因为sum (map (^3) n14)=11025说明不存在1-14的3次幂划分
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-5-12 20:07:52 | 显示全部楼层
[[4,5,7,9,12,13,14,15,17,18],[2,6,9,10,11,12,13,16,17,18],[2,4,5,7,12,13,14,16,1 7,18],[1,3,7,9,11,12,14,15,17,19],[2,4,6,7,11,13,14,15,17,19],[2,4,5,9,11,12,13, 16,17,19],[2,3,7,9,10,11,14,16,17,19],[1,5,7,8,9,12,14,16,17,19],[1,2,3,7,11,12, 14,16,17,19],[1,2,5,6,9,12,15,16,17,19],[3,4,5,8,11,12,13,15,18,19],[2,3,5,6,8,1 0,15,16,18,19],[1,4,5,6,8,11,13,17,18,19],[2,3,7,9,10,12,14,15,16,20],[1,4,7,9,1 1,12,13,14,17,20],[1,2,6,7,9,10,14,16,17,20],[3,4,7,8,10,11,13,14,18,20],[4,5,6, 8,9,10,13,15,18,20],[2,3,4,6,10,11,13,15,18,20],[1,4,5,6,8,12,13,15,18,20],[1,3, 6,7,8,10,14,15,18,20],[1,3,5,8,9,10,12,16,18,20],[2,4,5,6,8,10,13,16,18,20],[1,3 ,6,8,9,10,11,15,19,20],[1,3,4,5,7,8,14,15,19,20],[1,2,3,6,8,10,11,16,19,20]] 同样道理求得前20的3次幂划分是13组
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

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

GMT+8, 2024-11-21 17:53 , Processed in 0.032637 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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