praxis

Various programming exercises.
git clone git://git.jtobin.io/praxis.git
Log | Files | Refs

Length.hs (524B)


      1 {-# OPTIONS_GHC -Wall -fno-warn-type-defaults #-}
      2 {-# LANGUAGE BangPatterns #-}
      3 {-# LANGUAGE LambdaCase #-}
      4 
      5 import Prelude hiding (compare)
      6 
      7 len :: [a] -> Int
      8 len = loop 0 where
      9   loop !acc = \case
     10     []    -> acc
     11     (_:t) -> loop (succ acc) t
     12 
     13 compare :: [a] -> [b] -> Ordering
     14 compare = loop where
     15   loop l r = case (l, r) of
     16     (_ : _, []) -> GT
     17     ([], _ : _) -> LT
     18     ([], [])    -> EQ
     19     _           -> loop (drop 1 l) (drop 1 r)
     20 
     21 main :: IO ()
     22 main = do
     23   print (len [1..1000])
     24   print (compare [1..10] [1..])