;; Scheme ;; ; Q2 (define (cube x) (* x x x) ) ; Q3 (define (over-or-under x y) (cond ((< x y) (- 1)) ((> x y) (+ 1)) (else (+ 0 0))) ) ; Q4 (define (max a b) (if (> a b) a b)) (define (min a b) (if (> a b) b a)) (define (gcd a b) (if (or (= a 0) (= b 0)) (max a b) (if (= 0 (modulo (max a b) (min a b))) (min a b) (gcd (min a b) (modulo (max a b) (min a b))) ) ) ) ; Q5 (define lst '((1) 2 (3 . 4) 5) ) ; Q6 (define (remove item lst) (cond ((null? lst) ()) ((= (car lst) item) (if (null? (cdr lst)) () (remove item (cdr lst)) )) (else (cons (car lst) (remove item (cdr lst))))) ) ;;; Tests (remove 3 nil) ; expect () (remove 3 '(1 3 5)) ; expect (1 5) (remove 5 '(5 3 5 5 1 4 5 4)) ; expect (3 1 4 4) ; Q7 (define (filter f lst) (cond ((null? lst) ()) ((f (car lst)) (cons (car lst) (filter f (cdr lst)))) (else (filter f (cdr lst))) ) ) ; Q8 (define (make-adder num) (lambda (x) (+ x num)) ) ; Q9 (define (composed f g) (lambda (x) (f (g x))) ) ;; Extra Scheme Questions ;; ; Q10 (define (num-leaves tree) (if (null? tree) 0 (if (and (null? (left tree)) (null? (right tree))) 1 (+ (num-leaves (left tree)) (num-leaves (right tree)))) ) ) ; Q11 (define (accumulate combiner start n term) (if (= n 0) start (combiner (term n) (accumulate combiner start (- n 1) term)) ) ) ; Binary Tree ADT (define (make-btree entry left right) (cons entry (cons left right))) (define (entry tree) (car tree)) (define (left tree) (car (cdr tree))) (define (right tree) (cdr (cdr tree))) (define test-tree (make-btree 2 (make-btree 1 nil nil) (make-btree 4 (make-btree 3 nil nil) nil))) ; test-tree: ; 2 ; / \ ; 1 4 ; / ; 3
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question