Saturday, September 6, 2014

Basic Arduino Part2: connect with button


 Code for this project.

int LED = 12;
int BUTTON =4;

void setup(){
 
  pinMode(LED,OUTPUT);
  pinMode (BUTTON, INPUT);
 
}

void loop (){
  if(digitalRead(BUTTON)==HIGH)
  {
    digitalWrite(LED,HIGH);
  } else
  {digitalWrite(LED,LOW);} 
 
}


Demonstration:


Basic Arduino LED blink:

this video is presenting of how to connect led wire from arduino board to Solderless bredboard mini with 30k resister 

Wednesday, May 7, 2014

Dynamic and Static

1) If something is done dynamically, this means…
a) It is done at compile-time
b) It is done at link-time
c) It is done at load-time
d) It is done at run-time
2) Syntax refers to the…
a) Tokens of a language
b) Abstract Syntax Tree of a language
c) Grammar rules of a language 
d) Meaning behind a language

3) If something is done statically, this means…
a) It is done at compile-time 
b) It is done at link-time
c) It is done at load-time
d) It is done at run-time
4) Semantics refers to the…
a) IR of a language
b) Control Flow Graph of a language
c) Grammar rules of a language
d) Meaning behind a language
5) Which phase is *not* part of the back-end of a typical compiler:
a) Semantic analyzer
b) Code generator
c) Optimizer
d) Scanner
6) Your talking with a friend at another university, and he/she mentions reading about compilers and data
flow analysis. Data flow analysis is based on what kind of internal representation?
a) Context Free Grammar
b) Control Flow Graph 
c) Abstract Syntax Tree
d) Finite State Machine
7) Continuation of previous question… What is data flow analysis used for?
a) Detect various syntax errors
b) Detect various lexical errors
c) Detect various semantic errors 
d) Symbol table construction

C++ 11 Lambda Expression

Consider the following lambda expression in C++11


[&] (int i)
{
   if (A[i] > max)
       max = A[i];
}

a) Lambda expressions, whether in c++11 or F# , require the notation of a closure . Define the concept of a "Closure" and provide an example base on the lambda expression shown above.

A closure is the outside data needed n order to execute a lambda expression . For example, in the lambda expression shown above , the lambda expression needs the array A and the variable max.

b) What closure semantics are being used in the lambda expression above?

by reference (&) sign

c) explain in more detail

A closure by reference means the lambda expression has references to the need vaiables, i,e " pointers" For variable max, this is necessary so we can change the value of max if we find larger element in the array.

d) Why did the designers of c++11 include the concept of closure semantics?

closure semantics are included in c++11 to allow programmers to control 3 thngs

1) Whether the lambda expression has access to outside data or not("none")
2)whether the lambda expression can change("&") or not change ("=") a varible declared outside the scope of the lambda. " prevent side-effects"
3) to control the copying of data that might slow down the performance

Functional Programming F# makeAbsolute

Suppose L is a List of integers write a function in F# called makeAbsolute that returns a new list where every element is non-negative

for Example : L = [1;-2;3;0;-5]
new L will be L = [1;2;3;0;5]

let  rec  makeAbsolute (L) =
     match L with
| [  ] -> [ ] // base case if the list is empty return an empty list
|  _ ->  if L.Head < 0 // if the head of the list is negative
            then -L.Head :: makeAbsolute(L.Tail) // negate the head of the list to make it positive then //concatenate to the list
            else   L.Head :: makeAbsolute(L.Tail)//other wise concatenate normal element to the list

Another way in high functional programming

let abs(x) = if x< 0 then -x else x  // create a function that convert negative element to positive

let  rec makeAbsolute(L) =
  match L with
 | [  ] -> [ ] //again base case if the list is empty return an empty list
 |  _   -> abs(L.Head) :: makeAbsolute(L.Tail) // apply the abs function to the head of the list then recursively //apply to all element in the list



Functional programming F # Tail Element

Write a function that given a list , return the last element in the list (assume the list always contain at least one element.

let rec tailElement (L) =     // passing L array list
match L with                     // match the element in the list
| [E] -> E                          // base case , if there is one thing in the list return that element
| _ -> tailElement(L.Tail)
 // if there are more than one element walk from the second element(L.Tail) of the list  at the end of the list we will get the last element


Merge two List in F#

Merge is a function that takes two sorted lists in ascending order , and returns a sorted list in ascending order for example :

merge ([1;100;200],[2;3;4;100;101;300])
returned list [1;2;3;4;100;100;101;200;300]


let rec merge (L1,L2) =
match L1,L2 with
| [ ] ,[ ] ->[ ]
| _ ,[  ] ->L1
| [ ] ,_  -> L2
| _ , _ ->   if L1.Head < L2.Head then  L1.Head::merge(L1.Tail , L2)
                else  L1.Head::merge(L1, L2.Tail)


let R2 = merge([1;100;200],[2;3;4;100;101;300])
printfn "%A" R2