Welcome to W3Courses
 Like Us on Facebook

Programming

Calculate Length of a List using Scheme Source Code

The following code calculates length of a list

 CODE
(define (length ls)
      (if (null? ls)
             0
             (+ (length (cdr ls)) 1))))

Share

How to check if an email address exists without sending an email?

We have all been doing email address validation for a very long time to make sure that the email is correctly formatted. This is to avoid users entering wrongly formatted email address but still they can accidentally give us a wrong email address.

Example of a correctly formatted email address but still wrong:

mailbox.does.not.exist@reddit.com [VALID email fromat but still not correct]

Share

Logical Connectives (logical and, or, not, xor, imp) using Scheme Source Code

The following code makes use of logical connectives (logical and, or, not, xor, imp)

CODE
;and (logical connective)
(define (and x y)
      (cond ((eqv? x #t) y)
            (else #f)))

Share

Map Function using Scheme Source Code

Map function code

(define (map f L)
       (cond ((null? L) ‘())
               (else (cons (f (car L)) (map f (cdr L))))))

Share

Check if an Element is a Member of a List using Scheme Source Code

The following code checks if an element is a member of a list

CODE
(define (member? x y)
      (cond ((null? y) #f)
((eqv? x (car y)) #t)
             (else (member? x (cdr y)))))


RESULT
> (member_list 'd '(a x c v b))
#f
> (member? 'f  '(a s d f g h))
#t


 

Share

Power Function using Scheme Source Code

Power function code. Computes like 2^5 = 32

CODE
(define power
(lambda( x y)
(cond((eqv? y 0) 1)
((> y 0) (* (power x (- y 1)) x)))))

Share

Powerset Function using Scheme Source Code

Powerset function code

CODE
(define (powerset a)
   (if (null? a) (list '())
         (let ((p (powerset (cdr a))))
               (append (map (lambda (x) (cons (car a) x)) p) p))))

Share

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