Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
uva
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
uva
Commits
6c6d35c3
Commit
6c6d35c3
authored
Nov 15, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
funcprog: finished week 2, assignment 5.{1,2,3}.
parent
a97f3667
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
29 deletions
+52
-29
funcprog/week2/ass5_1.ml
funcprog/week2/ass5_1.ml
+7
-2
funcprog/week2/ass5_2.ml
funcprog/week2/ass5_2.ml
+30
-5
funcprog/week2/ass5_3.ml
funcprog/week2/ass5_3.ml
+15
-22
No files found.
funcprog/week2/ass5_1.ml
View file @
6c6d35c3
(*
* Check if the given year is a leap year. This will return true if the given
* integer is larger than 1582, can be divided by 4, but not by 100, or it can
* be divided by 400. Otherwise, false is returned.
*)
let
isLeapYear
x
=
let
isLeapYear
x
=
x
>
=
1582
&&
x
mod
4
==
0
&&
(
x
mod
100
!=
0
||
x
mod
400
==
0
)
x
>
1582
&&
x
mod
4
==
0
&&
(
x
mod
100
!=
0
||
x
mod
400
==
0
)
;;
;;
let
testLeapYear
x
=
let
testLeapYear
x
=
Printf
.
printf
"%
4
d: %b
\n
"
x
(
isLeapYear
x
);;
Printf
.
printf
"%d: %b
\n
"
x
(
isLeapYear
x
);;
(
testLeapYear
1400
);;
(* false *)
(
testLeapYear
1400
);;
(* false *)
(
testLeapYear
1582
);;
(* false *)
(
testLeapYear
1582
);;
(* false *)
...
...
funcprog/week2/ass5_2.ml
View file @
6c6d35c3
(*
* Implementation of date2str.
*
* Given a correct calendar triple (day, month, year), return a proper English
* date. For example: (1, 2, 2011) returns "February 1st, 2011".
*
* The suffix of the day number is not related to the actual month, so "February
* 31st, 2011" can be generated. However, there is no reason to implement error
* checking to prevent returning unvalid day/month combinations (not part of the
* assignment). Note: the assigment clearly states that a correct calendar
* triple is the input of date2str.
*
* This implementation does check for invalid day numbers or invalid month
* numbers. The exception Hell will be raised for these invalid integers.
*)
exception
Hell
exception
Hell
(*
* Given a day number, return the day number followed by its English suffix. For
* example, this will return ``1st'' for day 1, ``23rd'' for day 23 and ``12th'
* for day 12. If the day number is negative or the day number is not between 1
* and 31 (inclusive), Hell will be raised.
*)
let
day_with_suffix
day
=
let
day_with_suffix
day
=
match
day
with
match
day
with
1
|
21
|
31
->
(
string_of_int
day
)
^
"st"
1
|
21
|
31
->
(
string_of_int
day
)
^
"st"
|
2
|
22
->
(
string_of_int
day
)
^
"nd"
|
2
|
22
->
(
string_of_int
day
)
^
"nd"
|
3
|
23
->
(
string_of_int
day
)
^
"rd"
|
3
|
23
->
(
string_of_int
day
)
^
"rd"
|
_
when
day
>
0
->
(
string_of_int
day
)
^
"th"
|
_
when
(
day
>
0
&&
day
<
32
)
->
(
string_of_int
day
)
^
"th"
|
_
->
raise
Hell
|
_
->
raise
Hell
;;
;;
(*
* Return the English name of a month number (a number between 1 and 12,
* inclusive). If the month number is not defined, Hell will be raised.
*)
let
month_name
month
=
let
month_name
month
=
match
month
with
match
month
with
1
->
"January"
1
->
"January"
...
...
funcprog/week2/ass5_3.ml
View file @
6c6d35c3
let
rec
digitRoot
number
=
(*
let
result
=
ref
0
in
* Calculate the digital root of a number: add up all of its digits, and do that
let
current
=
ref
number
in
(
* recursively until a single digit is obtained.
Printf
.
printf
"input: %d
\n
"
!
current
;
*)
while
!
current
>
0
do
let
rec
digitRoot
?
(
sum
=
0
)
number
=
match
number
with
result
:=
!
result
+
(
!
current
mod
10
);
_
when
number
<
10
->
(
sum
+
number
)
Printf
.
printf
"%d + "
(
!
current
mod
10
);
|
_
->
(
digitRoot
(
digitRoot
~
sum
:
(
sum
+
(
number
mod
10
))
(
number
/
10
)))
current
:=
!
current
/
10
;
done
;
Printf
.
printf
"= %d
\n
"
!
result
;
if
!
result
>
9
then
(
digitRoot
!
result
)
else
!
result
)
;;
;;
let
test_digitRoot
input
=
let
test_digitRoot
input
=
...
@@ -23,8 +13,11 @@ let test_digitRoot input =
...
@@ -23,8 +13,11 @@ let test_digitRoot input =
Printf
.
printf
"%d -> %d
\n
"
input
(
digitRoot
input
)
Printf
.
printf
"%d -> %d
\n
"
input
(
digitRoot
input
)
;;
;;
(
test_digitRoot
20
);;
test_digitRoot
20
;;
(
test_digitRoot
24
);;
test_digitRoot
24
;;
(
test_digitRoot
1234
);;
test_digitRoot
1234
;;
(* = 1 *)
(
test_digitRoot
123456789
);;
test_digitRoot
65536
;;
(* = 7 *)
(
test_digitRoot
(
-
1
));;
(* what to do with negative numbers? *)
test_digitRoot
12345678
;;
(* = 9 *)
test_digitRoot
18273645
;;
(* = 9 *)
test_digitRoot
123456789
;;
(* = 9 *)
test_digitRoot
5674
;;
(* = 4 *)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment