7stud

7stud

Metaprogramming Elixir book: what does this mean?

On p. 6, there is this example:

defmodule Math do

  # {:+, [context: Elixir, import: Kernel], [5, 2]}
  defmacro say({:+, _, [lhs, rhs]}) do   #line 4
    quote do
      lhs = unquote(lhs)
      rhs = unquote(rhs)
      result = lhs + rhs
      IO.puts "#{lhs} plus #{rhs} is #{result}"
      result
    end
  end

  # {:*, [context: Elixir, import: Kernel], [8, 3]}
  defmacro say({:*, _, [lhs, rhs]}) do   #line 15
    quote do
      lhs = unquote(lhs)
      rhs = unquote(rhs)
      result = lhs * rhs
      IO.puts "#{lhs} times #{rhs} is #{result}"
      result
    end
  end
end

The text says:

Let’s break down the code. Since we know macros receive the AST representation of the arguments we pass to them, we pattern matched directly on the AST to determine which say definition to invoke. On lines 4 and 15, we can see that macros , like functions, can have multiple signatures. Having the example representation from our quoted results allowed us to easily bind the left- and right-hand side values to variables and print a message accordingly. Too complete the macro, we used quote to return an AST for the caller to replace our Math.say invocations. Here we also used unquote for the first time…

What in the world does:

Having the example representation from our quoted results allowed us to easily bind the left- and right-hand side values to variables and print a message accordingly.

mean?! I recognize the words as English, but they have no intelligible meaning to me.

Most Liked

7stud

7stud

Ahh, I figured it out. The phrase “Having the example representation from our quoted results” refers to:

 # {:+, [context: Elixir, import: Kernel], [5, 2]}

The “quoted results” are on the previous page where the book used quote() on some math expressions.

chrismccord

chrismccord

Creator of Phoenix

can confirm :slight_smile:

blatyo

blatyo

Conduit Core Team

That is my understanding as well. Just for some confirmation.

If you’re looking at the commented out representation of the AST and then imagine how that will be matched against the function head, it’s easy to see how 5 and 2 will be bound to the right-hand and left-hand variables.

7stud

7stud

lol. Thanks!

I would, however, suggest a rewrite. I read that sentence 20 times, and I couldn’t figure out what it meant until just after I posted my question (as sometimes happens!). Maybe something as simple as:

With the AST in the comments as a guide we were easily able to bind the left- and right-hand side values to variables and print a message accordingly .

Where Next?

Popular in Chat/Questions Top

Scoty
Hey, I am currently reading Programming Elixir and I am doing one of the exercises where you should write a solution to the “I’m thinkin...
New
Allyedge
Hey, I want to learn Elixir OTP and I wanted to know if there are any good resources that teach it. I found some web pages, but none of t...
New
maqbool
what books/Resources do you recommend to learn about distributed system(theory)?
New
imanuelgittens
Hey all! Just joined the forum and I’m very excited to learn the language. I just finished reading the documentation and I was wondering ...
New
g5becks
Hello everyone. So I just got done reading all of the introduction to Elixir and working through the basics of the language. I am not new...
New
ericmachine88
Hi all, I am currently on this course Half a way thru, and struggled a bit… sometimes I don’t even know what I am coding especially ...
New
AstonJ
It’s been a while since we asked this - I’m sure others (especially newcomers to the language) will be interested to hear how you’ve all ...
New
Twfo326
As a novice dev I’m trying to keep the curriculum as lean as possible. My requirements are modest: build simple CRUD apps with Phoenix...
New
venomnert
Background I have been a backend elixir developer for about 3 years now. I have been mainly working on simple CRUD applications. Context...
New
SavagePixie
I was wondering if there are any beginner-friendly, exercise-based resources for learning Elixir out there. I’m looking for something lik...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement