Skip to content

Instantly share code, notes, and snippets.

@vdeemann
Last active March 26, 2024 08:30
Show Gist options
  • Save vdeemann/3b9d4faf56cfeb3408f3f963962dec49 to your computer and use it in GitHub Desktop.
Save vdeemann/3b9d4faf56cfeb3408f3f963962dec49 to your computer and use it in GitHub Desktop.
#lang racket
(require racket/trace)
;(*) Find the last box of a list.
; my recursive length of a list
(define (len lst)
(if (eq? lst null)
0
(+ 1 (len(cdr lst)))
))
;(trace len)
;(len (list 1 2 3 4 5 6 7 8 9 11))
#|
# length_lst python while loop using ChatGPT to be converted tail-recursively
def length_lst(lst):
length = 0
while lst:
length = length + 1
lst = lst[1:]
return length
print(length_lst([0, 1, 2, 3, 4, 5]))
|#
; my tail-recursive length of a list
(define (tail-len lst)
(define (tail-len-helper lst length)
(cond
((null? lst) length)
(else (tail-len-helper (cdr lst) (+ length 1)))))
(tail-len-helper lst 0))
;(trace tail-len)
;(tail-len (list 1 2 3 4 5 6 7 8 9 11))
; my 1st solution
(define (last-of-the-list lst)
(if (not (= (len lst) 1))
(last-of-the-list (cdr lst))
(car lst)))
;(last-of-the-list (list 1 2 3 4 5 6 7 8 9 11))
; my 2nd solution
(define find-last
(lambda (lat)
(cond
((null? lat) '())
((eq? (tail-len lat) 1) car lat)
(else
(find-last(cdr lat))))))
;(trace find-last)
;(find-last '(a b c))
#|
# find_last python while loop using ChatGPT to be converted tail-recursively
def find_last(lat):
last = None
while lat:
last = lat[0]
lat = lat[1:]
return last
print(find_last([1, 2, 3, 4, 5])) # Output: 5
print(find_last([])) # Output: None (empty list)
print(find_last([1])) # Output: 1 (single-element list)
|#
; tail-recursive solution asking ChatGPT
(define (tail-find-last lat)
(define (find-last-helper lat last)
(cond
((null? lat) last)
(else (find-last-helper (cdr lat) (car lat)))))
(find-last-helper lat '()))
(trace tail-find-last)
(tail-find-last '(a b c))
#lang racket
(require racket/trace)
;(*) Find the last but one box of a list.
; my solution
(define (last-but-one-of-the-list lst)
(if (not (= (length lst) 2))
(last-but-one-of-the-list (cdr lst))
(car lst)))
;(trace last-but-one-of-the-list)
;(last-but-one-of-the-list '(a b c d e))
;(last-but-one-of-the-list '(a b c d e f g h i))
;; incorrect output
;;(last-but-one-of-the-list '(a))
;;(last-but-one-of-the-list '())
; solution from site
(define (last-but-one l)
(when (not (null? (cdr l)))
(if (null? (cddr l))
(car l)
(last-but-one (cdr l)))))
;(trace last-but-one)
;(last-but-one '(a b c d e))
;(last-but-one '(a b c d e f g h i))
;(last-but-one '(a))
;; incorrect output
;;(last-but-one '())
#|
# python while loop generated from ChatGPT to be converted tail-recursively for tail-last-but-one
def last_but_one(lst):
if len(lst) < 2:
return None # Return None if the list has less than two elements
prev = None
while len(lst) > 1:
prev = lst[0]
lst = lst[1:]
return prev
print(last_but_one([1, 2, 3, 4, 5])) # Output: 4
print(last_but_one([])) # Output: None (empty list)
print(last_but_one([1])) # Output: None (single-element list)
|#
; solution used from site, but tail-recursive using ChatGPT
(define (tail-last-but-one l)
(letrec ((last-but-one-helper
(lambda (lst prev)
(cond
((null? lst) #f) ; Base case: reached end of list
((null? (cdr lst)) prev) ; Second-to-last element found
(else (last-but-one-helper (cdr lst) (car lst)))))))
(last-but-one-helper l #f))) ; Initial call with an empty prev
;(trace tail-last-but-one)
;(tail-last-but-one '(a b c d e))
;(tail-last-but-one '(a b c d e f g h i))
;(tail-last-but-one '(a))
;(tail-last-but-one '())
; my tail-recursive length of a list
(define (tail-len lst)
(define (tail-len-helper lst count)
(cond
((null? lst) count)
(else (tail-len-helper (cdr lst) (+ count 1)))))
(tail-len-helper lst 0))
#|
# python while loop generated from ChatGPT to be converted tail-recursively for tail-last-but-one-in-list
def last_but_one_of_list(lst):
# If list has less than 2 elements, return None
if len(lst) < 2:
return None
# Initialize variables to keep track of second-to-last and last elements
second_last = None
last = lst[0]
# Iterate through the list
while len(lst) > 1:
# Update second-to-last and last elements
second_last = last
last = lst[1]
# Move to the next element
lst = lst[1:]
return second_last
lst = [1, 2, 3, 4, 5]
print(last_but_one_of_list(lst)) # Output: 4
print(last_but_one_of_list([])) # Output: None (empty list)
print(last_but_one_of_list([1])) # Output: None (single-element list)
|#
; my tail-recursive attempt from my solution
(define (tail-last-but-one-in-list l)
(define (helper-function second-last last lst)
(if (not (> (tail-len lst) 0))
second-last
(helper-function last (car lst) (cdr lst))
))
(helper-function #f (car l) l))
(trace tail-last-but-one-in-list)
(tail-last-but-one-in-list '(a b c d e))
(tail-last-but-one-in-list '(a b c d e f g h i))
(tail-last-but-one-in-list '(a))
(tail-last-but-one '())
#lang racket
(require racket/trace)
;(*) Find the K'th element of a list.
; solution from site, according to ChatGPT this function is tail-recursive
(define (find-k k lst)
(if (= k 0)
(car lst)
(find-k (- k 1) (cdr lst))))
(trace find-k)
(find-k 4 (list 1 2 3 4 5))
(find-k 3 (list 1 2 3 4 5))
(find-k 2 (list 1 2 3 4 5))
(find-k 1 (list 1 2 3 4 5))
(find-k 0 (list 1 2 3 4 5))
#lang racket
(require racket/trace)
;(*) Find the number of elements of a list.
; my solution
(define (find-length lst)
(length lst))
;(trace find-length)
;(find-length '(1 2 3 4 5))
; my 2nd solution
(define (len lst)
(if (eq? lst null)
0
(+ 1 (len(cdr lst)))
))
;(trace len)
;(len (list 1 2 3 4 5))
; tail recursive ChatGPT solution
(define (tail-rec-find-length lst)
(define (tail-rec-find-length-helper lst count)
(if (null? lst)
count
(tail-rec-find-length-helper (cdr lst) (+ count 1))))
(tail-rec-find-length-helper lst 0))
(trace tail-rec-find-length)
(tail-rec-find-length (list 1 2 3 4 5))
#lang racket
(require racket/trace)
;(*) Reverse a list.
; my solution
(define (reverse-list lst)
(reverse lst))
;(trace reverse-list)
;(reverse-list (list 1 2 3 4 5 6 7 8 9))
; my 2nd solution
(define (foldl-reverse-list lst)
(foldl cons '() lst))
;(trace foldl-reverse-list)
;(foldl-reverse-list (list 1 2 3 4 5 6 7 8 9))
#|
# python while-loop generated from ChatGPT to be converted tail-recursively for tail-rec-reverse-list
def tail_rec_reverse_list(lst):
def reverse_helper(lst, acc):
while lst:
acc = [lst[0]] + acc
lst = lst[1:]
return acc
return reverse_helper(lst, [])
# Test the function
lst = [1, 2, 3, 4, 5]
print(tail_rec_reverse_list(lst)) # Output: [5, 4, 3, 2, 1]
|#
; tail recursive solution using ChatGPT
(define (tail-rec-reverse-list lst)
(define (reverse-helper lst acc)
(if (null? lst)
acc
(reverse-helper (cdr lst) (cons (car lst) acc))))
(reverse-helper lst '()))
(trace tail-rec-reverse-list)
(tail-rec-reverse-list (list 1 2 3 4 5 6 7 8 9))
#lang racket
(require racket/trace)
;(*) Find out whether a list is a palindrome.
; check the solution on the built-in equal? command
(define (reverse-list lst)
(if (equal? (reverse lst) lst) #t #f))
;(define lst (list 1 1 3 4 1 1))
;(trace reverse-list)
;(reverse-list lst)
;(reverse-list '(1 1 3 1 1))
#|
# python while-loop from ChatGPT to be converted tail-recursively for is-palindrome
# Find out whether a list is a palindrome.
def is_palindrome(lst):
# Initialize pointers for the start and end of the list
start = 0
end = len(lst) - 1
# Iterate until the start pointer crosses the end pointer
while start < end:
# Check if the elements at start and end positions are equal
if lst[start] != lst[end]:
return False # Not a palindrome
start += 1
end -= 1
return True # Palindrome
# Test cases
print(is_palindrome([1, 2, 3, 2, 1])) # Output: True
print(is_palindrome([1, 2, 3, 4, 5])) # Output: False
print(is_palindrome([1, 2, 3, 3, 2, 1])) # Output: True
print(is_palindrome(['a', 'b', 'c', 'b', 'a'])) # Output: True
|#
; tail recursive solution from ChatGPT
(define (is-palindrome lst)
(letrec ((is-palindrome-helper
(lambda (lst start end)
(cond
((>= start end) #t) ; Base case: If start pointer crosses or equals end pointer, it's a palindrome.
((not (equal? (list-ref lst start) (list-ref lst end))) #f) ; If elements don't match, it's not a palindrome.
(else (is-palindrome-helper lst (+ start 1) (- end 1))))))) ; Continue checking recursively with updated pointers.
(is-palindrome-helper lst 0 (- (length lst) 1)))) ; Call the helper function with initial pointers.
(trace is-palindrome)
; Test cases
(display (is-palindrome '(1 2 3 2 1))) ; Output: #t (True)
(newline)
(display (is-palindrome '(1 2 3 4 5))) ; Output: #f (False)
(newline)
(display (is-palindrome '(a b c b a))) ; Output: #t (True)
(newline)
(display (is-palindrome '(a b c c b a))) ; Output: #t (True)
#lang racket
(require racket/trace)
;(**) Flatten a nested list structure.
; solution by ChatGPT 3.5
(define (flatten lst)
(cond
((null? lst) '()) ; If the list is empty, return an empty list.
((not (pair? lst)) ; If it's not a pair (i.e., an atom), return a list with that element.
(list lst))
(else ; If it's a pair, recursively flatten both the car and cdr and append the results.
(append (flatten (car lst)) (flatten (cdr lst))))))
;(trace flatten)
;(flatten '(1 (2 3) (4 (5 6))))
;(flatten '(1 (2 (3 (4 (5 (6)))))))
; found something here https://stackoverflow.com/a/19229769/8706936
(define (fold1 kons knil lst)
(if (null? lst)
knil
(fold1 kons (kons (car lst) knil) (cdr lst))))
(define (helper x lst)
(if (pair? x)
(fold1 helper lst x)
(cons x lst)))
(define (test-flatten lst)
(reverse (helper lst '())))
;(trace test-flatten)
;(test-flatten '(1 (2 3) (4 (5 6))))
;(test-flatten '(1 (2 (3 (4 (5 (6)))))))
;(test-flatten '(1 2 (3) (4 (5)) 6))
#|
# python while-loop generated from ChatGPT to be converted tail-recursively for tail-flatten
def flatten(lst):
flattened = []
i = 0
while i < len(lst):
if isinstance(lst[i], list):
lst[i:i+1] = lst[i] # Replace the nested list with its elements
else:
flattened.append(lst[i])
i += 1 # Move to the next element
return flattened
# Test cases
nested_list = [1, [2, 3], [4, [5, 6], 7], 8]
print(flatten(nested_list)) # Output: [1, 2, 3, 4, 5, 6, 7, 8]
nested_list = [[1, 2], [3, [4, 5]], 6, [7, 8]]
print(flatten(nested_list)) # Output: [1, 2, 3, 4, 5, 6, 7, 8]
|#
; tail recursive solution using ChatGPT
(define (tail-fold1 kons knil lst)
(if (null? lst)
knil
(tail-fold1 kons (kons (car lst) knil) (cdr lst))))
(define (tail-helper x lst)
(if (pair? x)
(tail-fold1 tail-helper lst x)
(cons x lst)))
(define (tail-flatten lst)
(let loop ((lst lst) (acc '()))
(if (null? lst)
(reverse acc)
(loop (cdr lst) (tail-helper (car lst) acc)))))
(trace tail-flatten)
(tail-flatten '(1 (2 3) (4 (5 6))))
(tail-flatten '(1 (2 (3 (4 (5 (6)))))))
(tail-flatten '(1 2 (3) (4 (5)) 6))
(tail-flatten '(1 (2 (3 (4 (5 (6)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment