From f46dafc461e4ee3fa1637051284f89b443f00a51 Mon Sep 17 00:00:00 2001 From: Edoardo La Greca Date: Tue, 9 Sep 2025 20:38:00 +0200 Subject: add files for lecture 7's homework --- lec07/Sized.hs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 lec07/Sized.hs (limited to 'lec07/Sized.hs') diff --git a/lec07/Sized.hs b/lec07/Sized.hs new file mode 100644 index 0000000..9214b76 --- /dev/null +++ b/lec07/Sized.hs @@ -0,0 +1,28 @@ +{-# LANGUAGE GeneralizedNewtypeDeriving, FlexibleInstances #-} +module Sized where + +import Data.Monoid + +newtype Size = Size Int + deriving (Eq, Ord, Show, Num) + +getSize :: Size -> Int +getSize (Size i) = i + +class Sized a where + size :: a -> Size + +instance Sized Size where + size = id + +-- This instance means that things like +-- (Foo, Size) +-- (Foo, (Bar, Size)) +-- ... +-- are all instances of Sized. +instance Sized b => Sized (a,b) where + size = size . snd + +instance Monoid Size where + mempty = Size 0 + mappend = (+) -- cgit v1.2.3