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
f2739c4d
Commit
f2739c4d
authored
13 years ago
by
Sander Mathijs van Veen
Browse files
Options
Downloads
Patches
Plain Diff
funcprog: Finished week 2, assignment 5.4.
parent
0b3c4094
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
funcprog/week2/ass5_4.ml
+32
-19
32 additions, 19 deletions
funcprog/week2/ass5_4.ml
with
32 additions
and
19 deletions
funcprog/week2/ass5_4.ml
+
32
−
19
View file @
f2739c4d
let
even_len
x
=
if
(
x
mod
2
)
==
0
then
x
else
x
-
1
;;
#
load
"str.cma"
;;
let
isPalindrome
palin
=
let
result
=
ref
true
in
(
for
i
=
0
to
((
even_len
(
String
.
length
palin
))
/
2
)
-
1
do
if
(
palin
.
[
i
]
!=
palin
.
[(
even_len
(
String
.
length
palin
))
-
i
-
1
])
then
begin
result
:=
false
;
end
else
()
done
;
!
result
)
;;
(*
* The function isPalindrome checks if a given character string is a palindrome,
* i.e. it is identical whether being read from left to right or from right to
* left. Note that this function removes punctuation and word dividers before
* checking the given character string.
*)
let
isPalindrome
raw
=
(*
* Check if the left and right character of the string are the same. An
* empty string and a string with length 1 is by definition a palindrome.
* Check uses a character string `s' and its length `l' to recursively
* determine if `s' is a palindrome.
*)
let
rec
check
s
l
=
l
<
2
||
(
s
.
[
0
]
==
s
.
[
l
-
1
]
&&
(
check
(
String
.
sub
s
1
(
l
-
2
))
(
l
-
2
)))
in
(* Remove punctuation / word dividers -> only alphanumeric chars remain. *)
let
filter
=
Str
.
regexp
"[^a-zA-Z0-9]+"
in
let
filtered
=
String
.
lowercase
(
Str
.
global_replace
filter
""
raw
)
in
(
check
filtered
(
String
.
length
filtered
))
;;
Printf
.
printf
"%b
\n
"
(
isPalindrome
"asddsa"
)
let
test_isPalindrome
str
=
Printf
.
printf
"isPalindrome(
\"
%s
\"
) -> %b
\n
"
str
(
isPalindrome
str
)
;;
test_isPalindrome
""
;;
test_isPalindrome
"a"
;;
test_isPalindrome
"baas saab"
;;
test_isPalindrome
"never odd or even"
;;
test_isPalindrome
"Was it a rat i saw?"
;;
test_isPalindrome
"expected failure!"
;;
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