aboutsummaryrefslogtreecommitdiff
path: root/p99/p06.neb
diff options
context:
space:
mode:
Diffstat (limited to 'p99/p06.neb')
-rw-r--r--p99/p06.neb23
1 files changed, 23 insertions, 0 deletions
diff --git a/p99/p06.neb b/p99/p06.neb
new file mode 100644
index 0000000..f219058
--- /dev/null
+++ b/p99/p06.neb
@@ -0,0 +1,23 @@
+; P06 Find out whether a list is a palindrome.
+; A palindrome can be read forward or backward; e.g. (x a m a x).
+
+(def odd-good (list "x" "a" "m" "a" "x")) ; odd number of elements (good)
+(def odd-bad (list "x" "f" "m" "a" "x")) ; odd number of elements (bad)
+(def even-good (list "a" "b" "b" "a")) ; even number of elements (good)
+(def even-bad (list "a" "b" "b" "f")) ; even number of elements (bad)
+(def single (list "a")) ; single element
+(def empty (list))
+
+(func palindrome? (lst) ; by convention, funcs returning :bool end with a ?
+ (if (< (length lst) 2) ; 0 or 1 element lists are palindromic
+ #true
+ (and
+ (eq? (first lst) (last lst)) ; first element and last element are equal
+ (palindrome? (most (rest lst)))))) ; this would be clearer if slices supported negative numbers
+
+(print (->string (palindrome? odd-good)))
+(print (->string (palindrome? odd-bad)))
+(print (->string (palindrome? even-good)))
+(print (->string (palindrome? even-bad)))
+(print (->string (palindrome? single)))
+(print (->string (palindrome? empty)))