Created
February 12, 2012 02:41
-
-
Save brikis98/1805899 to your computer and use it in GitHub Desktop.
Seven Languages in Seven Weeks: Prolog, Day 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fact(0, 1). | |
fact(N, Out) :- N > 0, N1 is N - 1, fact(N1, Prev), Out is N * Prev. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fib(1, 1). | |
fib(2, 1). | |
fib(N, Out) :- N > 2, N1 is N - 1, N2 is N - 2, fib(N1, Prev), fib(N2, PrevPrev), Out is Prev + PrevPrev. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sort(list) = | |
for i in [0..list.length] | |
list[i] <= list[i + 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sum([a1, a2, a3, ..., an]) = a1 + a2 + a3 + ... + an |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
min([Head | []], Head). | |
min([Head | Tail], TailMin) :- min(Tail, TailMin), TailMin =< Head. | |
min([Head | Tail], Head) :- min(Tail, TailMin), TailMin > Head. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reverse_list([], []). | |
reverse_list([Head | Tail], Out) :- reverse_list(Tail, TailReversed), append(TailReversed, [Head], Out). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sort_list([], []). | |
sort_list([Head], [Head]). | |
sort_list([First, Second | Tail], Sorted) :- | |
divide([First, Second | Tail], Left, Right), | |
sort_list(Left, LeftSorted), | |
sort_list(Right, RightSorted), | |
merge(LeftSorted, RightSorted, Sorted). | |
merge(LeftList, [], LeftList). | |
merge([], RightList, RightList). | |
merge([LeftHead | LeftTail], [RightHead | RightTail], [LeftHead | Merged]) :- | |
LeftHead =< RightHead, | |
merge(LeftTail, [RightHead | RightTail], Merged). | |
merge([LeftHead | LeftTail], [RightHead | RightTail], [RightHead | Merged]) :- | |
LeftHead > RightHead, | |
merge([LeftHead | LeftTail], RightTail, Merged). | |
divide([], [], []). | |
divide([Head], [Head], []). | |
divide([First, Second | Tail], [First | Left], [Second | Right]) :- | |
divide(Tail, Left, Right). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sort_list(Input, Output) :- | |
permutation(Input, Output), | |
check_order(Output). | |
check_order([]). | |
check_order([Head]). | |
check_order([First, Second | Tail]) :- | |
First =< Second, | |
check_order([Second | Tail]). | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sum(0, []). | |
sum(Total, [Head | Tail]) :- sum(Sum, Tail), Total is Head + Sum. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See Seven Languages in Seven Weeks: Prolog, Day 2 for more information.