Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lambda terms in de Bruijn notation are represented as trees with unary lambda no

ID: 3754652 • Letter: L

Question

 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.  The following test set needs to be displayed by the program.  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))))) 

Explanation / Answer

Lambda Terms :

A Lambda function is a compression functions that can take any number of arguments ie., can have only one expression.

Syntax : lambda arguments : expression

De Bruijn notation :

De Bruijn notation displays the unary and binary nodes. ...

def l(x) : return ('l',x)

def a(x,y) : return ('a',x,y)

Program:

class tree:

Initializing the step

  l(a(a(1,a(l(l(0)),1)),1)) --> def l(x) : return ('l',x), def a(x,y) :return ('a',x,y)

Ans : ('I',('a',('a',1),'a',('I',0)),('a',1)),'1'))

Class tree:

def __init__(self , value):

        self.value = value

        self.left = None

        self.right = None

syntax : lambda node: getattr(node, childattr)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote