他们究竟做了什么?我知道可能使用@(在模式匹配开始时指定一个名字),但是在〜上找不到任何东西.
我在下面的代码片段中找到了它们,取自http://www.haskell.org/haskellwiki/Prime_numbers,但是本文假设您能够熟练使用Haskell语法并且不打算解释其深奥的操作符(第一部分) '混淆是筛子宣言的开始):
primesPT () = 2 : primes'
where
primes' = sieve [3,5..] primes' 9
sieve (p:xs) ps@ ~(_:t) q
| p < q = p : sieve xs ps q
| True = sieve [x | x<-xs, rem x p /= 0] t (head t^2)
Run Code Online (Sandbox Code Playgroud)
关于这里使用的语法的任何解释(或链接到一个)将不胜感激.
为了准备即将推出的Google Code Jam,我开始研究一些问题.这是我尝试过的一个练习题:
http://code.google.com/codejam/contest/32016/dashboard#s=p0
这是我当前的Haskell解决方案的要点:
{-
- Problem URL: http://code.google.com/codejam/contest/32016/dashboard#s=p0
-
- solve takes as input a list of strings for a particular case
- and returns a string representation of its solution.
-}
import Data.List
solve :: [String] -> String
solve input = show $ minimum options
where (l1:l2:l3:_) = input
n = read l1 :: Int
v1 = parseVector l2 n
v2 = parseVector l3 n
pairs = [(a,b) | a <- permutations v1, b <- permutations v2] …Run Code Online (Sandbox Code Playgroud)