Skip to content

Instantly share code, notes, and snippets.

@jdallien
Created December 28, 2008 17:36
Show Gist options
  • Save jdallien/40998 to your computer and use it in GitHub Desktop.
Save jdallien/40998 to your computer and use it in GitHub Desktop.
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