[racket] Delete Nth Term

From: Kieron Hardy (kieron.hardy at gmail.com)
Date: Mon Oct 8 11:24:46 EDT 2012

As the error message states, you have a problem with the cond, in particular with the parens.

I like to use brackets to denote the cond and actions like this:

(cond
  [(condition1) value1]
  [(condition2) (action2)]
  [else (else-action)]
  )

Hope this helps,

Kieron

On Oct 8, 2012, at 8:11, Ashley Fowler <afowler2 at broncos.uncfsu.edu> wrote:

> Well this is what I got so far...
> 
> (define deleteNth
>     (lambda(N LS)
>     (cond
>       (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 
> 
> (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
> 
> ____________________
>  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/139a2385/attachment.html>

Posted on the users mailing list.