Scheme Exercise
my-reverse
my-reverse
(define (my-reverse ls)
(my-reverse-rec ls '()))
(define (my-reverse-rec ls p)
(if (null? ls)
p
(my-reverse-rec (cdr ls) (cons (car ls) p))))
sum
define sum
(define (sum l)
(sum-rec l 0))
(define (sum-rec l p)
(if (null? l)
p
(sum-rec (cdr l) (+ (car l) p))))
string->number
(define (string->number s)
(let ([l (string->list s)])
(string->number->rec l 0)
))
(define (string->number->rec l p)
(if (null? l)
p
(string->number->rec (cdr l) (+ (* p 10) (- (char->integer (car l)) 48)))))
fact-let
(define (fact-let n)
(let loop((n1 n) (p n))
(if (= n1 1)
p
(let ((m (- n1 1)))
(loop m (* p m))))))
A function that takes a list (ls) and an object (x) as arguments and returns a list removing x from ls.
(define (remove2 l k)
(let loop([l2 l] [p '()])
(if (null? l2)
(reverse p)
(if (= k (car l2))
(loop (cdr l2) p)
(loop (cdr l2) (cons (car l2) p))))))
(remove2 '(1 2 7 3 4 7 5 6 7 8 9) 7)
A function that takes a list (ls) and and an object (x) and returns the first position of x in ls. The position is counted from 0. If x is not found in ls, the function returns #f.
(define (pos ls x)
(let loop([ls2 ls] [p 0])
(if (null? ls2)
#f
(if (= x (car ls2))
p
(loop (cdr ls2) (+ p 1))))))
my-reverse that reverse the order of list items. (Function reverse is pre-defined.)
(define (my-reverse ls) (let loop([ls2 ls] [p '()]) (if (null? ls2) p (loop (cdr ls2) (cons (car ls2) p)))))
Summarizing items of a list consisting of numbers.
(define (sum ls)
(let loop([l ls] [p 0])
(if (null? l)
p
(loop (cdr l) (+ p (car l))))))
Converting a string that represents a positive integer to the corresponding integer, i.e. "1232" → 1232. Input error check is not required.
(define (str->num str)
(let loop([ls (string->list str)] [num 0])
(if (null? ls)
num
(loop (cdr ls) (+ (- (char->integer (car ls)) 48) (* num 10))))))
The function range that returns a list of numbers from 0 to n (not including n).
(define (range ls offset)
(let loop([l ls] [p '()] [pos 0])
(if (null? l)
(reverse p)
(if (< pos offset)
(loop (cdr l) (cons (car l) p) (1+ pos))
(reverse p)))))
my-reverse that reverse the order of list items. (Function reverse is pre-defined.)
(define (my-reverse ls)
(letrec ([iter (lambda (l p)
(if (null? l)
p
(iter (cdr l) (cons (car l) p))))])
(iter ls '())))
Summarizing items of a list consisting of numbers.
(define (sum ls)
(letrec ([iter (lambda (l p)
(if (null? l)
p
(iter (cdr l) (+ p (car l)))))])
(iter ls 0)))
Converting a string that represents a positive integer to the corresponding integer, i.e. "1232" → 1232. Input error check is not required.
(define (str->num str)
(letrec ([iter (lambda (ls num)
(if (null? ls)
num
(iter (cdr ls) (+ (* num 10) (- (char->integer (car ls)) 48)))))])
(iter (string->list str) 0)))
fact
(define (fact-do n)
(do ((n1 n (1- n1)) (p n (* p (- n1 1)))) ((= n1 1) p)))
reverse-do
(define (reverse-do ls)
(do ([l ls (cdr l)] [p '() (cons (car l) p)]) ((null? l) p)))
Summarizing items of a list consisting of numbers.
(define (sum-do lis)
(do ([l lis (cdr l)] [p 0 (+ p (car l))]) ((null? l) p)))
Converting a string that represents a positive integer to the corresponding integer, i.e. "1232" → 1232. Input error check is not required.
(define (str->num str)
(do ([s (string->list str) (cdr s)] [p 0 (+ (* p 10) (- (char->integer (car s)) 48))])
((null? s) p)))
Sort by magnitude of sin(x) in ascending order.
(sort (lambda (x y)
(< (sin x) (sin y))) '(1 0.5 -1 0 -0.5))
Sort by length of lists in descending order.
(define (sort-lenth ls)
(sort (lambda (x y) (> (length x) (length y))) ls ))
(sort-length (list (list 1 2 3) (list 2 3)))
Write a function that squares each item of a list, then sums them and then makes square root of it.
(define (sqrt-sum-sq-a ls)
(sqrt (apply + (map (lambda (x) (* x x)) ls))))
(sqrt-sum-sq-a '(1 2 3 4 5))
Define keep-matching-items by yourself.
(define (keep-matching-items proc ls)
(let loop([l ls] [p '()])
(if (null? l)
p
(loop (cdr l) (if (proc (car l)) (cons (car l) p) p)))))
Define map by yourself. It may be some how difficult to accept more than one list as arguments.
(define (my-map fun . lss)
(letrec ([iter (lambda (fun lss)
(if (null? lss)
'()
(cons (fun (car lss))
(iter fun (cdr lss)))))]
[map-rec (lambda (fun lss)
(if (memq '() lss)
'()
(cons (apply fun (iter car lss))
(map-rec fun (iter cdr lss)))))])
(map-rec fun lss)))
(my-map (lambda (x) (* x x)) '(1 2 3 4 5))
(my-map + '(1 2 3) '(4 5 6))
Write a function to copy files (my-copy-file).
(define (my-copy-file file-name new-file-name)
(call-with-input-file file-name (lambda (p)
(let loop([ls1 '()] [c (read-char p)])
(if (eof-object? c)
(begin
(close-input-port p)
(call-with-output-file new-file-name
(lambda (p2)
(let loop2((ls2 (reverse ls1)))
(if (null? ls2)
(close-output-port p2)
(begin
(write-char (car ls2) p2)
(loop2 (cdr ls2))))))))
(loop (cons c ls1) (read-char p)))))))
(my-copy-file "e:\\iNote\\readme.md" "e:\\iNote\\hello.md")
Write the function (print-lines) that takes arbitrary number of strings as arguments and outputs them to the standard output. The output strings should be separated by newline.
(define (print-lines . strs)
(let loop([ls strs])
(if (not (null? ls))
(begin
(display (car ls))
(newline) (loop (cdr ls)) )
)))
(print-lines "hello" "world")
Modify make-bank-account so that withdrawing more than balance causes error.
(define (make-bank-account balance)
(lambda (n)
(if (< (+ balance n) 0)
#f
(begin
(set! balance (+ balance n))
balance))))