Question
Recall the definition of the mylist data type, seen in class: type mylist = | Nil | Cons of int * mylist;; The following are
Recall the definition of the mylist data type, seen in class: type mylist = | Nil | Cons of int * mylist;; The following are legal instances of the mylist data type: Nil (* analogous to [] *) Cons(1,Cons(2,Cons(3,Nil))) (* analogous to 1::2::3::[] or [1;2;3] *) Cons(4,Cons(5,Cons(1,Cons(8,Nil)))) (* analogous to 4:5:1:8:[] or [4;5;1;8] *) Check the code from class for examples on how to use pattern matching on mylists to define functions that process mylists. For this problem, define a function called my_sum_all that takes a mylist as its only input. It should return the sum of the numbers in the mylist, as illustrated below: # my_sum_all (Cons(2,Cons(5,Cons(3,Nil))));; - : int = 10 # my_sum_all Nil;; - : int = 0 After you have defined my_sum_all, you can use the list_to_mylist function defined in class to test it: # my_sum_all (list_to_mylist [2;4;10;100]);; - : int = 116
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