[racket] Delete Nth Term

From: Ashley Fowler (afowler2 at broncos.uncfsu.edu)
Date: Mon Oct 8 10:11:31 EDT 2012

Well this is what I got so far...

(define deleteNth
    (lambda(N LS)
      (null? LS) LS)
         ((= N 0)(cdr LS))
         (else(cons(car LS)(deleteNth(- N 1)(cdr LS))))))

and it keeps giving me the error of "cond: bad syntax (clause is not a test-value pair) in: ls"
From: Matthias Felleisen [matthias at ccs.neu.edu]
Sent: Monday, October 08, 2012 9:51 AM
To: Ashley Fowler
Cc: users at racket-lang.org
Subject: Re: [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

  (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)
    [(= n 0) (rest l)]
    [(< n (length l)) (append (take l n) (rest (drop l n)))]
    [else l])
    [(empty? l) l]
    [(zero? n) (rest l)]
    [else (cons (first l) (deleteNth (sub1 n) (rest l)))])
    [(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)
        (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:

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121008/f3c6cb3d/attachment.html>

Posted on the users mailing list.