This is a sample blog posting.

If you log in to the site (the Author Login link is on the very bottom of this page) you will be able to edit it and all of the other existing articles. You will also be able to create a new article and make other changes to the site.

As you add and modify articles you will see how your site changes and also how you can customise it in various ways.

Go ahead, you can't break it.



1)

fac :: Integer -> Integer

fac n | n == 0 = 0

fac n | n > 0 = n+fac(n-1)

main = do

    print (fac 5)

 

stdin
 copy
Standard input is empty
 stdout
 copy
15




2)

sieve :: Integral a => [a] -> [a]
sieve (p : xs) = p : sieve [x | x <- xs, x `mod` p /= 0]

primes :: [Integer]
primes = sieve [2..]

main = print $ take 100 primes

stdin
 copy
Standard input is empty
 stdout
 copy
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541]



3)


main = do
putStrLn "Hello, World!"
return ()

stdin
 copy
Standard input is empty
 stdout
 copy
Hello, World!
 
 4)
 
-module(prog).
-export([main/0]).

main() ->io:fwrite("hello, world\n").
 
 
stdin
 copy
Standard input is empty
 stdout
 copy
hello, world

 
5)

program HelloWorld;
uses crt;

(* Here the main program block starts *)
begin
writeln('Hello, World!');
readkey;
end.

stdin
 copy
Standard input is empty
 stdout
 copy
Hello, World!

6)

// Your First Program

class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}


stdin
 copy
Standard input is empty
 stdout
 copy
Hello, World!



7)
 
quicksortdemo :: (Ord m) => [m] -> [m]
quicksortdemo [] = []
quicksortdemo (q:qs) = quicksortdemo [m | m <- qs, m < q] ++ [q] ++ quicksortdemo [n | n <- qs, n >= q]

main = do
print("Demo to show quicksort in haskell !!")
let array1 = [10, 30, 56, 2, 1]
let array2 = [23, 13, 10, 8, 9, 1, 5, 42, 50]
let array3 = [3, 7, 1, 4, 9, 56, 10]
let array4 = [9, 6, 12, 15, 10 , 2 , 4, 20]
let array5 = [200, 100, 400, 250, 450, 150, 350]

print("Printing result after sorting in quicksort in haskell !!")
let result1 = quicksortdemo array1
let result2 = quicksortdemo array2
let result3 = quicksortdemo array3
let result4 = quicksortdemo array4
let result5 = quicksortdemo array5
print("first result array is ::", result1)
print("second result array is ::", result2)
print("third result array is ::", result3)
print("fourth result array is ::", result4)
print("fifth result array is ::", result5)
 
 
 
stdin
 copy
Standard input is empty
 stdout
 copy
"Demo to show quicksort in haskell !!"
"Printing result after sorting in quicksort in haskell !!"
("first result array is ::",[1,2,10,30,56])
("second result array is ::",[1,5,8,9,10,13,23,42,50])
("third result array is ::",[1,3,4,7,9,10,56])
("fourth result array is ::",[2,4,6,9,10,12,15,20])
("fifth result array is ::",[100,150,200,250,350,400,450])
 
 
 8)
 
Testing.pl
 
likes(dan, sally).
likes(sally, dan).
likes(josh, brittney).

dating(X, Y) :-
likes(X, Y),
likes(Y, X).

friendship(X, Y) :-
likes(X, Y);
likes(Y, X).
 
Queries : 
 
 
Welcome to SWI-Prolog (threaded, 64 bits, version 8.4.0)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?-  ['/Users/DELL/Desktop/testing.pl'].
true.

?- likes(dan, sally).
true.

?- likes(blake, josh).
false.

?- dating(dan, sally).
true.

?- dating(josh, brittney).
false.

?- likes(brittney, josh).
false.

?- dating(sally, dan).
true.

?- friendship(josh, brittney).
true 
 
 
9)
 
 
% file: queens.pl

queens(N, Qs) :-
        range(1, N, Ns),
        queens(Ns, [], Qs).

queens([], Qs, Qs).
queens(UnplacedQs, SafeQs, Qs) :-
        select(UnplacedQs, UnplacedQs1, Q),
        not_attack(SafeQs, Q),
        queens(UnplacedQs1, [Q|SafeQs], Qs).

not_attack(Xs, X) :-
        not_attack(Xs, X, 1).

not_attack([], _, _) :- !.
not_attack([Y|Ys], X, N) :-
        X =\= Y+N,  X =\= Y-N,
        N1 is N+1,
        not_attack(Ys, X, N1).

select([X|Xs], Xs, X).
select([Y|Ys], [Y|Zs], X) :- select(Ys, Zs, X).

range(N, N, [N]) :- !.
range(M, N, [M|Ns]) :-
        M < N,
        M1 is M+1,
        range(M1, N, Ns).

run :-
        statistics(walltime, [Start,_]),
        findall(X, queens(10, X), L),
        statistics(walltime, [End,_]),
        length(L, N),
        Time is End - Start,
        format('~d solutions in ~3d seconds.~n', [N,Time]).


OUTPUT Queries

