15 Jul 2013, 03:42
Dave_gnome_head_isolated_pragsmall

Dave Thomas (338 posts)

  • (Hard). Try adding a method definition with a guard clause to the Test module. You’ll find that the tracing now longer works.

    • Find out why
    • See if you can fix it

    (You may need to explore Kernel.def/4)

01 May 2014, 23:28
Ernie2_pragsmall

Ernie Miller (4 posts)

I struggled with this one for a half an hour or so. So:

If you need further help, here’s a function with a guard:

def add_list_with_guard(list)
when length(list) > 3 do
  Enum.reduce(list, 0, &(&1 + &2))
end

…and here is its representation before unquoted:

{:when, [line: 37],
 [{:add_list_with_guard, [line: 36], [{:list, [line: 36], nil}]},
  {:>, [line: 37], [{:length, [line: 37], [{:list, [line: 37], nil}]}, 3]}]}

Consider what the pattern matching in your macro is then considering its definition “name” and “args”. Then, consider whether or not you might be able to define a clause for the function that takes this into account.

  You must be logged in to comment