[Haskell-beginners] function defenition. Do I understand it right?

Benjamin Edwards edwards.benj at gmail.com
Tue Jul 12 14:06:22 CEST 2011


I am not sure where your error is coming from.

module Main
  where

halve :: [a] -> ([a],[a])
halve xs | null xs = ([],[])
         | otherwise = (take r xs, drop s xs)
 where (p,q) = divMod (length xs) 2
       (r,s) = if q == 0 then (p,p) else (p + 1,p)

works fine for me in ghci and is approximately what you wrote :) For
what it's worth your implementation has a bug in it, you only want to
take an extra item in the odd case.

Ben



More information about the Beginners mailing list