Reputation: 65
Anyone who can help.
I am a beginner in Elixir Language. I am trying to perform an “unzip
” operation and the “iex
” environment shows syntax error “syntax error before: ')'
” at position “53
” of the script, I am writing. I know the algorithm works because I tested it on Haskell, but I cannot find where my syntax error is and what to do to resolve it.
This
defmodule Alf do
def lastx([]), do: raise ArgumentError, message: "empty list"
def lastx([x]), do: x
def lastx([x | xs]), do: lastx(xs)
def initx([]), do: raise ArgumentError, message: "empty list"
def initx([x]), do: []
def initx([x | xs]), do: [x | initx(xs)]
def revx([]), do: []
def revx(xs), do: [lastx(xs) | revx(initx(xs))]
def zipx([], b), do: []
def zipx(a, []), do: []
def zipx([x | a], [y | b]), do: [{x, y} | zipx(a, b)]
def fstx(x, _), do: x
def sndx(_, y), do: y
end
Here is the part that is not working properly:
defmodule Alf do
def unzipx_bs(xs, ys, []), do: (revx(xs), revx(ys))
def unzipx_bs(xs, ys, [z | zs]), do: unzipx_bs([fstx z | xs], [sndx z | ys], zs)
end
The “unzipx_bs
” script will later be set to private and will have a script called “unzipx” as the access interface that will perform the so-called “unzipx_bs [], [], [{1, 4}, {2, 5}, {3 , 6}]
” to generate a result similar to “[1, 2, 3], [4, 5, 6]
”.
Upvotes: 1
Views: 87
Reputation: 23184
I assume you wanted your unzipx_bs
function to return a pair. Tuples are written with {}
in elixir, so it should be {revx(xs), revx(ys)}
. I would further propose that you unpack your z
right there in the argument list, giving:
def unzipx_bs(xs, ys, []), do: {revx(xs), revx(ys)}
def unzipx_bs(xs, ys, [{x, y} | zs]), do: unzipx_bs([x | xs], [y | ys], zs)
Then:
Alf.unzipx_bs([], [], [{1, 2}, {3, 4}, {5, 6}])
# => {[1, 3, 5], [2, 4, 6]}
Upvotes: 2