Thursday 27 September 2012

A Simple Python Program for Moving a Car

        This is a program in python for develop a GUI for moving car using buttons 

from Tkinter import *
import Tkinter as tk
class Carcontrol(Frame):
    def __init__(self,name):
        Frame.__init__(self,bg='white')
        self.pack(expand=YES,fill=BOTH)
        self.master.title("Control the CAR")       

        f=Frame(self,width=500,height=500,bg='white')
        f.pack()
       
        self.f1=Frame(f)
        self.f1.pack(side=TOP,padx=10,pady=10)
        f2=Frame(f)
        f2.pack(side=TOP)

        self.w=Canvas(self.f1)
        self.w.canvasx ( 500, gridspacing=None )
        self.w.canvasy ( 500, gridspacing=None )
        self.p=self.w.create_polygon( 50,50,100,50,125,75,25,75,fill="green")
        self.r=self.w.create_rectangle(10,75,140,100,fill="red")
        self.o1=self.w.create_oval(20,95,45,125,fill="black")
        self.o2=self.w.create_oval(105,95,130,125,fill="black")
        self.w.pack()
       
        b1=Button(f2,text="LEFT",command=self.left)
        b1.pack(side=LEFT)
   
        b2=Button(f2,text="RIGHT",command=self.right)
        b2.pack(side=RIGHT)

    def left(self):
        print "left"
        self.w.move(self.p,-10,0)
        self.w.move(self.r,-10,0)
        self.w.move(self.o1,-10,0)
        self.w.move(self.o2,-10,0)
       
    def right(self):
        print "right"
        self.w.move(self.p,10,0)
        self.w.move(self.r,10,0)
        self.w.move(self.o1,10,0)
        self.w.move(self.o2,10,0)
       
def main():
    Carcontrol("").mainloop()

if __name__=="__main__":
    main()





Tuesday 11 September 2012

Constructing Procedures Using Lambda in Functional Programming

A powerful programming language is the ability to build abstractions by assigning names to common patterns and then to work in terms of the abstractions directly.Procedures provide this ability.Procedures that manipulate procedures are called higher-order procedures.

 The higher-order procedures can serve as powerful abstraction mechanisms, vastly increasing the expressive power of our language

 (lambda (x) (+ x 4))

 and

 (lambda (x) (/ 1.0 (* x (+ x 2))))
.

This is pi-sum  procedure
  
(define (pi-sum a b)
  (if (> a b)
      0
      (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))



pi-sum procedure can be expressed without defining any auxiliary procedures as


(define (pi-sum a b)
  (sum (lambda (x) (/ 1.0 (* x (+ x 2))))
       a
       (lambda (x) (+ x 4))
       b))


using lambda, we can write the integral procedure without having to define the auxiliary procedure add-dx:


(define (integral f a b dx)
  (* (sum f
          (+ a (/ dx 2.0))
          (lambda (x) (+ x dx))
          b)
     dx))


In general, lambda is used to create procedures in the same way as define, except that no name is specified for the procedure

(lambda (<formal-parameters>) <body>)

We can read a lambda expression as follows:
    (lambda             (x)             (+    x     4))
                                                
 the procedure   of an argument x  that adds  x and 4


a procedure as its value, a lambda expression can be used as the operator in a combination such as


((lambda (x y z) (+ x y (square z))) 1 2 3)
12