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)
>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>