Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitb4a0a8843c9fcde60ffdce36a474e08ebc7dcd6a (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-01-14 10:36:03 +0200
Committer
 Syndamia <kamen@syndamia.com>
2023-01-14 10:36:03 +0200
Files92f9dd2c3c9e4111602d2d82eda92a0ebcbafd88
uni-projects-b4a0a8843c9fcde60ffdce36a474e08ebc7dcd6a.tar
uni-projects-b4a0a8843c9fcde60ffdce36a474e08ebc7dcd6a.tar.gz
uni-projects-b4a0a8843c9fcde60ffdce36a474e08ebc7dcd6a.zip
Parent86851d64223bc11723e3f86accb786219814a71f (diff)
[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.hs10
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