Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.87 KB | None | 0 0
  1. -module(task4).
  2.  
  3. -export([reverse/1, reverse/3, listLength/1, listLength/2, nth/2, seq/2, seq/5]).
  4.  
  5. listLength(Xs) -> listLength(Xs, 0).
  6.  
  7. listLength([], Acc) -> Acc;
  8.     listLength( [_|Xs], Acc) -> listLength(Xs, Acc + 1).
  9.  
  10. reverse(Xs) -> reverse(Xs, listLength(Xs), []).
  11.  
  12. reverse(_, 0, Ys) -> Ys;
  13.     reverse([X|Xs], Acc, Ys) -> reverse(Xs , Acc - 1, [X|Ys]).
  14.  
  15. nth(1, [X|_]) -> X ;
  16.     nth (Acc, [_|Xs]) -> nth (Acc - 1, Xs).
  17.  
  18. seq (X, Y) -> seq (X, Y, 1, [], 0).
  19.  
  20. seq (X, Y, Acc, XtoY, _) when length(XtoY) == ((Y-X+Acc) div Acc) -> reverse(XtoY);
  21.     seq (X, Y, Acc, [], I) when ( (Y>(X-Acc)) and (Acc > 0) ) or ( (Y < (X-Acc)) and ((Acc < 0)) ) or ( (Acc == 0) and (X==Y) ) ->
  22.         seq(X, Y, Acc, [X], I+1);
  23.     seq (X, Y, Acc, XtoY, I) when ( (Y>(X-Acc)) and (Acc > 0) ) or ( (Y < (X-Acc)) and ((Acc < 0)) ) or ( (Acc == 0) and (X==Y) ) ->
  24.         seq(X, Y, Acc, [X+Acc*I|XtoY], I+1).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement