From 7b7bf727da3250907284368b98b0083a4b4e2386 Mon Sep 17 00:00:00 2001 From: mryouse Date: Sat, 3 Jun 2023 20:22:18 +0000 Subject: initial commit --- p99/p06.neb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 p99/p06.neb (limited to 'p99/p06.neb') 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))) -- cgit v1.2.3