Skip to content

Instantly share code, notes, and snippets.

@aymanosman
Created March 8, 2017 10:38
Show Gist options
  • Save aymanosman/ebd1a78a18bd5465ca694239ce27472c to your computer and use it in GitHub Desktop.
Save aymanosman/ebd1a78a18bd5465ca694239ce27472c to your computer and use it in GitHub Desktop.
# Sexp equivalent, modulo infix arithmetic expression
# (defmodule M
# (def foo (lol)
# 2+lol))
# Starting from the "true", most verbose form and applying syntactic rules to
# obtain the final form
# "true" expression, notice there are no vararg functions
defmodule(M,
[{:do, def(foo(lol),
[{:do, (2+lol)}])}])
# keyword-list
# Lists of {<symbol>, <term>} pairs qualify for the following transformation
### [{:a, :b}, {:c, :d}] -> [a: :b, c: :d] # notice the placement of the colons
# An expression of the form right of the arrow is called a "keyword-list"
defmodule(M,
[do: def(foo(lol),
[do: (2+lol)])])
# last-argument
# If the last argument of a function application is a keyword-list, the
# following rule applies.
### f(e1, e2, [a: e3, b: e4]) -> f(e1, e2, a: e3, b: e4)
defmodule(M,
do: def(foo(lol),
do: (2+lol)))
# space-apply
# f(a, b, c) -> f a, b, c
defmodule M,
do: def foo(lol),
do: (2+lol)
# do-block -- This is the most confusing transformation
### f a, b, do: e -> f a, b do e end
# applying it, one-step-at-a-time
defmodule M do def foo(lol), do: (2+lol) end
defmodule M do def foo(lol) do lol end end
# this is most useful for splitting things into multiple lines, so:
defmodule M do
def foo(lol) do
2+lol
end
end
# and we have our final form
# tk: explain how blocks work
# Further explaination of do-block
# The utility of the do-block comes from allowing sequences of expression to be
# layed out across multiple lines.
# do: a; b; c ->
# do
# a
# b
# c
# end
# These expression sequences seperated by lines are called a "block".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment