Elixir - Passing multiple lists as function parameters -
i new in elixir google searches , reading have not found me solution current problem. have seen examples in books , tutorials passing in single list parameter function so: [ head|tail ].
defmodule math def double_each([head|tail]) [head * 2| double_each(tail)] end def double_each([]) [] end end
i need pass in 2 lists , perform actions on them, so:
defmodule pagination def getpaginatedlist([], _, _, _) [] end def getpaginatedlist([list], [paginated], f, l) when f == 1 enum.take(list, l) end def getpaginatedlist(_, [paginated], f, l) when l <= f paginated end def getpaginatedlist([list], [paginated] \\ [], f, l) getpaginatedlist(tl(list), paginated ++ hd(list), f, l - 1) end end
however, receive error function clause not matching when passing in list , list b in error below:
iex(23)> pagination.getpaginatedlist(a, b, 1, 2) ** (functionclauseerror) no function clause matching in pagination.getpaginatedlist/4 pagination.ex:2: pagination.getpaginatedlist([1, 2, 3, 4, 5], [], 1, 2)
any ideas on may doing incorrectly? (apologies if simple question answer. have not been able find answer after few hours of searching , playing around this.)
your issues list argument pattern match. list
, paginated
arguments, don't need wrap them in list, since you'd matching on nested list. also, it's conventional use snake case in elixir function names. try giving shot:
defmodule pagination def get_paginated_list([], _, _, _) [] end def get_paginated_list(list, paginated, f, l) when f == 1 enum.take(list, l) end def get_paginated_list(_, paginated, f, l) when l <= f paginated end def get_paginated_list(list, paginated \\ [], f, l) get_paginated_list(tl(list), paginated ++ hd(list), f, l - 1) end end
Comments
Post a Comment