Lab Software 2 2SB04 Functional Programming with Scheme


Teacher : Andrew Davison
Even now Scheme has been changed to Racket but its doesn't matter.
http://racket-lang.org/
Scheme is an innovative programming language that builds on a rich academic and practical tradition.
http://www.plt-scheme.org/


Scheme has plenty of advanced features

e.g. first-class functions
function body code can be manipulated as data.
data can be used as function body code.
Scheme is used in lots of Artficial Intelligence (AI) projects and textbooks.
If you understand it can be write easier and faster than C

For example
to define variable
      (define myPi 3.141592)
to define function

      (define (sq x)
             (* x x))
          which is return x*x;
            we use (sq 5) its return 25
Condition
        (if (> x y)x y)
         which is return x if(x>y) and return y if not.
        can be combine function and condition like this
         (if (null? (cdr l))
            true
              (if (x<y (car l) (cadr l))
                    (incresing? (cdr l))
                     false
                 )    
         )

You can write factorial function as shown here

   (define (myfac x)
     (if(< x 1)
       1
       (* x(myfac (- x 1)) )
       )
   )
Here is the better one.

(define (myfac2 x)
  (cond
   ((< x 0)
    '(Error_UnAccepted_Value))
   ((< x 1)
    1)
   (else
    (* x(myfac (- x 1)) ) )
  )
 )

Some of Useful Function



(length x)
returns the length of a list x
(append x y)
makes a new list by appending list x onto the front of list y
(reverse x)
makes a list by reversing the list x
(cadr x)
returns the head element of the tail of x
(cons h l)
makes a new list where the head is h, the tail is l
(list arg1 arg2 … argn)
places all of its arguments into a new list
(null? x)
returns true is x is an empty list; otherwise returns false
(car x)
returns the first element (head) of a non-empty list x
(cdr x)
returns the tail of a non-empty list x
(apply f x)
its first argument, f, is a function name
its second argument, x, is the input for f
same as executing (f x)
(map f x)
returns a list by applying the function f to each element of the list x

Some of Source I'm copy from Mr.Andrew Davison 's Slide.

This is Example code

(define myPi 3.141592)
(define (sq x)
  (* x x))
(define (myMax x y)
  (if (> x y)x y)
  )
(define (plusList L)
  (+(car L) (cadr L))
  )
(define (sizer x)
  (cond
    ((= x 0) 0)
    ((> x 0) 1)
    (else -1)
   )
  )

(define (len L)
  (if(null? L)
     0
     (+ 1(len (cdr L)))
     )
 )
(define (myfac x)
  (if(< x 1)
     1
     (* x(myfac (- x 1)) )
     )
 )
(define (myfac2 x)
  (cond
   ((< x 0)
    '(Error_UnAccepted_Value))
   ((< x 1)
    1)
   (else
    (* x(myfac (- x 1)) ) )
  )  
 )
(define (app l1 l2)
  (cond
    ((null? l1) l2)
    (else (cons (car l1)
                (app (cdr l1) l2)))
    )
  )
(define (mem? el l)
  (cond
    ((null? l) false)
    ((equal? el (car l)) true )
    (else (mem? el (cdr l)))
    )
  )
(define (sumto x)
  (cond
   ((< x 0)
    '(Error_UnAccepted_Value))
   ((< x 1)
    0)
   (else
    (+ x(sumto (- x 1)) ) )
  )  
 )

(define (Countdown x)
  (if(> x -1)
     (cons x (Countdown (- x 1)))
     '(0)
   )
)
(define (SumTo2 x)
  (apply + (Countdown x)) 
)
(define (doubleList l)
  (if (null? l)
    '()
   (append (cons (car l)(list (car l))) (doubleList (cdr l)))
  )
 )
(define (x<y x y)
  (if(< x y)
     true
     false
     )
  )
(define (isIncreasing? l)
   (if (null? (cdr l))
       true
        (if (x<y (car l) (cadr l))
            (isIncreasing? (cdr l))
            false
         )      
  )
 )


Popular posts from this blog

เทคนิค ชนะ เกมเศรษฐี 14 ตารวด!!! ( Let's get Rich )

Using Google Chrome Inspect Element to Analyze the website

Easy way to download whole youtube play list and convert them to mp3