aboutsummaryrefslogtreecommitdiff
path: root/p99/p18.neb
blob: 7ccf0b731f97dc9f673cc6e5e7d9e4d322852af9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)))