# [racket] Delete Nth Term

3 versions, all pass the test suite. Now write the function
pick-one-of-three : [List X X X] -> X
and turn in.
#lang racket
(require rackunit)
;; Nat [Listof X] -> [Listof X]
;; delete the n-th item from l, if there is one
(module+
test
(check-equal? (deleteNth 4 '()) '())
(check-equal? (deleteNth 0 '(a b c)) '(b c))
(check-equal? (deleteNth 3 '(a b c)) '(a b c))
(check-equal? (deleteNth 2 '(a b c)) '(a b)) )
(define (deleteNth n l)
(cond
[(= n 0) (rest l)]
[(< n (length l)) (append (take l n) (rest (drop l n)))]
[else l])
#;
(cond
[(empty? l) l]
[(zero? n) (rest l)]
[else (cons (first l) (deleteNth (sub1 n) (rest l)))])
#;
(cond
[(and (zero? n) (empty? l)) l]
[(and (positive? n) (empty? l)) l]
[(and (zero? n) (cons? l)) (rest l)]
[(and (positive? n) (cons? l)) (cons (first l) (deleteNth (sub1 n) (rest l)))]))
On Oct 7, 2012, at 10:03 PM, Ashley Fowler wrote:
>* I need help with making a function that deletes the nth term from a list.(list LS with its Nth element (with indexes
*>* starting at 0) deleted)
*>* So far I have
*>*
*>* (define (deleteNth N ls)
*>* (if (null? ls)
*>* ls
*>* (deleteNth (- N 1) (cdr ls))))
*>*
*>* The results should be ...
*>*
*>* (deleteNth 4 '()) returns ()
*>* (deleteNth 0 '(a b c)) returns (b c)
*>* (deleteNth 3 '(a b c)) returns (a b c)
*>* ____________________
*>* Racket Users list:
*>* http://lists.racket-lang.org/users
*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121008/f55b4016/attachment-0001.html>