aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Sucan <jan@jansucan.com>2023-03-11 17:19:48 +0100
committerJan Sucan <jan@jansucan.com>2023-03-11 17:19:48 +0100
commitbc92b3c77a9216e50b13bc5d210b39ec78fe4edc (patch)
tree370d0304d4b934c7af3f30bbdfa46e7db5b9a1f9
parent8d5a356c8ee0f6c4f40ee40fd2b89ca248cc92c3 (diff)
3_b_5: Add solution
-rw-r--r--README.md2
-rw-r--r--ch03/3_b_5.hs22
2 files changed, 23 insertions, 1 deletions
diff --git a/README.md b/README.md
index 4ffc8f3..9218b56 100644
--- a/README.md
+++ b/README.md
@@ -57,7 +57,7 @@ more visible in the list the first exercise of a group is in bold italics.
| 3_b_2 | yes, in 3_b_1 | | |
| 3_b_3 | yes | | |
| 3_b_4 | yes | | |
-| 3_b_5 | | | |
+| 3_b_5 | yes | | |
| 3_b_6 | | 70 | |
| 3_b_7 | | | |
| 3_b_8 | | | |
diff --git a/ch03/3_b_5.hs b/ch03/3_b_5.hs
new file mode 100644
index 0000000..5bc89aa
--- /dev/null
+++ b/ch03/3_b_5.hs
@@ -0,0 +1,22 @@
+-- Write a function that determines whether its input list is a palindrome.
+
+reverseList [] = []
+reverseList (x:xs) = (reverseList xs) ++ [x]
+
+isPalindrome xs = xs == (reverseList xs)
+
+-- ghci> :l 3_b_5.hs
+-- [1 of 1] Compiling Main ( 3_b_5.hs, interpreted )
+-- Ok, one module loaded.
+-- ghci> isPalindrome []
+-- True
+-- ghci> isPalindrome [1]
+-- True
+-- ghci> isPalindrome [1, 2]
+-- False
+-- ghci> isPalindrome [1, 2, 1]
+-- True
+-- ghci> isPalindrome [1, 2, 2, 1]
+-- True
+-- ghci> isPalindrome [1, 2, 3, 2, 4]
+-- False