Skip to content

Commit

Permalink
Ica6 and practice functions
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-byrne committed Oct 15, 2024
1 parent 67f1999 commit d5b8061
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/ica04/ica4.sml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ fun cubeR(a: real) = a * a * a;
fun combine((i1, i2), (i3, r1), (r2, i4), (r3, r4)) = real(i1 + i2) * (real(i3) + r1) * (r2 + real(i4)) * (r3 + r4);

(* Question 5 *)
(* Bad Version (not using pattern matching) *)
fun firstChars(a, b) =
if (a = "" andalso b = "") then ""
else if a = "" then implode([hd(explode(b))])
else if b = "" then implode([hd(explode(a))])
else implode([hd(explode(a))] @ [hd(explode(b))]);

(* Good Version (using pattern matching) *)
fun firstChars("", "") = ""
| firstChars(a, "") = implode([hd(explode(a))])
| firstChars("", b) = implode([hd(explode(b))])
| firstChars(a, b) = implode([hd(explode(a))] @ [hd(explode(b))]);

(* Question 6 *)
fun sortPairs((a, b), (c, d)) = if (a + b) > (c + d) then ((a, b), (c, d)) else ((c, d), (a, b));

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit d5b8061

Please sign in to comment.