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
CSE 341 22sp
CSE 341 22sp public
Commits
df63ea40
Commit
df63ea40
authored
Apr 20, 2022
by
James R. Wilcox
Browse files
L11 code
parent
b4ee6a60
Changes
3
Hide whitespace changes
Inline
Sidebyside
lecture/L10/lec10live.ml
View file @
df63ea40
...
...
@@ 147,10 +147,39 @@ let rec sum_int_option_list2((l: int option list)): int =

Some
(
n
)
::
xs
>
n
+
sum_int_option_list2
(
xs
)
(* didn't quite finish this example on Monday *)
let
rec
sum_int_option_pair_list3
((
l
:
(
int
option
*
int
option
)
list
))
:
int
=
let
rec
sum_int_option_pair_list3
((
l
:
(
(
int
*
int
)
option
)
list
))
:
int
=
match
l
with

[]
>
0

(
None
,
None
)
::
xs
>
sum_int_option_pair_list3
(
xs
)

(
None
,
Some
(
n2
))
::
xs
>
sum_int_option_list3
(
xs
)

(
Some
(
n1
)
,
None
)
::
xs
>
sum_int_option_list3
(
xs
)

(
Some
(
n1
)
,
Some
(
n2
))
::
xs
>
sum_int_option_list3
(
xs
)

None
::
xs
>
sum_int_option_pair_list3
(
xs
)

Some
(
n1
,
n2
)
::
xs
>
n1
+
n2
+
sum_int_option_pair_list3
(
xs
)
(* return a list of triples with elements from the input lists in "lock step" *)
(* if the input lists have different lengths, then only elements up to the
length of the shortest one are returned *)
let
rec
zip3
((
l1
:
'
a
list
)
,
(
l2
:
'
b
list
)
,
(
l3
:
'
c
list
))
:
(
'
a
*
'
b
*
'
c
)
list
=
match
l1
with

[]
>
[]

x
::
xs
>
match
l2
with

[]
>
[]

y
::
ys
>
match
l3
with

[]
>
[]

z
::
zs
>
(
x
,
y
,
z
)
::
zip3
(
xs
,
ys
,
zs
)
let
l1
=
[
1
;
2
;
3
]
let
l2
=
[
"hello"
;
""
;
"world"
]
let
l3
=
[
true
;
false
;
false
;
true
]
let
rec
zip3
((
l1
:
'
a
list
)
,
(
l2
:
'
b
list
)
,
(
l3
:
'
c
list
))
:
(
'
a
*
'
b
*
'
c
)
list
=
match
(
l1
,
l2
,
l3
)
with

(
x
::
xs
,
y
::
ys
,
z
::
zs
)
>
(
x
,
y
,
z
)
::
zip3
(
xs
,
ys
,
zs
)

_
>
[]
(* what about throwing an error if they have different lengths *)
let
rec
zip3
((
l1
:
'
a
list
)
,
(
l2
:
'
b
list
)
,
(
l3
:
'
c
list
))
:
(
'
a
*
'
b
*
'
c
)
list
=
match
(
l1
,
l2
,
l3
)
with

([]
,
[]
,
[]
)
>
[]

(
x
::
xs
,
y
::
ys
,
z
::
zs
)
>
(
x
,
y
,
z
)
::
zip3
(
xs
,
ys
,
zs
)

_
>
failwith
"zip3: lists of different length"
\ No newline at end of file
lecture/L11/lec11.ml
0 → 100644
View file @
df63ea40
(* tail recursion *)
let
rec
fact1
n
=
if
n
=
0
then
1
else
n
*
fact1
(
n

1
)
let
rec
last
xs
=
match
xs
with

[]
>
failwith
"last: empty list"

x
::
[]
>
x

_
::
xs'
>
last
xs'
let
fact2
n
=
let
rec
loop
(
n
,
acc
)
=
if
n
=
0
then
acc
else
loop
(
n

1
,
acc
*
n
)
in
loop
(
n
,
1
)
let
rec
sum1
xs
=
match
xs
with

[]
>
0

i
::
xs'
>
i
+
sum1
xs'
let
sum2
xs
=
let
rec
f
(
xs
,
acc
)
=
match
xs
with

[]
>
acc

i
::
xs'
>
f
(
xs'
,
i
+
acc
)
in
f
(
xs
,
0
)
let
rec
rev1
xs
=
match
xs
with

[]
>
[]

x
::
xs'
>
(
rev1
xs'
)
@
[
x
]
let
rev2
xs
=
let
rec
loop
(
xs
,
acc
)
=
match
xs
with

[]
>
acc

x
::
xs'
>
loop
(
xs'
,
x
::
acc
)
in
loop
(
xs
,
[]
)
(* exceptions *)
exception
MyUndesirableCondition
exception
MyOtherException
of
int
*
int
let
oh_no
()
=
raise
MyUndesirableCondition
let
oh_no_with_info
()
=
raise
(
MyOtherException
(
7
,
42
))
let
catch_example
()
=
try
oh_no
()
with
MyUndesirableCondition
>
0
let
catch_example_with_info
()
=
try
oh_no_with_info
()
with
MyOtherException
(
x
,
y
)
>
x
+
y
let
boo
()
=
try
oh_no
()
with
MyOtherException
(
x
,
y
)
>
x
+
y
let
hd
xs
=
match
xs
with

[]
>
raise
MyUndesirableCondition

x
::_
>
x
let
foo1
=
hd
[
3
;
4
;
5
]
(*let foo2 = hd []*)
let
bar1
=
try
Some
(
hd
[
3
;
4
;
5
])
with
MyUndesirableCondition
>
None
let
bar2
=
try
Some
(
hd
[]
)
with
MyUndesirableCondition
>
None
let
rec
maxlist
(
xs
,
ex
)
=
(* int list * exn > int *)
match
xs
with

[]
>
raise
ex

x
::
[]
>
x

x
::
xs'
>
let
m
=
maxlist
(
xs'
,
ex
)
in
if
x
>
m
then
x
else
m
let
m1
=
maxlist
([
3
;
4
;
5
]
,
MyUndesirableCondition
)
let
m2
=
try
maxlist
([
3
;
4
;
5
]
,
MyUndesirableCondition
)
with
MyUndesirableCondition
>
42
(*let m3 = maxlist ([],MyUndesirableCondition)*)
let
m4
=
try
maxlist
([]
,
MyUndesirableCondition
)
with
MyUndesirableCondition
>
42
lecture/L11/lec11live.ml
0 → 100644
View file @
df63ea40
(* tail recursion *)
let
rec
sum
(
l
:
int
list
)
:
int
=
match
l
with

[]
>
0

x
::
xs
>
x
+
sum
xs
let
rec
countdown
(
n
:
int
)
:
int
list
=
if
n
=
0
then
[]
else
n
::
countdown
(
n

1
)
(* compiler optimization: tailcall optimization *)
let
sum2
(
l
:
int
list
)
:
int
=
(* "acc" stands for "accumulator" *)
let
rec
loop
((
l
:
int
list
)
,
(
acc
:
int
))
:
int
=
match
l
with

[]
>
acc

x
::
xs
>
loop
(
xs
,
acc
+
x
)
in
loop
(
l
,
0
)
let
_
=
sum2
([
1
;
2
;
3
])
(*
sum2([1; 2; 3])
sum2_helper([1;2;3],0)
sum2_helper([2;3],1)
sum2_helper([3],3)
sum2_helper([],6)
*)
let
countdown2
(
n
:
int
)
:
int
list
=
let
rec
countdown2_helper
((
n
:
int
)
,
(
acc
:
int
list
))
=
if
n
=
0
then
List
.
rev
acc
else
countdown2_helper
(
n

1
,
n
::
acc
)
in
countdown2_helper
(
n
,
[]
)
(*
if n = 0
then []
else n :: countdown (n1)
*)
\ No newline at end of file
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