Collection
Unlock This Episode
Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.
Introduction
Today we are going to talk about a concept known as “domain-specific languages”, and in particular “embedded domain-specific languages”. It may sound like a jargony term, but it’s something that you have definitely come across and you may even use it on a daily basis.
After giving the upfront definitions so that we all understand what a domain-specific language is, we will create one right in Swift and progressively add more and more advanced features to it. It’s a toy example, but it contains a lot of the core ideas and it can be a lot of fun to play with.
Subscribe to Point-Free
Access this episode, plus all past and future episodes when you become a subscriber.
Already a subscriber? Log in
Exercises
Improve the
simplify
function to also recognize the following patterns:- Factorize the
c
out of this expression:a * c + b * c
. - Reduce
1 * a
anda * 1
to justa
. - Reduce
0 * a
anda * 0
to just0
. - Reduce
0 + a
anda + 0
to justa
. - Are there any other simplification patterns you know of that you could implement?
- Factorize the
Enhance
Expr
to allow for any number of variables. Theeval
implementation will need to change to allow passing values in for all of the variables introduced.Implement infix operators
*
and+
to work onExpr
to get rid of the.add
and.mul
annotations.Implement a function
varCount: (Expr) -> Int
that counts the number of.var
’s used in an expression.Write a pretty printer for
Expr
that adds a new line and indentation when printing the sub-expressions inside.add
and.mul
.