Implement the curried functions in ocaml

Assignment Help Programming Languages
Reference no: EM13975043

Project - OCaml basics

Introduction

The goal of this project is to get you familiar with programming in OCaml. You will have to write a number of small functions, each of whose specification is given below. In our reference solution, each function's implementation is typically 3-6 lines of code; in a couple of cases you will want to write a helper function which will add another 3-6 lines.

Part A: Simple functions

Write the following functions:

Name

Type

Return value

Example

mult_of_five x

int -> bool

true if x is a multiple of 5 
false otherwise

mult_of_five 5 = true 
mult_of_five 1 = false

sum_upto_three ls

int list -> int

the sum of the list's elements up to the first three

sum_upto_three [1] = 1 
sum_upto_three [1;2;3] = 6 
sum_upto_three [1;2;3;4;5] = 6

caddr_int

int list -> int

the second element of the list 
-1 if the list has 0 or 1 elements

caddr_int [1;2;3] = 2 
caddr_int [1] = -1

Part B: Simple Curried Functions

A curried function is one that takes multiple arguments "one at a time". For example, the following function sub takes two arguments and computes their difference:

let sub x y = x - y

The type of this function is int -> int -> int. Technically, this says that sub is a function that takes an int and returns a function that takes another int and finally returns the answer, also an int. In other words, we could write

sub 2 1

and this will produce the answer 1. But we could also do something like this:

let f = sub 2 in

f 1

and this will also produce 1. Notice how we call sub with only one argument, so it returns a function f that takes the second argument. In general, you can think of a function f of the type

t1 -> t2 -> t3 -> ... -> tn

as a function that takes n-1 arguments of types t1, t2, t3, ..., tn-1 and produces a result of type tn. Such functions are written with OCaml syntax

let f a1 a2 a3 ... = body

where a1 has type t1, a2 has type t2, etc.

Implement the following simple, curried functions:

Name

Type

Return value

Example

mult_of_n x y

int -> int -> bool

whether x is a multiple of y

mult_of_n 5 5 = true 
mult_of_n 2 3 = false

triple_it x y z

'a -> 'b -> 'c -> 'a*'b*'c

a tuple containing the three arguments, in order

triple_it 5 5 5 = (5,5,5) 
triple_it "hello" "b" "a" = ("hello","b","a")

maxpair (x,y) (m,n)

'a*'b -> 'a*'b -> 'a*'b

(x,y) if it is larger than (m,n), according to lexicographic ordering 
(m,n) otherwise (see note about comparison functions below)

maxpair (1,2) (3,4) = (3,4) 
maxpair (1,2) (1,3) = (1,3)

The OCaml comparison functions (=,<=,>=,<, and >) are polymorphic, so you can give them any two arguments of the same type.

Part C: Recursive Functions

The rest of the project asks that you implement a number of recursive functions, many of which compute on lists.

Name

Type

Return value

Example

prod l

int list -> int

the product of all elements in l 
1 if l is empty

prod [5;6] = 30 
prod [0;5;3] = 0

unzip l

('a*'b) list -> ('a list)*('b list)

a pair of lists consisting of the all first and second elements, respectively, of the pairs in l

unzip [(1,2);(3,4)] = ([1;3],[2;4]) 
unzip [(3,7);(4,5);(6,9)] = ([3;4;6],[7;5;9])

maxpairall l

(int*int) list -> int*int

the largest pair in input list l, according to lexicographic ordering 
(0,0) if l is empty

maxpairall [(1,2);(3,4)] = (3,4) 
maxpairall [(1,2);(1,3);(0,0)] = (1,3)

addTail l x

'a list -> 'a -> 'a list

a new list where x is appended to the end of l

addTail [1;2] 3 = [1;2;3]

get_val x n

int list -> int -> int

element of list x at index n (indexes start at 0) 
-1 if n is outside the bounds of the list

get_val [5;6;7;3] 1 = 6 
get_val [5;6;7;3] 4 = -1

get_vals x y

int list -> int list -> int list

list of elements of list x at indexes in list y, 
-1 for any indexes in y are outside the bounds of x (as with get_vals) 
elements must be returned in order listed in y

get_vals [5;6;7;3] [2;0] = [7;5] 
get_vals [5;6;7;3] [2;4] = [7;-1]

list_swap_val b u v

'a list -> 'a -> 'a -> 'a list

list b with values u,v swapped 
change value of multiple occurrences of u and/or v, if found 
change value for u even if v not found in list, and vice versa

list_swap_val [5;6;7;3] 7 5 = [7;6;5;3] 
list_swap_val [5;6;3] 7 5 = [7;6;3]

index x v

'a list -> 'a -> int

