#lang typed/scheme
(provide (all-defined-out))

;; List Utilities

;; Remove the first element satisfying f (if any).
;; (X -> Boolean) [Listof X] -> [Listof X]
(: remf (All (X) ((X -> Boolean) [Listof X] -> [Listof X])))
(define (remf f ls)
  (cond [(empty? ls) empty]
        [(f (first ls)) (rest ls)]
         (cons (first ls)
               (remf f (rest ls)))]))

;; X [Listof X] -> [Listof X]
;; Interleave x between each pair of elements in ys.
(: interleave (All (X) (X [Listof X] -> [Listof X])))
(define (interleave x ys)
  (cond [(empty? ys) ys]
        [(empty? (rest ys)) (list (first ys))]
         (cons (first ys)
               (cons x
                     (interleave x (rest ys))))]))

;; [Listof X] X Index -> [Listof X]
(: replace-at (All (X) ([Listof X] X Integer -> [Listof X])))
(define (replace-at ls x i)
  (append (take ls i)
          (list x)
          (drop ls (add1 i))))

;; [Listof X] Index -> [Listof X]
(: remove-at (All (X) ([Listof X] Integer -> [Listof X])))
(define (remove-at ls i)
  (append (take ls i)
          (drop ls (add1 i))))