From 6c514e5fc29829b85fbcebeb73a3d82c4f4544a2 Mon Sep 17 00:00:00 2001 From: Gregory Date: Tue, 2 Jun 2020 03:39:00 +0300 Subject: [PATCH] remove pattern matching --- sml/week1/date.sml | 51 ++++++++++++++++++++++++----------------- sml/week1/operators.sml | 17 ++++++++++++++ 2 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 sml/week1/operators.sml diff --git a/sml/week1/date.sml b/sml/week1/date.sml index 8077bfb..f057dca 100644 --- a/sml/week1/date.sml +++ b/sml/week1/date.sml @@ -1,23 +1,13 @@ +use "operators.sml"; + (* year * month * day *) type Date = int * int * int -(* - 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 $ +(* can't use pattern matching for now *) +(* fun fold f acc lst = + case lst of + [] => acc + | head :: tail => fold f (f head acc) tail *) fun fold f acc lst = if lst = [] @@ -64,15 +54,34 @@ fun range from to = generate from to [] |> reverse end -(* naive sort, will blow up stack :( *) -fun sort f lst = +(* naive sort, will blow up the stack. Can't use pattern matching for now :( *) +(* fun sort f lst = case lst of [] => [] | hd :: [] => [hd] | first :: second :: rest => if f first second then second :: first :: sort f rest - else first :: second :: 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 + end fun is_older ((y1, m1, d1): Date, (y2, m2, d2): Date): bool = let @@ -169,7 +178,7 @@ fun what_month (day: int): int = number_before_reaching_sum (day, months) + 1 end -(* correct but to complex for such problem *) +(* correct but too complex for such problem *) (* fun month_range (day1: int, day2: int): int list = let fun append_month day months = what_month day :: months diff --git a/sml/week1/operators.sml b/sml/week1/operators.sml new file mode 100644 index 0000000..35a2111 --- /dev/null +++ b/sml/week1/operators.sml @@ -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 $ \ No newline at end of file