Section 2.2

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 略

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章