Question

In: Advanced Math

5a Please code in language Ocaml In each of the three parts in this problem, you...

5a

Please code in language Ocaml

In each of the three parts in this problem, you will get full credit if you use foldT and define at most one helper function.

(a) Define an OCaml function leafCount : ’a binTree -> int which returns an integer representing the total number of leaves in a tree.

Starter Code:

type 'a binTree =
| Leaf
| Node of 'a * ('a binTree) * ('a binTree)

let leafCount (t : 'a binTree) : int = (* your work here *) 0

Solutions

Expert Solution

ANSWER:

Given That data Define an OCAML function leaf Count : ’a bin tree -> int which returns an integer representing the total number of leaves in a tree.

SO

For leaf Count: If the current node is an internal node (non-leaf), then the sum of lea fCount on the left and right sub trees is returned. If it is a leaf, then 1 is returned, else 0.

While counting the number of leaves, if we come across an empty Node, then we return 0, else if the node is a leaf we return 1 or else we recursively call the leaf Count function on the left and right sub trees and sum the values.

Note: In the given tree definition, Leaf refers to an empty node (This terminology is quite confusing, but I have stuck to it for the sake of consistency).

Consider this tree as a small example.

   The expected answer for the number of leaves in this tree is 2.

Now examine the code snippet below. (Ocaml)

Ocaml Code pasted below:

type 'a bin Tree =
   | Leaf
   | Node of 'a * ('a bin Tree) * ('a bin Tree)


let rec leaf Count = function
   | Leaf -> 0
   | Node(_, Leaf, Leaf) -> 1
   | Node(_, l, r) -> leaf Count l + leaf Count r ;;

leaf Count my_tree


Related Solutions

Describe a sound strategy for language revival using each of the three parts of language planning.
Describe a sound strategy for language revival using each of the three parts of language planning.
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE DIFFERENTIATE FILES SO I CAN UNDERSTAND BETTER. NOTE - Submission in parts. Parts required - Dog Class Code, Dog Manager Class Code and the main code. Please differentiate all three in the answer. This Assignment is designed to take you through the process of creating basic classes, aggregation and manipulating arrays of objects. Scenario: A dog shelter would like a simple system to keep...
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
Please answer with code for C language Problem: Counting Numbers Write a program that keeps taking...
Please answer with code for C language Problem: Counting Numbers Write a program that keeps taking integers until the user enters -100. In the end, the program should display the count of positive, negative (excluding that -100) and zeros entered. Sample Input/Output 1: Input the number: 0 2 3 -9 -6 -4 -100 Number of positive numbers: 2 Number of Negative numbers: 3 Number of Zero: 1
The language is Assembly Language. Thank you! Assignment 3: Run the following code. Record what each...
The language is Assembly Language. Thank you! Assignment 3: Run the following code. Record what each of the flag changes are for each command. INCLUDE Irvine32.inc .data .code main PROC mov al,255 add al,1 call DumpRegs sub al,1 call DumpRegs sub al,1 call DumpRegs exit main ENDP END main
Please solve this problem with Python Language. P#2. A Pythagorean triplet is a set of three...
Please solve this problem with Python Language. P#2. A Pythagorean triplet is a set of three natural numbers, a < b < c, for which a2 + b2 = c2 . For example, 32 + 42 = 52. And a + b + c = 3 + 4 + 5 = 12. There exists exactly two Pythagorean triplet for which a + b + c = 300. Find a, b, c. Hints: Level-0 Algorithm: 1. Find the possible ranges of...
This problem is about java program and please show the detail comment and code in each...
This problem is about java program and please show the detail comment and code in each class. Thank you! Create four classes with the following UML diagrams: (The "-" means private and the testBankAccount() testMobilePhone() testChocolate() testStudent() all static +-----------------------------+ | BankAccount | +-----------------------------+ | - money: int | +-----------------------------+ | + BankAccount(int money) | | + getMoney(): int | | + setMoney(int money): void | | + testBankAccount(): void | +-----------------------------+ +------------------------------------------------+ | MobilePhone     | +------------------------------------------------+ | -...
*****************PLEASE PROVIDE THE CODE IN RACKET PROGRAMMING LANGUAGE ONLY AND MAKE SURE CODE RUNS ON THE...
*****************PLEASE PROVIDE THE CODE IN RACKET PROGRAMMING LANGUAGE ONLY AND MAKE SURE CODE RUNS ON THE WESCHEME IDE************* Write a tail-recursive Racket function "kept-short-rec" that takes an integer and a list of strings as parameters and evaluates to an integer. The resulting integer should be the number of strings on the original list whose string length is less than the integer parameter. For example, (kept-short-rec 3 '("abc" "ab" "a")) should evaluate to 2 because there are only 2 strings shorter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT