显示标签为“函式编程”的博文。显示所有博文
显示标签为“函式编程”的博文。显示所有博文

2011-03-21

求同存异,爱才有爱

有篇论文《Functional Programming through Deep Time》:
Initially I asked my husband for programming help, but his
usual response was to suggest first refactoring my code – aiming
to make it more “beautiful”
– before considering the problem at
hand. While Haskell is an elegant language, I just need code that
works
, so this fundamental difference in approach meant it was
easier to work entirely independently.

哪里都一样,男人天性地、固执地对美的追求,常令女人无奈。

2009-04-17

函数复合

求不大1000被3或被5整除所有数的和,可以这样
sum [x|x<-[1..999], x `mod` 3 == 0 
      || x `mod` 5 == 0 ]
但也可以将(x `mod`)和(== 0)分别作为两个函数复合为一个新函数(== 0).(x `mod`)
sum [x|x<-[1..999], any( (== 0).(x `mod`) )[3, 5] ]

2008-11-17

素数序列

在这里看到这两段Haskell代码的讨论,其中递归定义的素数序列是相当的漂亮。

通过奇数序列求解的素数序列:
isprime n = not $ elem 0 $ map (\x -> mod n x) 
    $ takeWhile (\x -> x^2 <= n) (2:[3,5..])

primelist = [x | x <- 2:[3,5..], isprime x]
通过递归定义的素数序列:
isprime n = not $ elem 0 $ map (\x -> mod n x) 
    $ takeWhile(\x -> x^2 <= n) primelist

primelist = 2:[x | x <- [3,5..], isprime x]

2008-10-27

函式程序之美

偶然从这里看到一段Haskell程序,用来求解由任意两个三位数乘积组成的最大回文数(palindrome),摘录如下(该程序版权属原作者)。这段程序是相当的简洁、美丽绝伦。

euler4 = 
   maximum [a*b | a<-range, b<-range, palindrome (a*b)]

range :: [Int]
range = [100..999] -- three digits

palindrome :: Int -> Bool
palindrome n = 
    digits == (reverse digits)
       where digits = show n

2008-10-18

一个生动有趣的Haskell教程

推荐一个Haskell入门教程,Learn You a Haskell for Great Good!,它尽力令学习Haskell、理解函式编程(functional programming,也可译为泛函编程)的过程变得生动。