Welcome to W3Courses
 Like Us on Facebook

Programming

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

Flatten a List using Scheme Source Code

The following code flattens a list

CODE
(define (flatten x)
      (cond ((null? x) '())
((atom? x) (list x))
       (else (append (flatten (car x)) (flatten (cdr x))))))


RESULT
> (flatten '(a ((c d) f) d))
(a c d f d)
> (flatten '(a (((d f g)) e) h))
(a d f g e h)

Share

Infix to Postfix Function using Scheme Source Code

Infix to Postfix code

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

Share

Insertion Sort Function using Scheme Source Code

Insertion Sort function code

CODE
(define insertion-sort
  (lambda (lst)
    (if (null? lst)
        '()
        (insert (car lst)
                (insertion-sort (cdr lst))))))

Share

Return the Last Element from the List using Scheme Source Code

The following code returns the last element from the list

 CODE
 (define z 0)
 (define (last-element x)
           (cond ((null? x) z)
                     (else (set! z (car x)) (last-element (cdr x)))))


 RESULT
> (last-element '(a b c d))
d
> (last-element '(d e f g h i j k))
k

Share