O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Your Turn

  • Exercise: MacrosAndCodeEvaluation-2

    Write a macro called times_n that takes a single numeric argument. It should define a function called times_n in the caller’s module that itself takes a single argument, and that multiplies that argument by n. So, calling times_n(3) should create a function called times_3, and calling times_3(4) should return 12. Here’s an example of it in use:

     
    defmodule​ Test ​do
     
    require​ Times
     
    Times.times_n(3)
     
    Times.times_n(4)
     
    end
     
     
    IO.puts Test.times_3(4) ​#=> 12
     
    IO.puts Test.times_4(5) ​#=> 20

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required