Last active
December 12, 2015 04:29
-
-
Save sudowork/4715064 to your computer and use it in GitHub Desktop.
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
/** | |
* Let's define a member function to tell if x is a member of ary | |
* | |
* def member(x, ary) | |
* return false if ary == [] | |
* return true if ary.first == x | |
* member(x, ary[1...ary.size]) | |
* end | |
* | |
* In Prolog: | |
*/ | |
is_member(H, [H | _]). | |
is_member(X, [_ | T]) :- member(X, T). | |
% using member to get "different functions" | |
iterate :- is_member(Member, [1, 2, 3]), print(Member), nl. | |
generate :- is_member(1, List), print(List), nl. | |
/** | |
* Now, let's define a length function | |
* def length(ary) | |
* return 0 if ary.eql? [] | |
* length(ary.rest) + 1 | |
* end | |
* | |
* In Prolog: | |
*/ | |
size(0, []). | |
size(N, [_ | T]) :- size(N1, T), N is N1 + 1. | |
lists_of_size_2 :- size(2, List), print(List), nl. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment