definition of take

Jorge Adriano Aires jadrian at mat.uc.pt
Wed Feb 16 08:13:12 EST 2005


Hi, 
I don't know much about the internals of GHC, but I like look around once in a 
while to learn a few things. I was wondering about the definition of take, 
which is, for Ints:

---------------------------------------------------------------------
takeUInt :: Int# -> [b] -> [b]
takeUInt n xs
  | n >=# 0#  =  take_unsafe_UInt n xs
  | otherwise =  []

take_unsafe_UInt :: Int# -> [b] -> [b]
take_unsafe_UInt 0#  _  = []
take_unsafe_UInt m   ls =
  case ls of
    []     -> []
    (x:xs) -> x : take_unsafe_UInt (m -# 1#) xs
---------------------------------------------------------------------

Wouldn't pattern matching against 1# instead of 0#, like in the following 
definition, be better in terms of garbage collecting the list or what it 
depends on? 

-----------------------------------------------------------------------
takeUInt :: Int# -> [b] -> [b]
takeUInt 0# _ =  []
takeUInt n xs
  | n ># 0#   =  take_unsafe_UInt n xs
  | otherwise =  []

take_unsafe_UInt :: Int# -> [b] -> [b]
take_unsafe_UInt 1#  (x:xs)  = [x]
take_unsafe_UInt m   ls      =
  case ls of
    []     -> []
    (x:xs) -> x : take_unsafe_UInt (m -# 1#) xs
------------------------------------------------------------------

My guess would be that, if we're consuming a (take n xs) lazily, with the 
first definition garbage collecting would happen when the last element had 
been consumed  and with the second, when the last element had been produced - 
(which is when we find out there are no more elements to consume in each 
case).

Thanks, and sorry if the question is to naive,
J.A.


More information about the Glasgow-haskell-users mailing list