Libertà vo cercando, ch'è sì cara, come sa chi per lei vita rifiuta

Swi-Prolog : ricorsione

Learn Prolog now : Recursion
Lista dei predicati in Prolog

% -- hungry bear
isDigering(X,Y):- justAte(X,Y).
isDigering(X,Y):- justAte(X,Z),
		  isDigering(Z,Y).
					
justAte(bear, john).
justAte(bear, honey).
justAte(john, chicken).
justAte(john, potatoes).
justAte(chicken, worm).

/*
	?- isDigering(bear, Gnam).
		Gnam = john ;
		Gnam = honey ;
		Gnam = chicken ;
		Gnam = potatoes ;
		Gnam = worm ;
*/

% -- The history of music 

descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z),
	       descend(Z,Y).
				
child(worksong,blues).
child(blues,jazz).
child(jazz, prog).
child(blues,rockabilly).
child(rockabilly, hardRock).
child(hardRock, metal).

% -- Counting one by one

numeral(0).
numeral( succ(X) ):- numeral(X).

% -----------------------

% -- King Fibonacci 

fib(1, 1).
fib(X, Y):- X>0, Z is X-1 ,
	    fib(Z, Y2),
	    Y is X*Y2 .

% somma da una lista			
sumList([], 0).
sumList(List, Sum):- [Head| Tail] = List,
		     sumList( Tail, TempSum),
		     Sum is  TempSum + Head.


% riconosce liste con lo stesso numero di caratteri
a2b([],[]).
a2b([_|A], [_|B]) :- a2b(A, B).


% calcolo lunghezza delle stringhe 
len([],0).
len([_|T], N):- len(T,X), 
		N is X+1.

% mischia due stringhe un carattere alla volta 
combine([],[],[]).
combine(L1, L2, Result):- 	L1 = [A|T1], L2 = [B|T2],
				combine(T1, T2, R),
				append([A,B],R,Result).
				%Result = [A,B,R]. crea una lista di liste	
	/* 
		 combine([c,n],[a,e],X).
		 X = [c, a, n, e] ;
	*/

Previous

[2] SWI-Prolog + Eclipse : Parole crociate

Next

Download of tweets with Python

1 Comment

  1. People noalrmly pay me for this and you are giving it away!

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.

Powered by WordPress & Theme by Anders Norén