-1

i realy need help with this assignment that goes as follow: i need to implement this function using map family.

mapSub(List1,List2).

Subtracts List2 from List1  
Follows the order of appearance in
List2.  
Implement it using map family 
Examples:    
mapSub([1,2,3,4,5,1,2,3,4,5],[1,1,2]).  
[2,3,4,5,3,4,5]  
mapSub ([1,2,3,4,5,1,2,3,4,5],[1,1,2,2]).  
[2,3,4,5,3,4,5]

the problem is how can i menage List2 with the functions i allowed to use,
for exmple if i find the first element from List2 and delete his first appearance in List1.
how can i replace the element i look for
to the head of List2 tail ( the next element in List2)
thank you.

ERV
  • 1
  • 1
    I think you are confusing maps with lists. A map in Erlang is a hashtable of associations `Key` => `Value`, e.g. `#{a => 2, b => 3, c=> 4, "a" => 1, "b" => 2, "c" => 4}`. In your question there are two lists. Please rewrite the question because for now it can't be answered properly (and should probably be closed). – Greg Apr 28 '16 at 08:46
  • @Amiramix: You think map family doesn't mean `lists:map/2` but `maps`? Now it makes a little bit sense. – Hynek -Pichi- Vychodil Apr 28 '16 at 12:23
  • 1
    I don't really know what map family may mean, however `lists:map/2` doesn't have much to do with Erlang maps. It's simply an operation that converts a list by processing each element on that list by the specified function, see map-reduce on Wikipedia: https://en.wikipedia.org/wiki/MapReduce There is also a new data type in Erlang called maps introduced in R17 about which you can read more here: http://learnyousomeerlang.com/maps You will need to decide which one is the map family you are after. – Greg Apr 28 '16 at 12:41
  • BTW `[3,4,5,2,3,4,5] = [1,2,3,4,5,1,2,3,4,5] -- [1,1,2]` and `[3,4,5,3,4,5] = [1,2,3,4,5,1,2,3,4,5] -- [1,1,2,2]` so you don't even have your examples right. – Hynek -Pichi- Vychodil Apr 28 '16 at 12:41

2 Answers2

0
mapSub(L1, L2) ->
    Inc = fun(X, Map) ->
                  Map#{X => maps:get(X, Map, 0) + 1}
          end,
    Map = lists:foldl(Inc, #{}, L2),
    sub(L1, Map).

sub([], _) -> [];
sub([H|T], Map) ->
    case maps:get(H, Map, 0) of
        0 ->
            [H | sub(T, Map)];
        N ->
            sub(T, Map#{H => N - 1})
    end.

test() ->
    L1 = [1,2,3,4,5,1,2,3,4,5],
    ok = check(L1, [1,1,2]),
    ok = check(L1, [1,1,2,2]).

check(L1, L2) ->
    Expect = L1 -- L2,
    case mapSub(L1, L2) of
        Expect -> ok;
        Error -> {error, [Expect, Error]}
    end.

BTW your Example results are wrong or you mean something totally different by subtract than --.

Hynek -Pichi- Vychodil
  • 26,174
  • 5
  • 52
  • 73
-1
-module(wy).
-compile(export_all).


main() ->
    L1 = [1,2,3,4,5,1,2,3,4,5],
    L2 = [1,1,2],
    [2,3,4,5,3,4,5] = mapSub(L1, L2),
    [2,3,4,5,3,4,5] = mapSub([1,2,3,4,5,1,2,3,4,5],[1,1,2,2]).

mapSub(L1, L2) ->
    mapSub(L1, L2, []).

mapSub([], _, Res) ->
    lists:reverse(Res);
mapSub(L1, [], Res) ->
    lists:reverse(Res) ++ L1;
mapSub([H1 | T1], [H1 | T2], Res) ->
    mapSub(T1, T2, Res);
mapSub([H1 | T1], [H2 | _] = L2, Res) when H1 /= H2->
    mapSub(T1, L2, [H1 | Res]).

You can try this code.

Your issue is a typical recursion.

BlackMamba
  • 10,054
  • 7
  • 44
  • 67