From bc92b3c77a9216e50b13bc5d210b39ec78fe4edc Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Sat, 11 Mar 2023 17:19:48 +0100 Subject: 3_b_5: Add solution --- ch03/3_b_5.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 ch03/3_b_5.hs (limited to 'ch03/3_b_5.hs') 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 -- cgit v1.2.3