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
7162dc1d
Commit
7162dc1d
authored
Nov 22, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
funcprog: Minor improvements.
parent
67bd7fb4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
5 deletions
+11
-5
funcprog/week3/ass7.ml
funcprog/week3/ass7.ml
+11
-5
No files found.
funcprog/week3/ass7.ml
View file @
7162dc1d
...
@@ -54,7 +54,10 @@ print "7.5: %b\n" ((=) (reduce (+) 0 [1;2;3;4;5]) 15);;
...
@@ -54,7 +54,10 @@ print "7.5: %b\n" ((=) (reduce (+) 0 [1;2;3;4;5]) 15);;
(* 6: isSublist l s that checks whether or not the argument list s is a sublist
(* 6: isSublist l s that checks whether or not the argument list s is a sublist
* of the argument list l. *)
* of the argument list l. *)
let
rec
isSublist
l
s
=
match
(
l
,
s
)
with
let
isSublist
l
s
=
(* Store the full search term *)
let
full
=
s
in
let
rec
sublistMatches
l
s
=
match
(
l
,
s
)
with
|
_
,
[]
->
true
|
_
,
[]
->
true
|
[]
,
_
->
false
|
[]
,
_
->
false
|
h1
::
t1
,
h2
::
t2
->
|
h1
::
t1
,
h2
::
t2
->
...
@@ -63,14 +66,17 @@ let rec isSublist l s = match (l, s) with
...
@@ -63,14 +66,17 @@ let rec isSublist l s = match (l, s) with
(* and the next head of s matches the next head of l. *)
(* and the next head of s matches the next head of l. *)
&&
(
t1
=
[]
&&
t2
=
[]
||
hd
t1
=
hd
t2
)
&&
(
t1
=
[]
&&
t2
=
[]
||
hd
t1
=
hd
t2
)
(* recursively match the tails of s and l. *)
(* recursively match the tails of s and l. *)
&&
isSublist
t1
t2
&&
sublistMatches
t1
t2
(* otherwise, continue with the tail of s. *)
(* otherwise, continue with the tail of l and reset s. *)
||
isSublist
t1
s
||
sublistMatches
t1
full
in
sublistMatches
l
s
;;
;;
print
"7.6.1: %b
\n
"
(
isSublist
[
1
;
2
;
3
;
4
]
[
3
;
4
]);;
print
"7.6.1: %b
\n
"
(
isSublist
[
1
;
2
;
3
;
4
]
[
3
;
4
]);;
print
"7.6.2: %b (%s)
\n
"
(
isSublist
[
1
;
2
;
3
;
4
]
[
2
;
4
])
"expected failure"
;;
print
"7.6.2: %b (%s)
\n
"
(
isSublist
[
1
;
2
;
3
;
4
]
[
2
;
4
])
"expected failure"
;;
print
"7.6.3: %b
\n
"
(
isSublist
[]
[]
);;
print
"7.6.3: %b (%s)
\n
"
(
isSublist
[
1
;
2
;
3
;
3
;
4
]
[
2
;
3
;
4
])
"expected failure"
;;
print
"7.6.4: %b
\n
"
(
isSublist
[
1
;
2
;
3
;
3
;
4
]
[
3
;
3
;
4
]);;
print
"7.6.5: %b
\n
"
(
isSublist
[]
[]
);;
(* 7. lookup l key that given a list l of (key, value) tuples and a key returns
(* 7. lookup l key that given a list l of (key, value) tuples and a key returns
* the value associated with that key. *)
* the value associated with that key. *)
...
...
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