EOPL assignment1

It is all about recursion……



;;this is test function
(define (count1 x)
       (+ x 1))
(define count
  (lambda (x)
    (+ x 1)))

;;1. Define and test a procedure countdown that takes a natural number and returns a list of the natural numbers less than or equal to that number, in descending order.

(define (countdown x)
  (if (zero? x) '(0)
      (cons x (countdown (- x 1)))))
;;(countdown 5)
;;2. Define and test a procedure insertR that takes two symbols and a list and returns a new list with the second symbol inserted after each occurrence of the first symbol.
(define insertR
  (lambda (x y z)
    (if (null? z)
        z
        (if (eq? x (car z))
            (cons (car z) (cons y (insertR x y (cdr z))))
            (cons (car z)  (insertR x y (cdr z)))))))
;;3.Define and test a procedure remove-1st that takes a a symbol and a list and returns a new list with the first occurrence of the symbol removed.

(define remove-1st
  (lambda (x xs)
    (if (null? xs)
        xs
        (if (eq? x (car xs))
                 (cdr xs)
                 (cons (car xs) (remove-1st x (cdr xs)))))))
    
;;5. filter
(define filter1
  (lambda (predict xs)
    (if (null? xs)
        xs
        (if (predict (car xs))
            (cons (car xs) (filter1 predict (cdr xs)))
            (filter1 predict (cdr xs))))))
;;; ( ) [ ] { } " , ' ` ; # | \ all of thest can not be used in the indentifers.
;;find function in haskell
(define find1
  (lambda (predict xs)
    (if (null? xs)
        xs
        (if (predict (car xs))
            (car xs)
            (find1 predict (cdr xs)))))) 
 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s