(********************************************************************************** ****************** Assignment 5 ************************************************** **********************************************************************************) use("ex5aux.sml"); (* Write here the full path of this file in your computer * For example, "C:/Desktop/ml/ex5aux.sml" for C:\Desktop\ml\ex5aux.sml *) (****************** Question 3 ****************************************************) (* 1. Signature: map_n_tree(f, t) Type: ('a -> 'b) * 'a n_tree -> 'b n_tree Purpose: Apply a given single-parameter function 'f' to the values of all leaves of a given n_tree 't'. The result is an n_tree with the same structure, containing the corresponding applications of 'f'. *) val rec map_n_tree = ... (* Example val tree1 = N_branch([Leaf 2, N_branch([Leaf 5, Leaf 3, Leaf 8])]); map_n_tree((fn x => x * 10), tree1); The last line should return: val it = N_branch [Leaf 20,N_branch [Leaf 50,Leaf 30,Leaf 80]] : int n_tree *) (* 2. Signature: map_labeled_n_tree(f, t) Type: ('a -> 'b) * 'a labeled_n_tree -> 'b labeled_n_tree Purpose: Apply a given single-parameter function 'f' to the values of all nodes of a given labeled_n_tree 't'. The result is an labeled_n_tree with the same structure, containing the corresponding applications of 'f'. *) val rec map_labeled_n_tree = ... (* Example val Ltree1 = L_N_branch(1, [L_Leaf 2, L_N_branch(3, [L_Leaf 5, L_Leaf 3, L_Leaf 8])]); map_labeled_n_tree((fn x => x * 10), Ltree1); The last line should return: val it = L_N_branch (10,[L_Leaf 20,L_N_branch (30,[L_Leaf 50,L_Leaf 30,L_Leaf 80])]) : int labeled_n_tree *) (* 3. Signature: preorder_labeled_n_tree(t) Type: 'a labeled_n_tree -> 'a list Purpose: Collect all elements to a list in a pre-order manner. *) val rec preorder_labeled_n_tree = ... (* Example: preorder_labeled_n_tree(Ltree1); This example should return val it = [1,2,3,5,3,8] : int list *) (* 4. Signature: replace_labeled_n_tree(from_val, to_val, t) Type: ''a * ''a * ''a labeled_n_tree -> ''a labeled_n_tree Purpose: replaces all occurences of 'from_val' with 'to_val' in the labeled_n_tree 't'. *) val replace_labeled_n_tree = ... (* Example replace_labeled_n_tree(3, 30, Ltree1); This example should return val it = L_N_branch (1,[L_Leaf 2,L_N_branch (30,[L_Leaf 5,L_Leaf 30,L_Leaf 8])]) : int labeled_n_tree *) (****************** Question 4*****************************************************) (* 1. Signature: encrypt_seq(k, s) Type: int * int seq -> int seq Purpose: accept an integer key and a sequence (e.g. a lazy list) of integers, and return a sequence of the encrypted elements of the given sequence, according to the key. *) val encrypt_seq = ... (* Example: val evens_from_4 = evens_from(4); val enc_evens_from_4 = encrypt_seq (1234, evens_from_4); take(enc_evens_from_4, 9); The last line should return val it = [8,9,10,11,16,17,18,19,24] : int list *) (* 2. Signature: neg_key_seq(k) Type: int -> int seq Purpose: repeat the negation of k's digits *) val neg_key_seq = ... (* Example: take (neg_key_seq(1234), 9); This example should return val it = [~4,~3,~2,~1,~4,~3,~2,~1,~4] : int list *) (* 3. Signature: decrypt_seq(k, es) Type: int * int seq -> int seq Purpose: decrypt sequence the encrypted sequence 'es' according to key 'k' *) val decrypt_seq = ... (* Example: take (decrypt_seq(1234, enc_evens_from_4) , 9); This example should return val it = [4,6,8,10,12,14,16,18,20] : int list *)