diff options
[FP] Slightly modified solutions to exercise 1 and 2 to use better constructs
1 files changed, 5 insertions, 5 deletions
| -rw-r--r-- | Year 2/Functional Programming/Exam2-2022.hs | 10 |
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 |
