I am trying to learn Expression Trees in C#. Can someone help me solve this puzzle found in Math Logic Puzzles by Kurt Smith ISBN 0-8069-3864-1 in C# code using Expression Trees link to First Printing Edition? Or, could it be, that this puzzle is best suited for a different approach? Am I trying to put a slipper on an Elephants foot?
Here is the code I have so far
using System.Collections.Generic;
using System.Linq.Expressions;
namespace ExpTreesBasics1
{
class Program
{
List<Person> ListOfPersons = new List<Person>();
static void Main(string[] args)
{
Person _paul = new Person()
{
Mountain = new Mountain(),
Pack = new Pack() { WeightExpression = Expression.Constant(30) }
};
Person _daleDorsey = new Person() {Mountain = null, Pack = new Pack() {Weight = 0}};
Person _jimMcGee = new Person() {Mountain = null, Pack = new Pack() {Weight = 0}};
Person _geraldBrown = new Person()
{
Mountain = new Mountain() {Height = 124},
Pack = new Pack() { WeightExpression = Expression.Subtract(Expression.Constant(_daleDorsey.Pack.Weight), Expression.Constant(_jimMcGee.Pack.Weight)) }
};
Person _andyStiller = new Person()
{
Mountain = new Mountain()
{
HeightExpression = Expression.Add(Expression.Constant(865), Expression.Constant(_geraldBrown.Mountain.Height))
},
Pack = new Pack() { WeightExpression = Expression.Divide(.5, Expression.Lambda(Person))}
};
var result = Expression.Add(Expression.Constant(865), Expression.Constant(_geraldBrown.Mountain.Height));
}
}
public class Person
{
public Mountain Mountain { get; set; }
public Pack Pack { get; set; }
}
public class Mountain
{
public string Name { get; set; }
public int Height { get; set; }
public Expression HeightExpression { get; set; }
}
public class Pack
{
public int Weight { get; set; }
public Expression WeightExpression { get; set; }
}
}
Here's a brute force solution in F#. Not sure this is really the best way of doing it - it works here because the set of combinations of first name / last name / weight / mountain is relatively small (about 1.7m permutations) - but at least it works :)
Posted as a gist because it's a reasonably large code snippet.