Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang racket
- ; reversing a list
- (define (my_reverse x)
- (if (null? x)
- x
- #| append rest of the elements (cdr) of my_reverse x
- to the first part of x (car)
- |#
- (append (my_reverse (cdr x)) (list (car x)))
- )
- )
- ; Fibonacci digits
- (define (Fib x)
- (cond
- ((zero? x ) 0)
- ((= x 1) 1)
- (else
- (+ (Fib (- x 1)) (Fib (- x 2)))
- )
- )
- )
- ; sum ignoring lists
- (define (sum x)
- (cond
- ((null? x) 0)
- ((= (length x) 1)
- (if (number? (car x))
- (car x)
- 0
- )
- )
- (else
- (if (number? (car x))
- (+ (car x) (sum (cdr x)))
- (sum (cdr x))
- )
- )
- )
- )
- ; nested sum
- (define (nested_sum x)
- (cond
- ((null? x) 0)
- (else
- (if (number? (car x))
- (+ (car x) (nested_sum (cdr x)))
- (+ (nested_sum (car x)) (nested_sum (cdr x)))
- )
- )
- )
- )
- ; remove nested lists
- (define (rmv_lists x)
- (cond
- ((null? x) x)
- (else
- (if (not (list? (car x)))
- (append (list (car x)) (rmv_lists (cdr x)))
- (append (rmv_lists (car x)) (rmv_lists (cdr x)))
- )
- )
- )
- )
- ; mean of a list of numbers
- ; we assume all elements of the input list are numbers
- ; we also assume there's no nested lists
- ; we use the previously defined sum function
- (define (my_mean x)
- (cond
- ((null? x) (display "Error"))
- (else
- (/ (sum x) (length x))
- )
- )
- )
- ;Test cases
- ;(my_reverse '(1 2 3 4 5 6))
- ;(Fib 8)
- ;(sum '('(1) 2 3 4 5))
- ;(nested_sum '(1 (2 (3)) (4 (5)) 6))
- ;(rmv_lists '(1 (((a 2) b) 3) c))
- ;(my_mean '(18 5.1 7 19.6 20))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement