-module (lab2). -compile(export_all). % Part 1 copy(X) -> X. length1(L) -> length(L). length2(L) -> length(lists:flatten(L)). append(X, Y) -> X ++ Y. remove1([], _) -> []; remove1([H|T], Key) -> case H of Key -> remove1(T, Key); _ -> [H|remove1(T, Key)] end. remove2([], _) -> []; remove2([H|T], Key) -> case is_list(H) of true -> [remove2(H, Key)|remove2(T, Key)]; _ -> case H of Key -> remove2(T, Key); _ -> [H|remove2(T, Key)] end end. removef1(X, Y) -> X -- Y. removef2(List, Key) -> removef2(List, Key, false). removef2([], _, _) -> []; removef2([H|T], Key, Found) -> case is_list(H) of true -> [removef2(H, Key, false)|removef2(T, Key, Found)]; _ -> case Found of true -> [H|removef2(T, Key, Found)]; _ -> case H of Key -> removef2(T, Key, true); _ -> [H|removef2(T, Key, Found)] end end end. removel1(L) -> lists:sublist(L,length(L)-1). removel2(L) -> removel1(lists:flatten(L)). substitue1(E, L) -> remove1(L, [E]). substitue2(E, L) -> remove2(L, [E]). reverse1(L) -> lists:reverse(L). reverse2(L) -> Fun = fun(X) -> case is_list(X) of true -> reverse2(X); _ -> X end end, lists:reverse(lists:map(Fun, L)). first_atom(L) -> hd(lists:flatten(L)). last(L) -> lists:last(L). add_if_none(E, L) -> case lists:member(E, L) of true -> L; _ -> L ++ [E] end. collect(L) -> lists:sort(L). flatten(L) -> lists:flatten(L). revl([H|[]]) -> [H]; revl([H|T]) -> [revl(T)] ++ [H]. devlev1([H|[]]) -> [H]; devlev1([H|T]) ->[H|[devlev1(T)]]. devlev2([H|[]]) -> [H]; devlev2(L) -> [devlev2(lists:sublist(L, length(L)-1))] ++ [lists:last(L)]. destlev1([H|[]]) -> [H]; destlev1([H|T]) -> [H|destlev1(hd(T))]. destlev2([H|[]]) -> [H]; destlev2([H|T]) -> destlev2(lists:sublist(H, length(H))) ++ [lists:last(T)]. remsec(L) -> lists:reverse(remsec(L, 1, [])). remsec([], _, Acc) -> Acc; remsec([H|T], N, Acc) -> case N rem 2 of 0 -> remsec(T, N+1,[ H|Acc]); 1 -> remsec(T, N+1, Acc) end. devpair(List) -> devpair(List, []). devpair([A,B|Tail], Acc) -> devpair(Tail, [{A,B}|Acc]); devpair([], Acc) -> lists:reverse(Acc). mix([H1|[]], [H2|[]]) -> [H1, H2]; mix([H1|T1], [H2|T2]) -> [H1, H2|mix(T1, T2)]. %alternate mix2(X, Y) -> mix2(X, Y, []). mix2([H1|T1], [H2|T2],Acc) -> mix2(T1, T2, [H1,H2|Acc]); mix2([],[], Acc) -> lists:reverse(Acc). depth(L) -> depth(L, 1, 1). depth([], N, Max) -> if N > Max -> N; true -> Max end; depth([H|T], N, Max) -> if is_list(H) -> M = depth(H, N+1, Max); true -> M = N end, depth(T, N, M). % Part 3 forall(_, []) -> true; forall(Fun,[H|T]) -> case Fun(H) of false -> false; _ -> forall(Fun, T) end. forsome(_, []) -> false; forsome(Fun, [H|T]) -> case Fun(H) of true -> true; _ -> forsome(Fun, T) end. forodd(Fun, List) -> length(lists:filter(Fun, List)) rem 2 == 1. atomlist(List) -> length(lists:filter(fun(X) -> is_atom(X) end, List)) == length(List). listp(List) -> is_list(List). onelevel([]) -> true; onelevel([H|T]) -> if is_list(H) -> false; true -> onelevel(T) end. % Part 4 order(X, Y, List) -> order(X,Y,List, false). order(_, _, [], _) -> false; order(X, Y, [H|T], Found) -> case H of X -> Found == false; Y -> false; _ -> order(X, Y, T, Found) end. order1(X, Y, List) -> order1(X,Y,List, false). order1(_, _, [], true) -> false; order1(_, _, [], false) -> orderundef; order1(X, Y, [H|T], Found) -> case H of X -> Found == false; Y -> false; _ -> order1(X, Y, T, Found) end. % Drugs lexorder([H1|T1], [H2|T2], List) -> if H1 =:= H2 -> lexorder(T1, T2, List); true -> order1(H1, H2, List) end. % Hard drugs lexorder1([H1|T1], [H2|T2], List) -> if H1 =:= H2 -> lexorder1(T1, T2, List); true -> order1(H1, H2, List) end. first([H1|T1], List) -> first([H1|T1], List, H1). first([], _, First) -> First; first([H1|T1], List, First) -> case lists:member(H1, List) of true -> H1; _ -> first(T1, List, First) end. rank(List1, List2) -> lists:sort(fun(X,Y) -> rank1(X, Y, List2) end, List1). rank1(_, _, []) -> false; rank1(X, Y, [H|T]) -> case H of X -> true; Y -> false; _ -> rank1(X,Y,T) end. % Part 5 possessing(Fun, List) -> lists:filter(Fun, List). suchthat(_, []) -> false; suchthat(Fun, [H|T]) -> case Fun(H) of true -> H; _ -> suchthat(Fun, T) end. suchthat1(_, [], X, _) -> X; suchthat1(Fun, [H|T], X, Y) -> case Fun(H) of true -> Y; _ -> suchthat1(Fun, T, X, Y) end. suchthat2(_, [], _) -> false; suchthat2(Fun, [H|T], Fun2) -> case Fun(H) of true -> Fun2(T); _ -> suchthat2(Fun, T, Fun2) end. first_coin([], _) -> false; first_coin([H|T],L) -> case lists:member(H, L) of true -> H; _ -> first_coin(T, L) end. % Part 6 setof(L) -> lists:usort(L). diflist(X, Y) -> sofs:difference(sofs:set(X), sofs:set(Y)). subset(X, Y) -> sofs:is_subset(sofs:set(X), sofs:set(Y)). union(X, Y) -> X ++ Y. lunion(L) -> lists:merge(L). intersection(X, Y) -> sofs:to_external(sofs:intersection(sofs:set(X), sofs:set(Y))). equalset(X, Y) -> sofs:set(X) == sofs:set(Y). cart(X, Y) -> [[A,B] || A <- X, B <- Y]. cart2(X, Y) -> sofs:to_external(sofs:product(sofs:set(X), sofs:set(Y))). setp(L) -> length(L) == sets:size(sets:from_list(L)). simdiff(X, Y) -> sofs:to_external(sofs:symdiff(sofs:set(X), sofs:set(Y))). % Part 7 pair(X, Y) -> lists:zip(X, Y). assoc(Key, List) -> {Key, dict:fetch(Key, dict:from_list(List))}. pairlis(X, Y, L) -> pair(X, Y) ++ L. subst(X, Y, Z) -> Fun = fun(_, Value) -> case is_list(Value) of true -> subst(X, Y, Value); _ -> case Value of Y -> X; _ -> Y end end end, dict:to_list(dict:map(Fun,dict:from_list(Z))). sub2(List, Key) -> case dict:find(Key, dict:from_list(List)) of {ok, Value} -> Value; error -> Key end. sublis(X, Y) -> Fun = fun(Key, Value) -> Result = sub2(X, Key), case Result of Key -> Value; _ -> Result end end, dict:to_list(dict:map(Fun, dict:from_list(Y))). sassoc(List, Key) -> sub2(List, Key). % Part 8