As you may remember from my last post I’m current reading Structure and Interpretation of Computer Programs as part of a study group we started during Pablo’s Fiesta.

I continue to find Lisp to be a fascinating language especially this really interesting piece of code.

```
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
```

Ok so what’s going on here?

- if b > 0
- return +

- else
- return -

In my previous post I demonstrated how you might implement this in javascript. Well I found it so interesting that I decided to find out if I could do it in C# as well. The following is my attempt at trying to return the + or – symbol from an if statement.

```
[TestFixture]
public class ExpressionTreeFixture
{
[Test]
public void a_plus_abs_b_should_equal_9()
{
a_plus_abs_b(5, -4).ShouldEqual(9);
}
private int a_plus_abs_b(int a, int b)
{
var operation = b > 0
? ExpressionType.Add
: ExpressionType.Subtract;
var maths = Expression
.MakeBinary(operation,
Expression.Constant(a),
Expression.Constant(b));
return Expression.Lambda<Func<int>>(maths).Compile()();
}
}
```

“Why would you write it this way when (insert code here) works better?”

Shut up troll! I’m not telling anybody to go off and write their code in expression trees when the following code is clear the correct why to write it.

```
if (b > 0) return a + b;
return a - b;
```

See that’s way easier, but this isn’t a post about smart things to do with expression trees now is it?

Let’s up the silliness some more and make it an extension method just for fun.

```
[TestFixture]
public class ExpressionTreeFixture
{
[Test]
public void a_plus_abs_b_should_equal_9()
{
5.PlusAbs(-4).ShouldEqual(9);
}
}
public static class IntExtension
{
public static int PlusAbs(this int a, int b)
{
var operation = b > 0
? ExpressionType.Add
: ExpressionType.Subtract;
var maths = Expression
.MakeBinary(operation,
Expression.Constant(a),
Expression.Constant(b));
return Expression.Lambda<Func<int>>(maths).Compile()();
}
}
```

I was having so much fun with this that I even roped some of my teammates in on it. Heres Chad Myers solution.

```
public int add(int a, int b)
{
return a + b;
}
public int subtract(int a, int b)
{
return a - b;
}
public Func<int, int> boo(int b)
{
var plus = new Func<int, int>(a => add(a, b));
var minus = new Func<int, int>(a => subtract(a, b));
return (b > 0) ? plus : minus;
}
```

So I think its cool that you can *kind of* return + or – from an if statement, but what started as a fun little exercise turned into an amazing adventure into learning expression trees.

So as my final brain beating exercise I tried make this into a Func<int,int,int> completely build using expression trees.

```
public Func<int,int,int> a_plus_abs_b ()
{
ParameterExpression a = Expression.Parameter(typeof (int), "a");
ParameterExpression b = Expression.Parameter(typeof (int), "b");
var greaterThanZero = Expression.MakeBinary(ExpressionType.GreaterThan, b, Expression.Constant(0));
var maths = Expression.Condition(greaterThanZero,
Expression.MakeBinary(ExpressionType.Add, a, b),
Expression.MakeBinary(ExpressionType.Subtract, a, b));
return Expression.Lambda<Func<int, int, int>>(maths, a, b).Compile();
}
```

So here’s a little challenge. In the comment section I invite you to completely school me in the ways of Expression Trees. Try and come up with a way more awesome version of a_plus_abs_b using expression trees or IL or whatever awesome sauce is in the .NET framework that I’m not aware of.

Herp Derp,

-Ryan