From b4a0a8843c9fcde60ffdce36a474e08ebc7dcd6a Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 14 Jan 2023 10:36:03 +0200 Subject: [FP] Slightly modified solutions to exercise 1 and 2 to use better constructs --- Year 2/Functional Programming/Exam2-2022.hs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'Year 2/Functional Programming') diff --git a/Year 2/Functional Programming/Exam2-2022.hs b/Year 2/Functional Programming/Exam2-2022.hs index 5f18783..a89addd 100644 --- a/Year 2/Functional Programming/Exam2-2022.hs +++ b/Year 2/Functional Programming/Exam2-2022.hs @@ -4,7 +4,7 @@ import Data.List -- Exercise 1 squareDigits :: Int -> Int -squareDigits n = read (foldr (++) "" (map (\c -> if not (c == '-') then show ((digitToInt c)^2) else c : "") (show n))) +squareDigits n = read (concat (map (\c -> if not (c == '-') then show ((digitToInt c)^2) else c : "") (show n))) :: Int -- Exercise 2 @@ -21,10 +21,10 @@ matching :: String -> [(Int, Int)] matching s = calc s 0 [] where calc [] _ _ = [] - calc s i stack - | head s == '[' = calc (tail s) (i + 1) (i : stack) - | head s == ']' = (head stack, i) : (calc (tail s) (i + 1) (tail stack)) - | otherwise = calc (tail s) (i + 1) stack + calc s i stack = case head s of + '[' -> calc (tail s) (i + 1) (i : stack) + ']' -> (head stack, i) : (calc (tail s) (i + 1) (tail stack)) + otherwise -> calc (tail s) (i + 1) stack -- Exercise 4 -- cgit v1.3.1-23-gd82d