Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Programming Language Expert

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

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M91669820
  • Price:- $90

Guranteed 48 Hours Delivery, In Price:- $90

Have any Question?


Related Questions in Programming Language

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

Structs and enumsoverviewin this task you will create a

Structs and Enums Overview In this task you will create a knight database to help Camelot keep track of all of their knights. Instructions Lets get started. 1. What the topic 5 videos, these will guide you through buildi ...

Background informationthis assignment tests your

Background Information This assignment tests your understanding of and ability to apply the programming concepts we have covered throughout the unit. The concepts covered in the second half of the unit build upon the fun ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Overviewthis tasks provides you an opportunity to get

Overview This tasks provides you an opportunity to get feedback on your Learning Summary Report. The Learning Summary Report outlines how the work you have completed demonstrates that you have met all of the unit's learn ...

Task arrays and structsoverviewin this task you will

Task: Arrays and Structs Overview In this task you will continue to work on the knight database to help Camelot keep track of all of their knights. We can now add a kingdom struct to help work with and manage all of the ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Assignment task -q1 a the fibonacci numbers are the numbers

Assignment Task - Q1. (a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As