Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(task4).
- -export([reverse/1, reverse/3, listLength/1, listLength/2, nth/2, seq/2, seq/5]).
- listLength(Xs) -> listLength(Xs, 0).
- listLength([], Acc) -> Acc;
- listLength( [_|Xs], Acc) -> listLength(Xs, Acc + 1).
- reverse(Xs) -> reverse(Xs, listLength(Xs), []).
- reverse(_, 0, Ys) -> Ys;
- reverse([X|Xs], Acc, Ys) -> reverse(Xs , Acc - 1, [X|Ys]).
- nth(1, [X|_]) -> X ;
- nth (Acc, [_|Xs]) -> nth (Acc - 1, Xs).
- seq (X, Y) -> seq (X, Y, 1, [], 0).
- seq (X, Y, Acc, XtoY, _) when length(XtoY) == ((Y-X+Acc) div Acc) -> reverse(XtoY);
- 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) ) ->
- seq(X, Y, Acc, [X], I+1);
- 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) ) ->
- seq(X, Y, Acc, [X+Acc*I|XtoY], I+1).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement