Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cse341-23wi-public
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
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
Woo
cse341-23wi-public
Commits
f8a49c8f
Commit
f8a49c8f
authored
2 years ago
by
Dan Grossman
Browse files
Options
Downloads
Patches
Plain Diff
make a file for starting lec04 with -- page breaks, one version of natsupto, code from lec03, etc.
parent
b07485e8
Branches
master
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lecture/lec04/lec04-live-starting-version.ml
+134
-0
134 additions, 0 deletions
lecture/lec04/lec04-live-starting-version.ml
with
134 additions
and
0 deletions
lecture/lec04/lec04-live-starting-version.ml
0 → 100644
+
134
−
0
View file @
f8a49c8f
#
utop_prompt_dummy
let
_
=
UTop
.
set_show_box
false
(* Local Bindings *)
let
silly1
(
z
:
int
)
=
let
x
=
if
z
>
0
then
z
else
34
in
let
y
=
x
+
9
in
if
x
>
y
then
x
*
2
else
y
*
y
let
silly2
()
=
(* poor style, just showing let expressions are expressions *)
let
x
=
1
in
(
let
x
=
2
in
x
+
1
)
+
(
let
y
=
x
+
2
in
y
+
1
)
let
celsius_of_fahrenheit
(
tempF
:
float
)
=
(
tempF
-.
32
.
0
)
*.
5
.
0
/.
9
.
0
(* example where let expressions probably make code more readable even though
no computations are repeated *)
(* will have better ways to do this with variant types *)
let
is_first_warmer
((
temp1
:
float
)
,
(
is_celsius1
:
bool
)
,
(
temp2
:
float
)
,
(
is_celsius2
:
bool
))
=
let
temp1C
=
if
is_celsius1
then
temp1
else
celsius_of_fahrenheit
temp1
in
let
temp2C
=
if
is_celsius2
then
temp2
else
celsius_of_fahrenheit
temp2
in
temp1C
>
temp2C
(* example where we avoid repeating a computation, but it would not matter much *)
let
rec
squaring_sequence
((
x
:
float
)
,
(
n
:
int
))
=
if
n
=
0
then
[]
else
let
sq
=
x
*.
x
in
sq
::
squaring_sequence
(
sq
,
n
-
1
)
let
rec
range
((
lo
:
int
)
,
(
hi
:
int
))
=
if
lo
<
hi
then
lo
::
range
(
lo
+
1
,
hi
)
else
[]
let
nats_upto1
(
x
:
int
)
=
range
(
0
,
x
)
let
nats_upto2
(
x
:
int
)
=
let
rec
range
((
lo
:
int
)
,
(
hi
:
int
))
=
if
lo
<
hi
then
lo
::
range
(
lo
+
1
,
hi
)
else
[]
in
range
(
0
,
x
)
let
nats_upto3
(
x
:
int
)
=
let
rec
loop
(
lo
:
int
)
=
(* loop is NOT a special word, could call it fred *)
if
lo
<
x
then
lo
::
loop
(
lo
+
1
)
else
[]
in
loop
0
(* badly named: evaluates to 0 on empty list *)
let
rec
bad_max
(
xs
:
int
list
)
=
if
xs
=
[]
then
0
(* bad semantic style *)
else
if
List
.
tl
xs
=
[]
then
List
.
hd
xs
else
if
List
.
hd
xs
>
bad_max
(
List
.
tl
xs
)
then
List
.
hd
xs
else
bad_max
(
List
.
tl
xs
)
let
rec
better_max
(
xs
:
int
list
)
=
if
xs
=
[]
then
0
(* bad semantic style *)
else
if
List
.
tl
xs
=
[]
then
List
.
hd
xs
else
let
tl_max
=
better_max
(
List
.
tl
xs
)
in
if
List
.
hd
xs
>
tl_max
then
List
.
hd
xs
else
tl_max
let
rec
good_max1
(
xs
:
int
list
)
=
if
xs
=
[]
then
None
else
let
tl_ans
=
good_max1
(
List
.
tl
xs
)
in
if
not
(
tl_ans
=
None
)
&&
Option
.
get
tl_ans
>
List
.
hd
xs
then
tl_ans
else
Some
(
List
.
hd
xs
)
let
good_max2
(
xs
:
int
list
)
=
if
xs
=
[]
then
None
else
let
rec
max_nonempty
(
xs
:
int
list
)
=
if
List
.
tl
xs
=
[]
(* xs better not be [] *)
then
List
.
hd
xs
else
let
tl_ans
=
max_nonempty
(
List
.
tl
xs
)
in
if
List
.
hd
xs
>
tl_ans
then
List
.
hd
xs
else
tl_ans
in
Some
(
max_nonempty
xs
)
(*let does_not_typecheck = (good_max1 [3;4;2]) + 4*)
(* A t option is *not* an int *)
let
risky_but_works
=
(
Option
.
get
(
good_max1
[
3
;
4
;
2
]))
+
2
(* type-checks, raises exception *)
(* let risky_and_fails = (Option.get (good_max1 [])) + 2*)
let
propagate_option
=
let
x
=
good_max1
[
3
;
4
;
2
]
in
if
Option
.
is_some
x
then
Some
((
Option
.
get
x
)
+
2
)
else
None
(* benefits of no mutation *)
(* does not matter if returns an alias *)
let
sort_pair
(
pr
:
int
*
int
)
:
int
*
int
=
if
fst
pr
<
snd
pr
then
pr
(* (fst pr, snd pr) *)
else
(
snd
pr
,
fst
pr
)
(* naturally and efficiently introduces sharing and we don't care *)
let
rec
append
((
xs
:
'
a
list
)
,
(
ys
:
'
a
list
))
=
if
xs
=
[]
then
ys
else
List
.
hd
xs
::
append
(
List
.
tl
xs
,
ys
)
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