{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Fun with Trees\n", "\n", "In this homework, let us experiment with creating and parsing expression trees in Julia." ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tree (generic function with 6 methods)" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tree(v::Tuple) = tree(v...)\n", "tree(a,b) = throw(\"Need an odd number of arguments\") # throw an error\n", "tree(a) = throw(\"Need at least three arguments\")\n", "tree(a,b,c) = Expr(:call,a,b,c)\n", "tree(v...) = Expr(:call,v[1],tree(v[2:end-1]),v[end])" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tree(:*, :+, :^, :x, 3, :y, 4) = :((x ^ 3 + y) * 4)\n", "tree(:*, :^, :+, :x, :y, 3, 4) = :((x + y) ^ 3 * 4)\n", "tree(:*, :^, :+, :x, 3, :y, 4) = :((x + 3) ^ y * 4)\n" ] } ], "source": [ "@show tree(:*,:+,:^,:x,3,:y,4)\n", "# Interchange args 2&3, and 5&6\n", "@show tree(:*,:^,:+,:x,:y,3,4);\n", "@show tree(:*,:^,:+,:x,3,:y,4);" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [], "source": [ "#using Pkg\n", "#Pkg.add(\"Latexify\")\n", "using Latexify" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See the [latexify and how Julia's metaprogramming makes it so useful](https://www.youtube.com/watch?v=wpV0Nz-93Hk) video from juliacon!" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\left( x^{3} + y \\right) \\cdot 4$" ], "text/plain": [ "L\"$\\left( x^{3} + y \\right) \\cdot 4$\"" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "latexify(tree(:*,:+,:^,:x,3,:y,4) )" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\left( x + y \\right)^{3} \\cdot 4$" ], "text/plain": [ "L\"$\\left( x + y \\right)^{3} \\cdot 4$\"" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "latexify(tree(:*,:^,:+,:x,:y,3,4) )" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\frac{\\left( x^{3} + y \\right) \\cdot 4}{\\left( x + y \\right)^{3} \\cdot 4}$" ], "text/plain": [ "L\"$\\frac{\\left( x^{3} + y \\right) \\cdot 4}{\\left( x + y \\right)^{3} \\cdot 4}$\"" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "latexify(Expr(:call,:/,tree(:*,:+,:^,:x,3,:y,4),tree(:*,:^,:+,:x,:y,3,4)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise: \n", "\n", "Build a [reverse polish](https://en.wikipedia.org/wiki/Reverse_Polish_notation_) calculator in julia." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "My high school math teacher [(Gil Kessler)](https://www.wnyc.org/story/302441-where-math-teachers-go-to-get-energized)\n", "proudly showed me his reverse Polish HP calculator at a time when I had only heard of Texas Instruments. I believe it was a model like this [HP 25](https://en.wikipedia.org/wiki/HP-25)\n", "I thought reverse Polish was very strange, but he insisted it was way faster (fewer keystrokes)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```julia\n", "\n", "#Answer:\n", "\n", "#reverse_polish(v::Tuple) = \n", "#reverse_polish(a,b,c) =\n", "#reverse_polish(v...) = \n", "\n", "# @test reverse_polish(3,4,5,:*,:-) == :(3 - 4 * 5)\n", "# @test eval(reverse_polish(4,5,:*)) == 20\n", "```" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.0.0", "language": "julia", "name": "julia-1.0" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.0.0" } }, "nbformat": 4, "nbformat_minor": 2 }