Advertisement
Guest User

Untitled

a guest
Jun 7th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.61 KB | None | 0 0
  1. % Nub remove the repeated elements of a list
  2. nub(X) -> nub(X, []).
  3.  
  4. nub([], XUnique) -> XUnique;
  5. nub([X|Xs], XUnique) ->
  6.     PositionWhereXFound = find(X, XUnique),
  7.     if
  8.         PositionWhereXFound > -1 ->
  9.             nub(Xs, XUnique);
  10.         true -> % 'else'
  11.             nub(Xs, XUnique ++ [X])
  12.     end.
  13.  
  14. % Find the item in a list
  15. % Return the position where the first occurrence of Item is found
  16. % Otherwise, return -1
  17. find(_, []) -> -1;
  18. find(Item, X) -> find(Item, X, 0).
  19. find(Item, [Item|_Xs], Position) -> Position;
  20. find(Item, [_X|Xs], Position) -> find(Item, Xs, Position + 1);
  21. find(_, [], _) -> -1.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement