logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

Write a recursive function pow, which takes two integer parameters x and n, and returns xn. Also write a function float pow, which does the same thing.

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

1  Recursion and Higher-order Functions    

In this section, you may not use any functions available in the OCaml library that already solves all or most of the question. For example, OCaml provides a List.rev function, but you may not use that in this section.

  1. Write a recursive function pow, which takes two integer parameters x and n, and returns xn. Also write a function float pow, which does the same thing, but for x being a float (n is still an integer). You may assume that n will always be non-negative.

  2. Write a function compress to remove consecutive duplicates from a

# compress ["a";"a";"b";"c";"c";"a";"a";"d";"e";"e";"e"];;

- : string list = ["a"; "b"; "c"; "a"; "d"; "e"]

  1. Write a function remove if of the type 'a list -> ('a -> bool) -> 'a  list, which takes a list and    a predicate, and removes all the elements that satisfy the condition expressed in the

# remove_if [1;2;3;4;5] (fun x -> x mod 2 = 1);;

- : int list = [2; 4]

  1. Write a function equivs of the type ('a -> 'a -> bool) -> 'a list ->  'a  list list, which par-  titions a list into equivalence classes according to the equivalence

# equivs (=) [1;2;3;4];;

- : int list list = [[1];[2];[3];[4]]

# equivs (fun x y -> (=) (x mod 2) (y mod 2)) [1; 2; 3; 4; 5; 6; 7; 8];;

- : int list list = [[1; 3; 5; 7]; [2; 4; 6; 8]]

  1. Some programming languages (like Python) allow us to quickly slice a list based on two integers i and j, to return the sublist from index i (inclusive) and j (not inclusive). We want such a slicing function in OCaml as

Write  a  function  slice  as  follows:  given  a  list  and  two  indices,  i  and  j,  extract  the  slice  of  the  list containing the elements from the ith (inclusive) to the jth (not inclusive) positions in the original list.

# slice ["a";"b";"c";"d";"e";"f";"g";"h"] 2 6;;

- : string list = ["c"; "d"; "e"; "f"]

Invalid index arguments should be handled gracefully. For example,

# slice ["a";"b";"c";"d";"e";"f";"g";"h"] 3 2;;

- : string list = []

# slice ["a";"b";"c";"d";"e";"f";"g";"h"] 3 20;

- : string list = ["d";"e";"f";"g";"h"];

  1. Write a function called composition, which takes two functions as its input, and returns their compo- sition as the output.

 

# let square_of_increment = composition square increment;; val square_of_increment : int -> int = <fun>

# square_of_increment 4;; (* increments 4 to 5, and THEN computes square *)

- : int = 25

  1. Write a function called equiv on, which takes three inputs: two functions f and g, and a list lst. It returns true if and only if the functions f and g have identical behavior on every element of lst.

#  let f i =  i *  i;;

val f : int -> int = <fun> # let g i = 3 * i;;

val g : int -> int = <fun> # equiv_on f g [3];;

  • : bool = true

# equiv_on f g [1;2;3];;

  • : bool = false

    1. Write a functions called pairwisefilter with two parameters: (i) a function cmp that compares two elements of a specific T and returns one of them, and (ii) a list lst of elements of that same type T. It returns a list that applies cmp while taking two items at a time from lst. If lst has odd size, the last element is returned “as is”.

# pairwisefilter min [14; 11; 20; 25; 10; 11];;

- : int list = [11; 20; 10]

# (* assuming that shorter : string * string -> string = <fun> already exists *)

# pairwisefilter shorter ["and"; "this"; "makes"; "shorter"; "strings"; "always"; "win"];;

- : string list = ["and"; "makes"; "always"; "win"]

  1. Write the polynomial function, which takes a list of tuples and returns the polynomial function corre- sponding to that Each tuple in the input list consists of (i) the coefficient, and (ii) the exponent.

# (* below is the polynomial function f(x) = 3x^3 - 2x + 5 *) # let f = polynomial [3, 3;; -2, 1; 5, 0];;

val f : int -> int = <fun>  # f 2;;

- : int = 25

  1. The power set of a set S is the set of all subsets of S (including the empty set and the entire set). Write a function powerset of the type 'a list -> 'a list list, which treats lists as unordered sets, and returns the powerset of its input You may assume that the input list has no duplicates.

# powerset [3; 4; 10];;

- : int list list = [[]; [3]; [4]; [10]; [3; 4]; [3; 10]; [4; 10]; [3; 4; 10]];

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Um e HaniScience

623 Answers

Hire Me
expert
Muhammad Ali HaiderFinance

824 Answers

Hire Me
expert
Husnain SaeedComputer science

612 Answers

Hire Me
expert
Atharva PatilComputer science

572 Answers

Hire Me
July
January
February
March
April
May
June
July
August
September
October
November
December
2025
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
SunMonTueWedThuFriSat
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
00:00
00:30
01:00
01:30
02:00
02:30
03:00
03:30
04:00
04:30
05:00
05:30
06:00
06:30
07:00
07:30
08:00
08:30
09:00
09:30
10:00
10:30
11:00
11:30
12:00
12:30
13:00
13:30
14:00
14:30
15:00
15:30
16:00
16:30
17:00
17:30
18:00
18:30
19:00
19:30
20:00
20:30
21:00
21:30
22:00
22:30
23:00
23:30