Singly-linked list/Element removal: Difference between revisions
Content added Content deleted
(→{{header|C sharp}}: Added C# (unsafe translation of C)) |
|||
Line 705: | Line 705: | ||
{} |
{} |
||
</pre> |
</pre> |
||
=={{header|Racket}}== |
|||
This is written entirely in terms of car and cdr (the linked list/pair primitives in Racket and Scheme). Usually, you'd have reverse and append available to you... but, again, it's interesting to see how they are implemented (by me, at least) |
|||
<lang racket>#lang racket/base |
|||
(define (rev l (acc null)) |
|||
(if (null? l) |
|||
acc |
|||
(rev (cdr l) (cons (car l) acc)))) |
|||
(define (++ l m) |
|||
(if (null? l) |
|||
m |
|||
(let recur ((l-rev (rev l)) (acc m)) |
|||
(if (null? l-rev) |
|||
acc |
|||
(recur (cdr l-rev) (cons (car l-rev) acc)))))) |
|||
(define (remove-at l i (acc null)) |
|||
(cond |
|||
[(null? l) (rev acc)] |
|||
[(positive? i) (remove-at (cdr l) (sub1 i) (cons (car l) acc))] |
|||
[else (++ (rev acc) (cdr l))])) |
|||
(displayln (remove-at '(1 2 3) 0)) |
|||
(displayln (remove-at '(1 2 3) 1)) |
|||
(displayln (remove-at '(1 2 3) 2)) |
|||
(displayln (remove-at '(1 2 3) 3))</lang> |
|||
{{out}} |
|||
<pre>(2 3) |
|||
(1 3) |
|||
(1 2) |
|||
(1 2 3)</pre> |
|||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |