Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
side by side Prolog and Erlang list reversal
%%% Prolog
% [reverse].
% [reverse loaded]
% yes
%
% | ?- reverse([1,2,3],L).
% L = [3,2,1]
%
reverse(List, ReversedList) :-
reverse(List, [], ReversedList).
reverse([], ReversedList, ReversedList).
reverse([Head | Rest], TempList, ReversedList) :-
reverse(Rest, [Head | TempList], ReversedList).
%%% Erlang
%
% 1> c(reverse).
% {ok,reverse}
% 2> reverse:reverse([1,2,3]).
% [3,2,1]
reverse(List) ->
reverse(List, []).
reverse([Head | Rest], Reversed_List) ->
reverse(Rest, [Head | Reversed_List]);
reverse([], Reversed_List) ->
Reversed_List.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment