Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Exam (elaborations)

Functional programming homework1

Rating
-
Sold
-
Pages
14
Grade
A+
Uploaded on
10-09-2023
Written in
2023/2024

This document has an OCaml Functional programming homework

Institution
Course

Content preview

Online CS University

Advanced functional programming with OCaml

Homework & Practice Quiz




Exercise 01: Predefined functions




1- Explain what the following predefined functions do in Ocaml:

Int_of_float , float_ of_int, int_of_char, ceil, floor, sqrt, sin, cos, exp, log.

2- The Ocaml language has a rich string module. Explain the following functions related to this

module:

Length, get, create, sub, index, compare

3- Explain the following functions in Ocaml's List module: map, iter, for_all, exists




Exercise 02: Natural numbers




4- The following type is defined in OCAML to represent natural integers:


5- type nat = O | S of nat ;;


6- According to this definition, a natural number is either a zero, represented by the O

constructor, or the successor of a natural number, represented by the S constructor.

, 7- Evaluate the following expressions : S(S O) ;; O ;; S O ;; S(S(S(O))) ;;



Part 01: Calculations on the defined type




8- Write the recursive function addn to add two elements of type nat (the result is of type nat).


9- Write the recursive function multn multiplication of two elements of type nat (the result is of

type nat).


10-Write a factn function that calculates the factorial of a nat number (the result is a nat

number).



Part 02: Property checks on the defined type



11-Write a function that checks whether a number of type nat is zero.


12-Write a function that checks whether a number of type nat is even.




Part 03: Transformation from/to the defined type



13-Write a recursive int_of_nat function to obtain the integer value (type int) corresponding


to a nat value.


14-Write a nat_of_int function to obtain the nat value corresponding to a positive or zero integer.

Written for

Course

Document information

Uploaded on
September 10, 2023
Number of pages
14
Written in
2023/2024
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

$4.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
WhiteNoteBook

Get to know the seller

Seller avatar
WhiteNoteBook cs university
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
13
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions