Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
uva
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
uva
Commits
6c6d35c3
Commit
6c6d35c3
authored
13 years ago
by
Sander Mathijs van Veen
Browse files
Options
Downloads
Patches
Plain Diff
funcprog: finished week 2, assignment 5.{1,2,3}.
parent
a97f3667
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
funcprog/week2/ass5_1.ml
+7
-2
7 additions, 2 deletions
funcprog/week2/ass5_1.ml
funcprog/week2/ass5_2.ml
+30
-5
30 additions, 5 deletions
funcprog/week2/ass5_2.ml
funcprog/week2/ass5_3.ml
+15
-22
15 additions, 22 deletions
funcprog/week2/ass5_3.ml
with
52 additions
and
29 deletions
funcprog/week2/ass5_1.ml
+
7
−
2
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
=
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
=
Printf
.
printf
"%
4
d: %b
\n
"
x
(
isLeapYear
x
);;
Printf
.
printf
"%d: %b
\n
"
x
(
isLeapYear
x
);;
(
testLeapYear
1400
);;
(* false *)
(
testLeapYear
1582
);;
(* false *)
...
...
This diff is collapsed.
Click to expand it.
funcprog/week2/ass5_2.ml
+
30
−
5
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
(*
* 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
=
match
day
with
1
|
21
|
31
->
(
string_of_int
day
)
^
"st"
|
2
|
22
->
(
string_of_int
day
)
^
"nd"
|
3
|
23
->
(
string_of_int
day
)
^
"rd"
|
_
when
day
>
0
->
(
string_of_int
day
)
^
"th"
|
_
->
raise
Hell
1
|
21
|
31
->
(
string_of_int
day
)
^
"st"
|
2
|
22
->
(
string_of_int
day
)
^
"nd"
|
3
|
23
->
(
string_of_int
day
)
^
"rd"
|
_
when
(
day
>
0
&&
day
<
32
)
->
(
string_of_int
day
)
^
"th"
|
_
->
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
=
match
month
with
1
->
"January"
...
...
This diff is collapsed.
Click to expand it.
funcprog/week2/ass5_3.ml
+
15
−
22
View file @
6c6d35c3
let
rec
digitRoot
number
=
let
result
=
ref
0
in
let
current
=
ref
number
in
(
Printf
.
printf
"input: %d
\n
"
!
current
;
(*
* Calculate the digital root of a number: add up all of its digits, and do that
* recursively until a single digit is obtained.
*)
while
!
current
>
0
do
result
:=
!
result
+
(
!
current
mod
10
);
Printf
.
printf
"%d + "
(
!
current
mod
10
);
current
:=
!
current
/
10
;
done
;
Printf
.
printf
"= %d
\n
"
!
result
;
if
!
result
>
9
then
(
digitRoot
!
result
)
else
!
result
)
let
rec
digitRoot
?
(
sum
=
0
)
number
=
match
number
with
_
when
number
<
10
->
(
sum
+
number
)
|
_
->
(
digitRoot
(
digitRoot
~
sum
:
(
sum
+
(
number
mod
10
))
(
number
/
10
)))
;;
let
test_digitRoot
input
=
...
...
@@ -23,8 +13,11 @@ let test_digitRoot input =
Printf
.
printf
"%d -> %d
\n
"
input
(
digitRoot
input
)
;;
(
test_digitRoot
20
);;
(
test_digitRoot
24
);;
(
test_digitRoot
1234
);;
(
test_digitRoot
123456789
);;
(
test_digitRoot
(
-
1
));;
(* what to do with negative numbers? *)
test_digitRoot
20
;;
test_digitRoot
24
;;
test_digitRoot
1234
;;
(* = 1 *)
test_digitRoot
65536
;;
(* = 7 *)
test_digitRoot
12345678
;;
(* = 9 *)
test_digitRoot
18273645
;;
(* = 9 *)
test_digitRoot
123456789
;;
(* = 9 *)
test_digitRoot
5674
;;
(* = 4 *)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment