aboutsummaryrefslogtreecommitdiff
path: root/p99/p18.neb
diff options
context:
space:
mode:
Diffstat (limited to 'p99/p18.neb')
-rw-r--r--p99/p18.neb27
1 files changed, 27 insertions, 0 deletions
diff --git a/p99/p18.neb b/p99/p18.neb
new file mode 100644
index 0000000..7ccf0b7
--- /dev/null
+++ b/p99/p18.neb
@@ -0,0 +1,27 @@
+; P18 Extract a slice from a list.
+; Given two indices, I and K, the slice is the list containing the elements between the
+; I'th and K'th element of the original list (both limits included). Start counting the elements with 1.
+
+(def a (list "a" "b" "c" "d" "e" "f" "g" "h" "i" "k"))
+
+(func my-slice (lst idx1 idx2)
+ (my-slice lst idx1 idx2 (list)))
+
+(func my-slice (lst :nil idx1 idx2 acc) acc)
+
+(func my-slice (lst :{:any} idx1 idx2 acc)
+ (my-slice
+ (rest lst)
+ (-- idx1)
+ (-- idx2)
+ (if
+ (or
+ (> idx1 1)
+ (< idx2 1))
+ acc
+ (append acc (first lst)))))
+
+(func -- (num) (- num 1))
+
+(print (->string a))
+(print (->string (my-slice a 3 7)))