Welcome to W3Courses
 Like Us on Facebook

Scheme

Queue Function using Scheme Source Code

Queue function code

Share

Reverse a list using Scheme Source Code

The following code reverses a list

CODE
(define (reverse x)
         (if (null? x) ‘()
              (append (reverse (cdr x)) (list (car x)))))


 

Share

Replace an Element from a List using Scheme Source Code

The following code replaces an element from a list

CODE
(define replace( lambda (x b y)
      (if (null? y )
             ()
      (if (eqv? (car y) x)
             (cons b (replace x b (cdr y)))
      (cons (car y) (replace x b (cdr y)))))))

Share

Check if a list is Shorter than the Other using Scheme Source Code

The following code checks if a list is shorter than the other

CODE
(define shorter(lambda(x y)
          (cond ((null? x) 1)
                ((null? y) 0)
                (else (shorter (cdr x) (cdr y)))))

Share

Stack Function using Scheme Source Code

Stack function code

Share

Sum all Integers between given Two Integers using Scheme Source Code

The following code sums all integers between given two integers

CODE
 (define (sumall a b)
      (cond ((= a b) a)
            (else (+ a (sumall (+ a 1) b)))))


The following will add 2+3+4+5

RESULT
> (sumall 2 5)
14

Share

Tower of Hanoi Function using Scheme Source Code

Tower of Hanoi function code

 (define (hanoi n a b c)
      (if (eqv? n 0) '()
      (append (hanoi (- n 1) a c b)
            (list (cons a b))
            (hanoi (- n 1) c b a))))

Share

Tree Function using Scheme Source Code

Tree function code

Share