Programming In Scheme: Big Word

Below is a more complex program developed in the Scheme programming language to illustrate how functional languages operate.

This particular program will take a list of atoms that are words. It will then locate the largest word among the list and output it as the result. It is performed by two functions that operate together, one that manipulates the list and one that compares the atoms.


(define bigword
  (lambda (words)
    (findbig (car words) (cdr words))
  )
)

(define findbig
  (lambda (biggest words)
    (cond
      ((null? words) (display (symbol->string biggest)))
      (else
        (if (>= (string-length (symbol->string biggest)) (string-length (symbol->string (car words))))
           (findbig biggest (cdr words))
        )
        (if (< (string-length (symbol->string biggest)) (string-length (symbol->string (car words))))
           (findbig (car words) (cdr words))
        )
      )
    )
  )
)

(bigword '(Computer Science is a science of abstraction creating the right model for a problem and devising the appropriate mechanizable techniques to solve it))

When run, the output is:

mechanizable
>

The general algorithm is as follows:
1. The sentence is a list with words as atoms
2. Going through the list check the first word’s length
3. Compare the next word length with the first’s
4. If first is larger, compare the third, fourth
5. If it is less, compare the second with third
6. Continue.

Programming In Scheme: Vector Product

Below is a simple program developed in the Scheme programming language to illustrate how functional languages operate. This particular program will perform the “vector product” or “cross product” of any two lists which atoms represent the vector’s numbers.


(define vectorproduct
  (lambda (v1 v2)
    (cond
      ((null? v1) 0)
      ((null? v2) 0)
      (else
        (+ (* (car v1) (car v2)) (vectorproduct (cdr v1) (cdr v2)) )
      )
    )
  )
)

(vectorproduct '(2 3 4) '(5 6 7))

When run, the output is:

56
>

Programming In Scheme: Attach At End

Below is a simple program developed in the Scheme programming language to illustrate how functional languages operate. This particular program will attach any atom to the end of any list. See if you can understand the functional logic behind the program.

This particular program uses the Lambda Expression in its recursion.


(define attach-at-end
  (lambda (arg lis)
    (cond
      ( (null? (cdr lis))
        (append lis (list arg))
      )
      (else
        (cons (car lis) (attach-at-end arg (cdr lis)))
      )
    )
  )
)

(attach-at-end 'apples '(1 2 3 4 5 6))

When run, the output is:

(1 2 3 4 5 6 apples)
>

Programming in Scheme: Count Distinct Elements

Below is a simple program developed in the Scheme programming language to illustrate how functional languages operate. This particular program will count the number of distinct elements in a list. See if you can understand the functional logic behind the program.


(define count-dist-elements
  (lambda (lis)
    (cond
      ( (null? lis) 0)
      ( (member (car lis) (cdr lis))
        (+ 0 (count-dist-elements (cdr lis)))
      )
      (else
        (+ 1 (count-dist-elements (cdr lis)))
      )
    )
  )
)

(count-dist-elements '(1 2 1 1 2 3 4 5 5 6 6 7 7 8 8 8 9 10 11 11 12 10 13))

When run, the output is:

13
>

Programming In Scheme: Transpose

Below is a simple program developed in the Scheme programming language to illustrate how functional languages operate. This particular program will transpose a matrix. See if you can understand the functional logic behind the program.


(define transpose
  (lambda (lis)
    (cond
      ((null? lis) ())
      ((null? (car lis)) ())
      (else
        (cons (append (map car lis)) (transpose (map cdr lis)))
      )
    )
  )
)

(transpose '( (1 2 3 4) (4 5 6 7) (7 8 9 0) ))

When run, the output is:

((1 4 7) (2 5 8) (3 6 9) (4 7 0))
>