Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Lambda terms in de Bruijn notation are represented as trees with unary lambda nodes and binary application nodes, with de Bruijn indices (starting at 0)
Lambda terms in de Bruijn notation are represented as trees with unary lambda nodes and binary application nodes, with de Bruijn indices (starting at 0) marking their leaf nodes. Write a Python program, that given a lambda term in de Bruijn notation, displays it as a tree. The program must use Python tuples for both our lambda nodes and application nodes. To facilitate testing with easier to read inputs, we define: def l(x) : return ('l',x) def a(x,y) : return ('a',x,y) As an example, the Python expression l(l(a(a(0,l(3)),a(l(0),0)))) returns the tree ('l', ('l', ('a', ('a', 0, ('l', 3)), ('a', ('l', 0), 0)))) that can be displayed like this: l | | l | | a __|_ / \ a a | | / \ / \ 0 l l 0 | | | | 3 0 While you can choose to write your own algorithm that outputs the tree using ASCII characters as in the picture above, you can also opt for using any of the available Python packages to help drawing nicer looking trees. Test set. l(a(a(1,a(l(l(0)),1)),1)) l(l(l(l(a(0,a(1,l(l(a(0,a(2,0)))))))))) l(l(l(a(a(0,a(1,a(a(l(l(0)),0),2))),1)))) l(l(l(a(l(1),a(l(a(1,0)),a(a(a(2,0),0),0)))))) l(l(l(a(l(a(2,a(a(l(0),l(a(1,a(1,a(1,l(1)))))),l(0)))),a(2,0)))))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started