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/StringBuffer.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 lec07/StringBuffer.hs (limited to 'lec07/StringBuffer.hs') diff --git a/lec07/StringBuffer.hs b/lec07/StringBuffer.hs new file mode 100644 index 0000000..138ef3f --- /dev/null +++ b/lec07/StringBuffer.hs @@ -0,0 +1,22 @@ +{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-} +module StringBuffer where + +import Data.Monoid + +import Buffer + +instance Buffer String where + toString = id + fromString = id + line n b = safeIndex n (lines b) + replaceLine n l b = unlines . uncurry replaceLine' . splitAt n . lines $ b + where replaceLine' pre [] = pre + replaceLine' pre (_:ls) = pre ++ l:ls + numLines = length . lines + value = length . words + +safeIndex :: Int -> [a] -> Maybe a +safeIndex n _ | n < 0 = Nothing +safeIndex _ [] = Nothing +safeIndex 0 (x:_) = Just x +safeIndex n (_:xs) = safeIndex (n-1) xs \ No newline at end of file -- cgit v1.2.3