0

I have a question I want to do the addition of 2 matrix by row in erlang, I'm trying to apply the code for Haskell:

add :: Num a => [[a]] -> [[a]] -> [[a]]
add = zipWith $ zipWith (+)

I did something like this:

add([[]],[[]]) -> []
add = zipWith $ zipWith (+)

but it get's an error, with the $, I'm really confused. How can I do this in erlang?

And works in this way:

  add([[ 1, 2 ],[ 3 , 4 ]] , [[ 4 , 5 ],[ 6 , 7 ]] ).

Result:

[[ 6, 8], [ 10, 12]]
Luiz Miranda
  • 128
  • 8
  • How are define your matrixes? –  Nov 13 '17 at 18:45
  • That's not even vaguely Erlang syntax. Can you give more detail about what you're trying to do? – Roger Lipscombe Nov 13 '17 at 18:47
  • The matrixes is by row, an input from the console, something like this: add([[1,2],[3,4]],[[4,5],[6,7]]). Which is matrix A (1 2) (3 4) Matrix B (3 4) (4 5) – Luiz Miranda Nov 13 '17 at 18:50
  • so, your Q split into 2 part - extracting rows from matrix (list) and write elementwise sum into new one, right? –  Nov 13 '17 at 18:53
  • Yes, Is that I was planning on add the first list add with the second one, using something like map, but I don't know how to implement it. – Luiz Miranda Nov 13 '17 at 19:01
  • so let's start solve first part - write function s_print/2 which take as arguments 2 matrix and print corresponding to adding rows. After that - write function sum/2 which take 2 list and return list with elementwise sum. And after that you need just union this two function. –  Nov 13 '17 at 19:06

2 Answers2

3

This is the direction translation of your Haskell function to Erlang:

add(Xss, Yss) ->
  lists:zipwith(fun(Xs, Ys) -> lists:zipwith(fun(X, Y) -> X + Y end, Xs, Ys) end, Xss, Yss).

(+) becomes fun(X, Y) -> X + Y end and since Erlang doesn't have anything like the $ operator or automatic partial application of functions, we need to name all arguments and explicitly pass them to lists:zipwith.

It works as expected:

1> a:add([[1,2],[3,4]],[[4,5],[6,7]]) == [[5, 7], [9, 11]].
true
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • Thanks, but How can I retrieve the result: add([[ 1, 2 ],[ 3 , 4 ]] , [[ 4 , 5 ],[ 6 , 7 ]] ). RESULT [[ 6, 8], [ 10, 12]] – Luiz Miranda Nov 14 '17 at 00:33
  • 2
    Your Haskell code returns the same value as my answer. Why should it return `[[6, 8], [10, 12]]`? – Dogbert Nov 14 '17 at 07:56
  • It has a mistake, I test it, but I do not why it does not print the second part. a:add([[1,2],[3,4]],[[4,5],[6,7]]). RESULT: [[5,7],"\t\v"] – Luiz Miranda Nov 14 '17 at 22:42
  • In Erlang, `"\t\v" == [6, 7]`. See https://stackoverflow.com/q/32670978/320615 for more info. – Dogbert Nov 15 '17 at 10:29
1
1> shell:strings(false).            
true
2> AddRow = fun(X, Y) -> lists:zipwith(fun erlang:'+'/2, X, Y) end.
#Fun<erl_eval.12.99386804>
3> Add = fun(X, Y) -> lists:zipwith(AddRow, X, Y) end.             
#Fun<erl_eval.12.99386804>
4> Add([[1,2],[3,4]],[[4,5],[6,7]]).                               
[[5,7],[9,11]]
Hynek -Pichi- Vychodil
  • 26,174
  • 5
  • 52
  • 73