index of rightmost occurrence of value v in list x 
(indexes start at 0)
 
-1 if not found

index [1;2;2] 1 = 0 
index [1;2;2;3] 2 = 2 
index [1;2;3] 5 = -1

distinct l

'a list -> 'a list

a new list that contains the distinct elements of l, in the same order they appear in l

distinct [1;2;2] = [1;2] 
distinct [2;1;2;2;3] = [2;1;3]

find_new x y

'a list -> 'a list -> 'a list

list of members of list x not found in list y 
maintain relative order of elements in result

find_new [4;3;7] [5;6;5;3] = [4;7] 
find_new [5;6;5;3] [4;3;7] = [5;6;5]

is_sorted x

'a list -> bool

true if elements in x are in sorted order, false otherwise 
return true for []

is_sorted [5;5;7;9] = true 
is_sorted [9;7;5] = false

Attachment:- OCaml basics.rar

Reference no: EM13975043

Questions Cloud

How characteristics of oil make anadarkos capacity planning : Discuss how the characteristics of oil make Anadarko's capacity planning strategy possible. Why would a company like Apple not be able to plan its capacity in the same way?
What procedures you implement to prevent disease outbreak : What resources will you use to help provide food, water, and toilet facilities for victims? Who will you call and when will you call? What procedures will you implement to prevent disease outbreak? How will you care for the needs of the public healt..
Determine the seasonal indexes and break : Combining the quarterly bookings for each year,   fit a linear trend equation and forecast the number of bookings for 2013. Determine the seasonal indexes and break the 2013 forecast in part (a) into its quarterly components.
Prices for regular unleaded gasoline : The U.S. Energy  Information Administration reports the following prices for regular unleaded gasoline in Germany from 2003 through 2008. Source: U.S. Energy Information Administration, Annual Energy Review 2008, p. 321.
Implement the curried functions in ocaml : Goal of this project is to get you familiar with programming in OCaml. You will have to write a number of small functions - write a helper function which will add another 3-6 lines.
What are the communication challenges within an organization : Create and deliver a presentation that provides results and suggestions for improvement based on the data obtained from the audit and the literature on organizational communication
What is the magnitude of the total momentum of the system : If you run toward the ball at a speed of 4.4 m/s, and the ball is flying directly at you at a speed of 32 m/s, what is the magnitude of the total momentum of the system (you and the ball)? Assume your mass is 60 kg.
Problem regarding the exponential smoothing : 1. Given the time series in Exercise 18.65, and using exponential smoothing with a 5 0.5, what would have been the forecast for 2009?
Forecast for water consumption during that quarter : A firm predicts it will use 560,000 gallons   of water next year. If the seasonal index of water consumption is 135 for the second quarter of the year, what is the forecast for water consumption during that quarter?

Reviews

Write a Review

Programming Languages Questions & Answers

  What criterion is being used to determine the best estimates

What we would like to do is create similar tables in an .rtf file that produces the same statistics for any numerical value against each level of a know classification/factor from any data set we wish to inspect.

  Computes the wind-chill factor for a given temperature

The user should be able to enter a temperature and wind speed via text boxes, then click a button to view the corresponding wind-chill factor in a separate text box. As part of your page, you should define a function named Wi ndChi 11 that accepts..

  Examine the boxplot

Examine the boxplot and briefly discuss the overall pattern of electricity usage for the households.

  Prepare menu driven program for arithmetic operations

Create algorithms, flowchart and write programs in c language for following: Prepare a menu driven program for different arithmetic operations.

  Write looping structure pseudocode accepting employee data

Write looping structure pseudocode which prompts user for employee data; application continues to accept data for new employees until user enters 0 for ID number to indicate desire to quit.

  Using datagrid in an application

Update the field named fldQuestionDescription.

  Write a script that accepts a part of a process name

Using what we know about shell scripts, write a script that accepts a part of a process name as the first argument (like pgrep), which prints out the PID of all matching processes in blue, which is color code 4. Then print out the number of proces..

  What are the risks associated with directly executing user

1. What are the risks associated with directly executing user input? What are the minimum steps needed to parse user input before it is allowed to be used in execution? 2. What are the risks associated with inheritance in classes? Why are generic cla..

  Create class uses element array of digits to store integers

Create a class HugeInteger which uses a 40-element array of digits to store integers as large as 40 digits each. Provide methods input, output, add and subtract.

  Design and develop a unix file sharing system

Design and develop a UNIX file sharing system. For this task you will be required to develop a simple application in C programming language.

  Write down a program which asks user to type of series of

write a program that asks user to type of series of positive integers. when the user types a negative value the program

  What is the output of the statement

Given the subsequent array declaration, if the array is stored starting at address 2500, what is the output of the following statement?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd