remove pattern matching
This commit is contained in:
parent
79a441d61c
commit
6c514e5fc2
2 changed files with 47 additions and 21 deletions
|
@ -1,23 +1,13 @@
|
||||||
|
use "operators.sml";
|
||||||
|
|
||||||
(* year * month * day *)
|
(* year * month * day *)
|
||||||
type Date = int * int * int
|
type Date = int * int * int
|
||||||
|
|
||||||
(*
|
(* can't use pattern matching for now *)
|
||||||
Pipe operator.
|
(* fun fold f acc lst =
|
||||||
Simply to write code like "data |> fun" instead "fun data".
|
case lst of
|
||||||
Then you can "pipe" results to functions via partial application:
|
[] => acc
|
||||||
|
| head :: tail => fold f (f head acc) tail *)
|
||||||
fun square a b = a * b
|
|
||||||
fun sum a b = a + b
|
|
||||||
|
|
||||||
10 |> sum 10 |> square 2 // 40
|
|
||||||
*)
|
|
||||||
fun |> (x, f) = f x
|
|
||||||
|
|
||||||
(* composition operator *)
|
|
||||||
fun $ (f, x) = f x
|
|
||||||
|
|
||||||
infix |>
|
|
||||||
infix $
|
|
||||||
|
|
||||||
fun fold f acc lst =
|
fun fold f acc lst =
|
||||||
if lst = []
|
if lst = []
|
||||||
|
@ -64,15 +54,34 @@ fun range from to =
|
||||||
generate from to [] |> reverse
|
generate from to [] |> reverse
|
||||||
end
|
end
|
||||||
|
|
||||||
(* naive sort, will blow up stack :( *)
|
(* naive sort, will blow up the stack. Can't use pattern matching for now :( *)
|
||||||
fun sort f lst =
|
(* fun sort f lst =
|
||||||
case lst of
|
case lst of
|
||||||
[] => []
|
[] => []
|
||||||
| hd :: [] => [hd]
|
| hd :: [] => [hd]
|
||||||
| first :: second :: rest =>
|
| first :: second :: rest =>
|
||||||
if f first second
|
if f first second
|
||||||
then second :: first :: sort f rest
|
then second :: first :: sort f rest
|
||||||
|
else first :: second :: sort f rest *)
|
||||||
|
|
||||||
|
(* also will blow up the stack *)
|
||||||
|
fun sort f lst =
|
||||||
|
if lst = []
|
||||||
|
then []
|
||||||
|
else
|
||||||
|
if tl lst = []
|
||||||
|
then lst
|
||||||
|
else
|
||||||
|
let
|
||||||
|
val first = hd lst
|
||||||
|
val second = hd $ tl lst
|
||||||
|
val rest = tl $ tl lst
|
||||||
|
val greater = f first second
|
||||||
|
in
|
||||||
|
if greater
|
||||||
|
then second :: first :: sort f rest
|
||||||
else first :: second :: sort f rest
|
else first :: second :: sort f rest
|
||||||
|
end
|
||||||
|
|
||||||
fun is_older ((y1, m1, d1): Date, (y2, m2, d2): Date): bool =
|
fun is_older ((y1, m1, d1): Date, (y2, m2, d2): Date): bool =
|
||||||
let
|
let
|
||||||
|
@ -169,7 +178,7 @@ fun what_month (day: int): int =
|
||||||
number_before_reaching_sum (day, months) + 1
|
number_before_reaching_sum (day, months) + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
(* correct but to complex for such problem *)
|
(* correct but too complex for such problem *)
|
||||||
(* fun month_range (day1: int, day2: int): int list =
|
(* fun month_range (day1: int, day2: int): int list =
|
||||||
let
|
let
|
||||||
fun append_month day months = what_month day :: months
|
fun append_month day months = what_month day :: months
|
||||||
|
|
17
sml/week1/operators.sml
Normal file
17
sml/week1/operators.sml
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
(*
|
||||||
|
Pipe operator.
|
||||||
|
Simply to write code like "data |> fun" instead "fun data".
|
||||||
|
Then you can "pipe" results to functions via partial application:
|
||||||
|
|
||||||
|
fun square a b = a * b
|
||||||
|
fun sum a b = a + b
|
||||||
|
|
||||||
|
10 |> sum 10 |> square 2 // 40
|
||||||
|
*)
|
||||||
|
fun |> (x, f) = f x
|
||||||
|
|
||||||
|
(* composition operator *)
|
||||||
|
fun $ (f, x) = f x
|
||||||
|
|
||||||
|
infix |>
|
||||||
|
infix $
|
Loading…
Reference in a new issue