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
9c48a7b7
Commit
9c48a7b7
authored
Jan 22, 2022
by
Dan Grossman
Browse files
rearrange lecture 10 to do currying first
parent
b7351597
Changes
1
Hide whitespace changes
Inline
Sidebyside
lecture/lec10/lec10.ml
View file @
9c48a7b7
...
...
@@ 3,54 +3,6 @@
let _ = UTop.set_show_box false
*)
(* function composition (righttoleft, like math) *)
let
compose
=
fun
f
>
fun
g
>
fun
x
>
f
(
g
x
)
(* infix notation for composition *)
let
(
%
)
=
compose
let
double_plus_one
x
=
compose
(
fun
x
>
x
+
1
)
(
fun
x
>
2
*
x
)
x
let
_
=
double_plus_one
3
(* "variablestyle" let binding is better style *)
let
double_plus_one
=
compose
(
fun
x
>
x
+
1
)
(
fun
x
>
2
*
x
)
let
_
=
double_plus_one
3
(* also nice to use the infix operator *)
let
double_plus_one
=
(
fun
x
>
x
+
1
)
%
(
fun
x
>
2
*
x
)
let
_
=
double_plus_one
3
let
sqrt_of_abs
i
=
Float
.
sqrt
(
float_of_int
(
Int
.
abs
i
))
let
sqrt_of_abs
i
=
(
Float
.
sqrt
%
float_of_int
%
Int
.
abs
)
i
let
sqrt_of_abs
=
Float
.
sqrt
%
float_of_int
%
Int
.
abs
let
_
=
sqrt_of_abs
(

8
)
(* pipeline operator (lefttoright) *)
(* actually builtin to OCaml, but here's the definition *)
let
(
>
)
x
f
=
f
x
let
sqrt_of_abs
i
=
i
>
Int
.
abs
>
float_of_int
>
Float
.
sqrt
let
_
=
sqrt_of_abs
(

8
)
(* also lefttoright *)
let
pipeline_option
(
f
,
g
)
=
fun
x
>
match
f
x
with

None
>
None

Some
y
>
g
y
let
sqrt_if_positive
=
pipeline_option
((
fun
i
>
if
i
>
0
then
Some
(
float_of_int
i
)
else
None
)
,
(
fun
x
>
Some
x
)
%
sqrt
)
let
_
=
sqrt_if_positive
3
let
_
=
sqrt_if_positive
(

3
)
(*  *)
(* Currying *)
let
sorted3_tuple
(
x
,
y
,
z
)
=
...
...
@@ 108,16 +60,62 @@ let forall p = List.fold_left (fun acc x > acc && p x) true
let
has_zero
=
exists
(
fun
x
>
x
=
0
)
let
no_zeros
=
forall
(
fun
x
>
x
<>
0
)
(* currying is the norm in OCaml, even if partial application unlikely *)
let
rec
append
xs
ys
=
match
xs
with

[]
>
ys

x
::
xs'
>
x
::
append
xs'
ys
(* currying is the norm in OCaml, even if partial application
is
unlikely *)
let
rec
append
xs
ys
=
match
xs
with

[]
>
ys

x
::
xs'
>
x
::
append
xs'
ys
(*
revisit compose
*)
(*
next idiom: combining functions
*)
(* function composition (righttoleft, like math) *)
let
compose
=
fun
f
>
fun
g
>
fun
x
>
f
(
g
x
)
let
compose_shorter
f
g
x
=
f
(
g
x
)
(* infix notation for composition *)
let
(
%
)
=
compose
let
double_plus_one
x
=
compose
(
fun
x
>
x
+
1
)
(
fun
x
>
2
*
x
)
x
let
_
=
double_plus_one
3
(* "variablestyle" let binding is better style *)
let
double_plus_one
=
compose
(
fun
x
>
x
+
1
)
(
fun
x
>
2
*
x
)
let
_
=
double_plus_one
3
(* also nice to use the infix operator *)
let
double_plus_one
=
(
fun
x
>
x
+
1
)
%
(
fun
x
>
2
*
x
)
let
_
=
double_plus_one
3
let
sqrt_of_abs
i
=
Float
.
sqrt
(
float_of_int
(
Int
.
abs
i
))
let
sqrt_of_abs
i
=
(
Float
.
sqrt
%
float_of_int
%
Int
.
abs
)
i
let
sqrt_of_abs
=
Float
.
sqrt
%
float_of_int
%
Int
.
abs
let
_
=
sqrt_of_abs
(

8
)
(* pipeline operator (lefttoright) *)
(* actually builtin to OCaml, but here's the definition *)
let
(
>
)
x
f
=
f
x
let
sqrt_of_abs
i
=
i
>
Int
.
abs
>
float_of_int
>
Float
.
sqrt
let
_
=
sqrt_of_abs
(

8
)
(* also lefttoright *)
let
pipeline_option
f
g
=
fun
x
>
match
f
x
with

None
>
None

Some
y
>
g
y
let
sqrt_if_positive
=
pipeline_option
(
fun
i
>
if
i
>
0
then
Some
(
float_of_int
i
)
else
None
)
((
fun
x
>
Some
x
)
%
sqrt
)
let
_
=
sqrt_if_positive
3
let
_
=
sqrt_if_positive
(

3
)
(* converting between curried and tupled versions of a function *)
let
curried_of_paired
f
x
y
=
f
(
x
,
y
)
let
paired_of_curried
f
(
x
,
y
)
=
f
x
y
...
...
@@ 157,7 +155,7 @@ let _ = remove_empty_lists [[]; [true]]
*)
(* workaround
1
: go back to bad style :\ *)
(* workaround
if you want polymrophics
: go back to
"
bad
"
style :\ *)
let
remove_empty_lists
ls
=
List
.
filter
(
fun
x
>
List
.
length
x
>
0
)
ls
let
_
=
remove_empty_lists
[[];
[
1
]]
let
_
=
remove_empty_lists
[[];
[
true
]]
...
...
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