Welcome to W3Courses
 Like Us on Facebook

Scheme

Append Two Lists Using Scheme Source Code

The following code appends two lists

 (define (append x y)
         (cond((null? x) y)
                (else (cons (car x) (append (cdr x) y) ))))

Share

Cartesian Product Using Scheme Source Code

Cartesian product code

Share

Copy List Using Scheme Source Code

The following code copies a list

 (define (copy x)
       (cond((null? x) ‘())
               (else (cons (car x) (copy(cdr x)) ))))

Share

Cube Function Using Scheme Source Code

The following code finds the cube of a number

 (define cube(lambda(x) (* x (* x x))))

or

 (define cube(lambda(x) (* x x x)))

 

Share

Delete all Occurrences of an Element from a List Using Scheme Source Code

The following code deletes all occurrences of an element from a list

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

Share

Delete the Third Occurrence of an Element from a List Using Scheme Source Code

The following code deletes the third occurrence of an element from a list

Share

Delete the Nth Occurrence of an Element from a List with Set! Using Scheme Source Code

The following code deletes the Nth occurrence of an element from a List with Set!

Share

Delete the Nth Occurrence of an Element from a List without Set! Using Scheme Source Code

The following code deletes the Nth occurrence of an element from a list without using Set!

Share

Check if two Lists are Equal using Scheme Source Code

The following code checks if two lists are equal

  (define equal(lambda (x y)
      (cond((and(null? x) (null? y)) #t)
            ((and(not (null? x)) (not (null? y))) (equal (cdr x) (cdr y)))
            (else #f))))

Share

Check if Even or Odd using Scheme Source Code

The following code checks if a number is Even or Odd

CODE
(define (even? n)
      (if (eqv? n 0) #t
             (odd? (- n 1))))
(define (odd? n)
      (if (eqv? n 0) #f
             (even? (- n 1))))

Share