% sicstus -P
SICStus -P 3 #0: Mon Oct 3 12:44:31 MET 1994
| ?- compile(queens).
{compiling /src/sicstus/muse/V3/examples/queens.pl...}

yes
| ?- muse_flag(num_workers,_,5).
| ?- run.
724 solutions in 2.760 seconds.

yes
| ?- muse_flag(num_workers,_,1).
| ?- run.
724 solutions in 10.400 seconds.

yes
| ?- Speedup is 10400/2760.

Speedup = 3.7681159420289854 ? 

yes
| ?- halt.

% 

10)

testing2.pl

character(hercules).
character(abraham_lincoln).
character(george_washington).
character(ulysses_grant).
character(zeus).
character(venus).
character(hermes).
character(cleopatra).
real(abraham_lincoln).
real(george_washington).
real(ulysses_grant).
real(cleopatra).
mythological(hercules).
mythological(zeus).
mythological(hermes).
mythological(venus).
immortal(X) :-
mythological(X).
mortal(X) :-
real(X).

Queries :

Welcome to SWI-Prolog (threaded, 64 bits, version 8.4.0)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- ['/Users/DELL/Desktop/testing2.pl'].
true.

?- mortal(X).
X = abraham_lincoln ;
X = george_washington ;
X = ulysses_grant ;
X = cleopatra.

?- immortal(X).
X = hercules ;
X = zeus ;
X = hermes ;
X = venus.

?-


 11)

testing4.pl

 

parent(robert,dan).
parent(geraldine,dan).
parent(art,robert).
parent(john,art).
parent(dan,sean).
parent(dan,ryan).

descendant(D,A) :- parent(A,D).
descendant(D,A) :- parent(P,D), descendant(P,A).

 

Output queries 

?- ['/Users/DELL/Desktop/testing4.pl'].
true.

?- descendant(robert,dan).
false.

?- parent(robert,dan).
true.

?- descendant(sean,john).
false.

?- descendant(P,dan).
P = sean .

?- descendant(P,dan).
P = sean ;
P = ryan.

?- descendant(P,dan).
P = sean ;
P = ryan.

?- parent(dan,A).
A = sean ;
A = ryan.

?- parent(D,sean).
D = dan.

?-

 

12)

testing5.pl

sumof([],0).
sumof([X|Y],Z):-
sumof(Y,W),
Z is X + W.

 

 Output queries :

Welcome to SWI-Prolog (threaded, 64 bits, version 8.4.0)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- ['/Users/DELL/Desktop/testing5.pl'].
true.

?- sumof([1,2,3,4,5,6,7,8,9,19],X).
X = 64.

?-

 

13)

TowerOfHanoi.pl

move(1,X,Y,_) :-
write('Move top disk from '), write(X), write(' to '), write(Y), nl.
move(N,X,Y,Z) :-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).

 

Output Queries 

?- move(4,source,target,auxiliary).
Move top disk from source to auxiliary
Move top disk from source to target
Move top disk from auxiliary to target
Move top disk from source to auxiliary
Move top disk from target to source
Move top disk from target to auxiliary
Move top disk from source to auxiliary
Move top disk from source to target
Move top disk from auxiliary to target
Move top disk from auxiliary to source
Move top disk from target to source
Move top disk from auxiliary to target
Move top disk from source to auxiliary
Move top disk from source to target
Move top disk from auxiliary to target
true .

 

14)

 

PROGRAM HeronFormula
IMPLICIT NONE

REAL :: a, b, c ! three sides
REAL :: s ! half of perimeter
REAL :: Area ! triangle area
LOGICAL :: Cond_1, Cond_2 ! two logical conditions

READ(*,*) a, b, c

WRITE(*,*) "a = ", a
WRITE(*,*) "b = ", b
WRITE(*,*) "c = ", c
WRITE(*,*)

Cond_1 = (a > 0.) .AND. (b > 0.) .AND. (c > 0.0)
Cond_2 = (a + b > c) .AND. (a + c > b) .AND. (b + c > a)
IF (Cond_1 .AND. Cond_2) THEN
s = (a + b + c) / 2.0
Area = SQRT(s * (s - a) * (s - b) * (s - c))
WRITE(*,*) "Triangle area = ", Area
ELSE
WRITE(*,*) "ERROR: this is not a triangle!"
END IF

END PROGRAM HeronFormula

 

output:

3
6
8
a = 3.00000000
b = 6.00000000
c = 8.00000000

Triangle area = 7.64444256


...Program finished with exit code 0
Press ENTER to exit console.

 

15)

 

mylist = [ [2,4,1], [1,2,3], [2,3,5] ]
a=0
b=0
total = 0
while a <= 2:

while b < 2:

total += mylist[a][b]
print("a= ",a)
print("b= ",b)

b += 1
a += 1


print (total)

 

OUTPUT: 

a= 0
b= 0
a= 0
b= 1
6
>

 

16)

mylist = [ [2,2,1], [1,2,3], [1,1,1] ]

total = 0

for sublist in mylist:

total += sum(sublist)

print(total)

 

OUTPUT: 

14
>