praxis

Various programming exercises.
Log | Files | Refs

commit f20599606d97c531d260ce866980975d358cecb8
parent d6a1d6c2114f5a076ddcc828de869806bb765d65
Author: Jared Tobin <jared@jtobin.ca>
Date:   Mon,  2 Jul 2018 11:09:50 +1200

Add length exercise.

Diffstat:
A20180629_length/Length.hs | 23+++++++++++++++++++++++
1 file changed, 23 insertions(+), 0 deletions(-)

diff --git a/20180629_length/Length.hs b/20180629_length/Length.hs @@ -0,0 +1,23 @@ +{-# LANGUAGE BangPatterns #-} +{-# LANGUAGE LambdaCase #-} + +import Prelude hiding (compare) + +len :: [a] -> Int +len = loop 0 where + loop !acc = \case + [] -> acc + (h:t) -> loop (succ acc) t + +compare :: [a] -> [b] -> Ordering +compare = loop where + loop l r = case (l, r) of + (h : _, []) -> GT + ([], h : _) -> LT + ([], []) -> EQ + _ -> loop (drop 1 l) (drop 1 r) + +main :: IO () +main = do + print (len [1..1000]) + print (compare [1..10] [1..])