-
-
Save namjae/b17a76c36ec56eac3619db0ca8d7ec15 to your computer and use it in GitHub Desktop.
Some common operation on Scheme List structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define (delete-n list n) | |
| (if (= n 0) | |
| (cdr list) | |
| (cons (car list) (delete-n (cdr list) (- n 1))))) | |
| (define (insert-n list item n) | |
| (if (= n 0) | |
| (cons item list) | |
| (cons (car list) (insert-n (cdr list) item (- n 1))))) | |
| (define (list-nth list n) | |
| (if (= n 0) | |
| (car list) | |
| (list-nth (cdr list) (- n 1)))) | |
| (define (replace-nth list n item) | |
| (if (= n 0) | |
| (cons item (cdr list)) | |
| (cons (car list) (replace-nth (cdr list) (- n 1) item)))) | |
| (define (swap-list-item list m n) | |
| (let | |
| ((a (list-nth list m)) | |
| (b (list-nth list n))) | |
| (replace-nth | |
| (replace-nth list m b) n a))) | |
| (swap-list-item (list 1 2 3 4 5 6) 2 4) | |
| (replace-nth (list 3 2 9 2) 2 8) | |
| (delete-n (list 1 2 3 4 5) 2) | |
| (insert-n (list 1 2 3 4 5) 10 2) | |
| (list-nth (list 1 2 3 4 5) 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment