Reverse a list.
my_reverse(L1,L2) :- my_rev(L1,L2,[]).
my_rev([],L2,L2) :- !.
my_rev([X|Xs],L2,Acc) :- my_rev(Xs,L2,[X|Acc]).
Reverse a list.
my_reverse(L1,L2) :- my_rev(L1,L2,[]).
my_rev([],L2,L2) :- !.
my_rev([X|Xs],L2,Acc) :- my_rev(Xs,L2,[X|Acc]).
Find the number of elements of a list.
my_length([],0).
my_length([_|L],N) :- my_length(L,N1), N is N1 + 1.
Find the K'th element of a list in prolog
element_at(X,[X|_],1).
element_at(X,[_|L],K) :- K > 1, K1 is K - 1, element_at(X,L,K1).
Find the last element of a list in prolog
my_last(X,[X]).
my_last(X,[_|L]) :- my_last(X,L).
Determining if a term is a list.
list([]).
list([X|Xs]) :- list(Xs).
?- list([a,b,c]).
yes
?- list(abc).
no
Permutation:
insert(X,L,[X|L]).
insert(X,[H|T],[H|U]) :-
insert(X,T,U).
permute([],[]).
permute([H|T],L) :-
permute(T,U),
insert(H,U,L).
ADDITION:
set x 5
set y 10
expr $x + $y
set z [ expr $x + $y]
puts "sum=$z"
output:
15
sum=15
LOOP:
set i 0
while{$i <10}{
puts $i
set i [ expr $i + 1]
}
The optional orientation values may be defined in degrees or by text like :
right (0)
right-up (45)
right-down (-45),
left (180), left-up (135), left-down (-135), up (90), down (-90).
|
|
|
The following piece of Tcl code creates three duplex links between the nodes.
|
You can save and start the script now. You might notice that the topology looks a bit awkward in nam. You can hit the 're-layout' button to make it look better, but it would be nice to have some more control over the layout. Add the next three lines to your Tcl script and start it again.
|
You will probably understand what this code does when you look at the topology in the nam window now. It should look like the picture below.
$ns duplex-link node1 node2 bandwidth delay queue-type
|
A new node object is created with the command '$ns node'. The above code creates two nodes and assigns them to the handles 'n0' and 'n1'.
The next line connects the two nodes.
|
This line tells the simulator object to connect the nodes n0 and n1 with a duplex link with the bandwidth 1Megabit, a delay of 10ms and a DropTail queue.
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
$ns color 1 blue: is to set color of the packets for a flow specified by the flow id (fid).
$n3 shape "square"
$n3 color "black"
$n0 shape "square"
$n0 color "blue"
#Setup a UDP connection
set udp [new Agent/UDP]1. #Setup a TCP connection(ftp)
set tcp [new Agent/TCP]1.Network protocol stack written in C++
2.Tcl (Tool Command Language) used for
specifying scenarios and events.
3. Simulates both wired and wireless networks.
4.Ns, the simulator itself
5.Nam, the network animator--Nam editor: GUI interface to generate ns scripts
NAM (NETWORK ANIMATOR)
-> PROVIDES A VISUAL INTERPRETATION OF THE NETWORK CREATED
-> CONTROLS PLAY,FAST FORWARD,REWIND,PAUSE
X-GRAPH
->INTERACTIVE PLOTTING,GRAPHING ANIMATED AND DERIVATIVES
->SIZE 800x400