Add CT331 Programming Paradigms
This commit is contained in:
@ -0,0 +1,22 @@
|
||||
#lang racket
|
||||
|
||||
;; a cons pair of two numbers
|
||||
(cons 1 2)
|
||||
|
||||
;; a list of 3 numbers using only the cons function
|
||||
;; this could be more easily done using the single quote `'` (i.e., `'(1 2 3)`) but i don't use it as it seemed against the spirit of the question
|
||||
(cons 1 (cons 2 (cons 3 empty)))
|
||||
|
||||
;; a list containing a string, a number, and a nested list of three numbers using only the cons function
|
||||
(cons "a string"
|
||||
(cons 0
|
||||
(cons (cons 1 (cons 2 (cons 3 empty))) empty)
|
||||
)
|
||||
)
|
||||
|
||||
;; a list containing a string, a number, and a nested list of three numbers, using only the list function
|
||||
(list "a string" 0 (list 1 2 3))
|
||||
|
||||
;; a list containing a string, a number, and a nested list of three numbers, using only the append function
|
||||
;; using `'` as the arguments of the `append` function must be themselves lists
|
||||
(append '("a string") '(0) '((1 2 3)))
|
@ -0,0 +1,93 @@
|
||||
#lang racket
|
||||
|
||||
(provide ins_beg)
|
||||
(provide ins_end)
|
||||
(provide count_top_level)
|
||||
(provide count_instances)
|
||||
(provide count_instances_tr)
|
||||
(provide count_instances_deep)
|
||||
|
||||
;; function to insert an element at the beginning of a list
|
||||
(define (ins_beg el lst)
|
||||
;; assuming that the second element is always a list
|
||||
(cons el lst)
|
||||
)
|
||||
|
||||
;; function to insert an element at the end of a list
|
||||
(define (ins_end el lst)
|
||||
;; making el into a list if it isn't already
|
||||
(append lst (cons el empty))
|
||||
)
|
||||
|
||||
;; function to count the number of top-level items in a list
|
||||
(define (count_top_level lst)
|
||||
(if (null? lst)
|
||||
0 ;; return 0 if we've reached the end of the list
|
||||
(+ 1 (count_top_level (cdr lst))) ;; return 1 plus the count_top_level of the second element of the cons pair (the rest of the list)
|
||||
)
|
||||
)
|
||||
|
||||
;; non-tail recursive function to count the number of times a given item occurs in a list (assuming items are atomic)
|
||||
(define (count_instances item lst)
|
||||
(if (null? lst)
|
||||
0 ;; return 0 if at the end of the list
|
||||
(+
|
||||
(if (equal? item (car lst))
|
||||
1 ;; if the item is equal to the first element of the list, add 1
|
||||
0 ;; if the item is not equal to the first element of the list, add 0
|
||||
)
|
||||
(count_instances item (cdr lst)) ;; recurse with the remainder of the list
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
;; helper function for count_instances_tr
|
||||
(define (count_instances_tr_helper item lst cnt)
|
||||
(cond
|
||||
;; return the count if the end of the list is reached (0 for empty list)
|
||||
((null? lst)
|
||||
cnt
|
||||
)
|
||||
;; if the first element of the list is equal to the item
|
||||
((eq? (car lst) item)
|
||||
;; recurse with the remainder of the list and an incremented count
|
||||
(count_instances_tr_helper item (cdr lst) (+ cnt 1))
|
||||
)
|
||||
;; if the first element of the list is not equal to the item
|
||||
(else
|
||||
;; recurse with the remainder of the list and an unchanged count
|
||||
(count_instances_tr_helper item (cdr lst) cnt)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
;; tail recursive function to count the number of times a given item occurs in a list (assuming items are atomic)
|
||||
(define (count_instances_tr item lst)
|
||||
;; calling helper function with the list and the count so far (0)
|
||||
(count_instances_tr_helper item lst 0)
|
||||
)
|
||||
|
||||
;; function to count the number of times an item occurs in a list and its sub-lists
|
||||
(define (count_instances_deep item lst)
|
||||
(cond
|
||||
;; return nothing if we've reached the end of the list
|
||||
((null? lst)
|
||||
0
|
||||
)
|
||||
|
||||
;; if the first item is a list, recurse through the first element and then the rest and return the sum of the two results
|
||||
((pair? (car lst))
|
||||
(+ (count_instances_deep item (car lst)) (count_instances_deep item (cdr lst)))
|
||||
)
|
||||
|
||||
;; if the first element is equal to the item, add 1 to the count and recurse with the rest of the list
|
||||
((eq? item (car lst)) ; If the first element is equal to the item, increment count
|
||||
(+ 1 (count_instances_deep item (cdr lst)))
|
||||
)
|
||||
|
||||
;; else if the first element is not equal to the item, recurse with the rest of the list
|
||||
(else
|
||||
(count_instances_deep item (cdr lst))
|
||||
)
|
||||
)
|
||||
)
|
@ -0,0 +1,145 @@
|
||||
#lang racket
|
||||
|
||||
;; function to display the contents of a binary search tree in sorted order
|
||||
(define (display_contents bst)
|
||||
(cond
|
||||
;; if the binary search tree is null, print an empty string (nothing)
|
||||
[(null? bst) (display "")]
|
||||
|
||||
;; if the binary search tree has nodes
|
||||
[else
|
||||
;; display the contents of the left sub-tree of the current node
|
||||
(display_contents (cadr bst))
|
||||
|
||||
;; display the current node
|
||||
(display (car bst))
|
||||
(newline)
|
||||
|
||||
;; display the contents of the right sub-tree of the current node
|
||||
(display_contents (caddr bst))
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
;; function to search a tree and tell whether a given item is presesnt in a given tree
|
||||
(define (search_tree item bst)
|
||||
(cond
|
||||
;; return false if we've reached the end of the tree without finding a match
|
||||
((null? bst) #f)
|
||||
|
||||
;; return true if the current node is equal to the item
|
||||
((equal? item (car bst)) #t)
|
||||
|
||||
;; else return whether the item was found in the left sub-tree or the right sub-tree
|
||||
(else
|
||||
(or
|
||||
(search_tree item (cadr bst)) ;; search left sub-tree
|
||||
(search_tree item (caddr bst)) ;; search right sub-tree
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
;; function to insert an item into a binary search tree
|
||||
(define (insert_item item bst)
|
||||
(cond
|
||||
;; if there are no nodes in the tree, create a new tree with the item as the root
|
||||
((null? bst)
|
||||
(list item '() '())
|
||||
)
|
||||
|
||||
;; if the item is less than the current node, insert it into the left-hand side of the tree
|
||||
((< item (car bst))
|
||||
;; create new bst with same root node, same right-hand side, but a left-hand side that has had the item inserted
|
||||
(list (car bst) (insert_item item (cadr bst)) (caddr bst))
|
||||
)
|
||||
|
||||
;; if the item is greater than the current node, insert it into the right-hand side of the tree
|
||||
((> item (car bst))
|
||||
;; create new bst with same root node, same left-hand side, but a right-hand side that has had the item inserted
|
||||
(list (car bst) (cadr bst) (insert_item item (caddr bst)))
|
||||
)
|
||||
|
||||
;; else the item is equal to the current node, so do nothing
|
||||
(else bst)
|
||||
)
|
||||
)
|
||||
|
||||
;; function to insert a list of items into a binary search tree
|
||||
(define (insert_list lst bst)
|
||||
(if (null? lst)
|
||||
;; if the list is null, just return the bst with no changes
|
||||
bst
|
||||
|
||||
;; otherwise, recurse with the remainder of the list and the binary tree produced by inserting the first item of the list into bst
|
||||
(insert_list (cdr lst) (insert_item (car lst) bst))
|
||||
)
|
||||
)
|
||||
|
||||
;; tree-sort function
|
||||
(define (tree_sort lst)
|
||||
;; inserting the list into a tree structure to sort it and then displaying the contents of that tree
|
||||
(display_contents (insert_list lst '()))
|
||||
)
|
||||
|
||||
;; function to insert an item into a binary search tree based off a sorting function
|
||||
;; the sorting function should return accept two items and arguments, and return true if they were passed in order, and false otherwise or if they are equal
|
||||
(define (insert_item_custom item bst sorter)
|
||||
(cond
|
||||
;; if there are no nodes in the tree, create a new tree with the item as the root
|
||||
((null? bst)
|
||||
(list item '() '())
|
||||
)
|
||||
|
||||
;; if the item is goes before the current node, insert it into the left-hand side of the tree
|
||||
((sorter item (car bst))
|
||||
;; create new bst with same root node, same right-hand side, but a left-hand side that has had the item inserted
|
||||
(list (car bst) (insert_item_custom item (cadr bst) sorter) (caddr bst))
|
||||
)
|
||||
|
||||
;; if the item goes after the current node, insert it into the right-hand side of the tree
|
||||
((sorter (car bst) item)
|
||||
;; create new bst with same root node, same left-hand side, but a right-hand side that has had the item inserted
|
||||
(list (car bst) (cadr bst) (insert_item_custom item (caddr bst) sorter))
|
||||
)
|
||||
|
||||
;; else the item is equal to the current node, so do nothing
|
||||
(else bst)
|
||||
)
|
||||
)
|
||||
|
||||
;; sorter function which states whether the two arguments were supplied in strictly ascending order (i.e., if item == item2, return false)
|
||||
(define (sort_ascending item1 item2)
|
||||
(if (< item1 item2)
|
||||
#t
|
||||
#f
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
;; sorter function which states whether the two arguments were supplied in strictly descending order (i.e., if item == item2, return false)
|
||||
(define (sort_descending item1 item2)
|
||||
(if (> item1 item2)
|
||||
#t
|
||||
#f
|
||||
)
|
||||
)
|
||||
|
||||
;; sorter function which states whether the two arguments were supplied in strictly ascending order based on the final digit (i.e., if item == item2, return false)
|
||||
(define (sort_ascending_last item1 item2)
|
||||
(if (< (modulo item1 10) (modulo item2 10))
|
||||
#t
|
||||
#f
|
||||
)
|
||||
)
|
||||
|
||||
;; function to insert a list of items into a binary search tree in the order determined by a sorting function
|
||||
(define (insert_list_custom lst bst sorter)
|
||||
(if (null? lst)
|
||||
;; if the list is null, just return the bst with no changes
|
||||
bst
|
||||
|
||||
;; otherwise, recurse with the remainder of the list and the binary tree produced by inserting the first item of the list into bst
|
||||
(insert_list_custom (cdr lst) (insert_item_custom (car lst) bst sorter) sorter)
|
||||
)
|
||||
)
|
Reference in New Issue
Block a user