《Learn You a Haskell for Great Good!》的原文摘录

  • 我们看到如何在函子值上映射“多参数”函数得到函数值的函子值。我们能对它做什么吗?对于一个参数,我们可以映射一个函数,它接受某个函数为参数,给这个函数应用一个参数。因为函子值里的任何东西都会被作为参数传递给我们的函数。 (查看原文)
    hshq 1赞 2014-08-08 15:35:44
    —— 引自第191页
  • _ means that we don't care what we'll draw from the list anyway so instead of writing a variable name that we'll never use, we just write _. (查看原文)
    我很严肃的 2011-07-04 14:46:57
    —— 引自章节:Starting Out
  • Use tuples when you know in advance how many components some piece of data should have. Tuples are much more rigid because each different size of tuple is its own type, so you can't write a general function to append an element to a tuple — you'd have to write a function for appending to a pair, one function for appending to a triple, one function for appending to a 4-tuple, etc. (查看原文)
    我很严肃的 2011-07-04 15:52:23
    —— 引自章节:Tuples
  • In our gcd' function, the logging is fast because the list appending ends up looking like this: a ++ (b ++ (c ++ (d ++ (e ++ f)))) Lists are a data structure that's constructed from left to right, and this is efficient because we first fully construct the left part of a list and only then add a longer list on the right. But if we're not careful, using the Writer monad can produce list appending that looks like this: ((((a ++ b) ++ c) ++ d) ++ e) ++ f This associates to the left instead of to the right. This is inefficient because every time it wants to add the right part to the left part, it has to construct the left part all the way from the beginning! (查看原文)
    gfreezy 2011-12-15 22:21:34
    —— 引自章节:for-a-few-monads-more
  • Function application has the highest precedence of all the operations in Haskell. In other words, these two statements are equivalent. (查看原文)
    羡辙 2012-02-25 11:09:07
    —— 引自第4页
  • This example introduces Haskell’s if statement. You’re probably already familiar with if statements from other languages, but what makes Haskell’s unique is that the else part is mandatory. On the other hand, a Haskell program is a collection of functions. Func- tions are used to transform data values into result values, and every function should return some value, which can in turn be used by another function. Since every function has to return something, this implies that every if has to have a corresponding else. Otherwise, you could write a function that has a return value when a certain condition is met but doesn’t have one when that condition isn’t met! Briefly: Haskell’s if is an expression that must return a value, and not a statement. (查看原文)
    羡辙 2012-02-25 11:23:06
    —— 引自第6页
  • Accessing List Elements If you want to get an element of a list by index, use the !! operator. As with most programming languages, the indices start at 0: (查看原文)
    羡辙 2012-02-25 11:57:00
    —— 引自第9页
  • multThree :: Int -> Int -> Int -> Int multThree x y z = x * y * z (查看原文)
    [已注销] 2012-08-10 17:40:27
    —— 引自第61页
  • multThree :: Int -> (Int -> (Int -> Int)) (查看原文)
    [已注销] 2012-08-10 17:40:27
    —— 引自第61页
  • Prelude> let mul3 x = x*x*x Prelude> :t mul3 mul3 :: Num a => a -> a Prelude> (mul3 2) 2 3 <interactive>:15:2: No instance for (Num (a0 -> a1 -> t0)) arising from a use of `mul3' Possible fix: add an instance declaration for (Num (a0 -> a1 -> t0)) In the expression: (mul3 2) 2 3 In an equation for `it': it = (mul3 2) 2 3 (查看原文)
    [已注销] 7回复 2012-08-10 18:39:12
    —— 引自第61页
  • Prelude> let iua = (`elem` ['A'..'Z']) Prelude> iua 'Z' True Prelude> iua 1 <interactive>:33:5: No instance for (Num Char) arising from the literal `1' Possible fix: add an instance declaration for (Num Char) In the first argument of `iua', namely `1' In the expression: iua 1 In an equation for `it': it = iua 1 (查看原文)
    [已注销] 2012-08-10 19:03:36
    —— 引自第62页
  • head' [] = error "No head for empty lists!" head' (x:_) = x (查看原文)
    [已注销] 2012-08-15 14:06:08
    —— 引自第48页
  • head' xs = case xs of [] -> error "No head for empty lists!" (x:_) -> x (查看原文)
    [已注销] 2012-08-15 14:06:08
    —— 引自第48页
  • replicate' n x | n <= 0 = [] | otherwise = x : replicate' (n-1) x (查看原文)
    [已注销] 2012-08-15 15:18:30
    —— 引自第54页
  • quicksort (x:xs) = let smallerOrEqual = [a | a <- xs, a <= x] larger = [a | a <- xs, a > x] in quicksort smallerOrEqual ++ [x] ++ quicksort larger (查看原文)
    [已注销] 2012-08-15 15:19:48
    —— 引自第57页
  • flip' f = g where g x y = f y x (查看原文)
    [已注销] 1回复 2012-08-15 17:12:46
    —— 引自第65页
  • ghci> takeWhile (/=' ') "elephants know how to party" "elephants" (查看原文)
    [已注销] 2012-08-15 17:39:23
    —— 引自第69页
  • ghci> sum (takeWhile (<10000) (filter odd (map (^2) [1..]))) 166650 (查看原文)
    [已注销] 2012-08-15 17:39:23
    —— 引自第69页
  • ghci> let listOfFuns = map (*) [0..] ghci> (listOfFuns !! 4) 5 20 (查看原文)
    [已注销] 2012-08-16 10:25:02
    —— 引自第71页
  • ghci> map (\(a,b) -> a + b) [(1,2),(3,5),(6,3),(2,6),(2,5)] [3,8,9,8,7] (查看原文)
    [已注销] 2012-08-16 10:29:00
    —— 引自第72页
<前页 1 2 3 4 5 6 7 8 后页>