Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Logan Ince
cse34122wipublic
Commits
0325c5d0
Commit
0325c5d0
authored
Dec 30, 2021
by
Dan Grossman
Browse files
import lec03 code
parent
a7dff92d
Changes
1
Hide whitespace changes
Inline
Sidebyside
lecture/lec03/lec03.ml
0 → 100755
View file @
0325c5d0
(* These two lines will be at the top of provided lecture code
just to make the repl cleaner for teaching. You can leave
them commented out, or not, or use them in your homework,
or not, as you wish. *)
(*#utop_prompt_dummy
let _ = UTop.set_show_box false *)
(* tuples, starting with pairs *)
let
swap
(
pr
:
int
*
int
)
=
(
snd
pr
,
fst
pr
)
let
sum_pairs
((
pr1
:
int
*
int
)
,
(
pr2
:
int
*
int
))
=
(
fst
pr1
+
fst
pr2
,
snd
pr1
+
snd
pr2
)
let
four_six
=
sum_pairs
((
1
,
2
)
,
(
3
,
4
))
let
sort_pair
(
pr
:
int
*
int
)
=
if
fst
pr
<
snd
pr
then
pr
else
swap
pr
let
one_four
=
sort_pair
(
4
,
1
)
let
two_three
=
sort_pair
(
2
,
3
)
let
div_mod
((
x
:
int
)
,
(
y
:
int
))
=
(
x
/
y
,
x
mod
y
)
(* nesting *)
let
middle_elt
(
prpr
:
int
*
(
int
*
int
))
=
fst
(
snd
prpr
)
let
two
=
middle_elt
(
1
,
(
2
,
3
))
(* arbitrary tuples *)
let
person1
=
((
"Zachary"
,
"L."
,
"Tatlock"
)
,
201
)
let
person2
=
((
"Daniel"
,
"J."
,
"Grossman"
)
,
309
)
(* BUT wait! Not only isn't there a function to get the third (or fourth or...) element, but fst and snd don't work on
* 3tuples (or 4tuples or...)
* Indeed, sometimes type systems "get in our way"  fst and snd are functions that require pairs (2tuples)
* OCaml's answer is patternmatching (next week) so for Homework 1, we "give you" fst3, snd3, and thd3, which you
* will use for triples (3tuples)
*)
(* you cannot understand these implementations yet but you can use them *)
let
fst3
(
x
,_,_
)
=
x
(* gets the first element of a triple *)
let
snd3
(
_
,
x
,_
)
=
x
(* gets the second element of a triple *)
let
thd3
(
_
,_,
x
)
=
x
(* gets the third element of a triple *)
let
middle_initials
=
(
snd3
(
fst
person1
)
,
snd3
(
fst
person2
))
(* lists *)
(* building lists: from [] (proncounced empty) and :: (pronounced cons) or the [x1;...;xn] shorthand *)
let
empty
=
[]
let
some_years
=
2019
::
(
2020
::
(
2021
::
[]
))
let
some_years
=
2019
::
2020
::
2021
::
[]
let
some_years
=
[
2019
;
2020
;
2021
]
(* confusing error message because this is a oneelement list of a triple
let bad_some_years = [2019,2020,2021]
*)
(* naturally, we can make lists using any expressions *)
let
more_years
=
(
2020

3
)
::
(
fst
(
sum_pairs
(
one_four
,
two_three
)))
::
(
if
0
>
2
then

3
else
4
)
::
some_years
(* because lists typically have any length, we use recursion to make them or process them
* check for empty: ___ = []
* get first element: List.hd (pronounced head)
* get the list containing all but the first element: List.tl (pronounced tail)
*)
let
rec
countdown
(
n
:
int
)
=
if
n
<=
0
then
[]
else
n
::
countdown
(
n

1
)
let
rec
sum_list
(
xs
:
int
list
)
=
if
xs
=
[]
then
0
else
(
List
.
hd
xs
)
+
sum_list
(
List
.
tl
xs
)
let
rec
length
(
xs
:
'
a
list
)
=
if
xs
=
[]
then
0
else
1
+
length
(
List
.
tl
xs
)
let
rec
nth
((
xs
:
'
a
list
)
,
(
n
:
int
))
=
if
n
=
0
then
List
.
hd
xs
else
nth
(
List
.
tl
xs
,
n

1
)
let
rec
sum_pair_list
(
prs
:
(
int
*
int
)
list
)
=
if
prs
=
[]
then
0
else
(
fst
(
List
.
hd
prs
))
+
(
snd
(
List
.
hd
prs
))
+
sum_pair_list
(
List
.
tl
prs
)
let
rec
firsts
(
prs
:
(
'
a
*
'
b
)
list
)
=
if
prs
=
[]
then
[]
else
(
fst
(
List
.
hd
prs
))
::
(
firsts
(
List
.
tl
prs
))
let
rec
seconds
(
prs
:
(
'
a
*
'
b
)
list
)
=
if
prs
=
[]
then
[]
else
(
snd
(
List
.
hd
prs
))
::
(
seconds
(
List
.
tl
prs
))
let
sum_pair_list2
(
prs
:
(
int
*
int
)
list
)
=
(
sum_list
(
firsts
prs
))
+
(
sum_list
(
seconds
prs
))
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment