2.17
{- Prelude.last
-- | Extract the last element of a list, which must be finite and non-empty.
last :: [a] -> a
#ifdef USE_REPORT_PRELUDE
last [x] = x
last (_:xs) = last xs
last [] = errorEmptyList "last"
#else
-- eliminate repeated cases
last [] = errorEmptyList "last"
last (x:xs) = last' x xs
where last' y [] = y
last' _ (y:ys) = last' y ys
#endif
-}
-- Because it assure you a nonempty list
lastPair [x] = x
lastPair (x:xs) = lastPair xs
2.18
{- Prelude.reverse
-- | 'reverse' @xs@ returns the elements of @xs@ in reverse order.
-- @xs@ must be finite.
reverse :: [a] -> [a]
#ifdef USE_REPORT_PRELUDE
reverse = foldl (flip (:)) []
#else
reverse l = rev l []
where
rev [] a = a
rev (x:xs) a = rev xs (x:a)
#endif
-}
reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x]
2.19
cc amount coinValues
| amount == 0 = 1
| amount < 0 = 0
| coinValues == [] = 0
| otherwise = (cc amount (tail coinValues)) + (cc (amount - (head coinVa
lues)) coinValues)
2.20
不知道怎么定义任意个参数的函数。。
2.21
squareList = map (\x -> x*x)
2.22略
2.23
forEach = mapM_
2.24 ……
2.25 好像不是很好操作,Haskell里面List不能这么写,Tuple应该用模式匹配。
2.26 略
2.27
deepReverse xs = reverse $ map reverse xs
2.28 concat
2.29 略
分享到:
相关推荐
Section 2.2. Program Input and the raw_input()Built-in Function Section 2.3. Comments Section 2.4. Operators Section 2.5. Variables and Assignment Section 2.6. Numbers Section 2.7. Strings Section 2.8...
Introduction, Section 2.2. Problems, Section 2.3. Models of Computation, Chapter 3. Algorithms, Section 3.1. Introduction, Section 3.2. Anatomy of an Algorithm, Section 3.3. Introducing Big O ...
Section 2.2. Guided Transmission Media Section 2.3. Wireless Transmission Section 2.4. Communication Satellites Section 2.5. The Public Switched Telephone Network Section 2.6. The Mobile ...
Section 2.2. Guided Transmission Media Section 2.3. Wireless Transmission Section 2.4. Communication Satellites Section 2.5. The Public Switched Telephone Network Section 2.6. The Mobile ...
Section 2.2. Guided Transmission Media Section 2.3. Wireless Transmission Section 2.4. Communication Satellites Section 2.5. The Public Switched Telephone Network Section 2.6. The Mobile Telephone...
Section 2.2. Guided Transmission Media Section 2.3. Wireless Transmission Section 2.4. Communication Satellites Section 2.5. The Public Switched Telephone Network Section 2.6. The Mobile ...
Section 2.2. Guided Transmission Media Section 2.3. Wireless Transmission Section 2.4. Communication Satellites Section 2.5. The Public Switched Telephone Network Section 2.6. The Mobile ...
Section 2.2. Guided Transmission Media Section 2.3. Wireless Transmission Section 2.4. Communication Satellites Section 2.5. The Public Switched Telephone Network Section 2.6. The Mobile ...
Section 2.2. Configuration and Hardening Section 2.3. Changing Web Server Identity Section 2.4. Putting Apache in Jail Chapter 3. PHP Section 3.1. Installation Section 3.2. Configuration ...
Section 2.2. Guided Transmission Media Section 2.3. Wireless Transmission Section 2.4. Communication Satellites Section 2.5. The Public Switched Telephone Network Section 2.6. The Mobile ...
# Exercises for Section 2.2 ### 2.2.1 Consider the context-free grammar: S -> S S + | S S * | a 1. Show how the string `aa+a*` can be generated by this grammar. 2. Construct a parse tree for this ...
Section 2.2. Names Section 2.3. Numbers Section 2.4. Strings Section 2.5. Statements Section 2.6. Expressions Section 2.7. Literals Section 2.8. Functions Chapter 3. Objects Section 3.1. ...
Section 2.2. Anatomy of an Embedded System Section 2.3. Storage Considerations Section 2.4. Embedded Linux Distributions Section 2.5. Chapter Summary Chapter 3. Processor Basics Section 3.1. Stand-...
Section 2.2. The Layers of a Network Section 2.3. IP, TCP, and UDP Section 2.4. The Internet Section 2.5. The Client/Server Model Section 2.6. Internet Standards Chapter 3. Basic Web ...
Section 2.2. IP Datagram Header Section 2.3. Routing Section 2.4. Multicast Section 2.5. Summary Chapter 3. Transmission Control Protocol Section 3.1. Segment Header Section 3.2. ...