Skip to content

Commit

Permalink
add: module03: tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
atsushifx committed Jun 22, 2024
1 parent 1acb451 commit ea5ec11
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ fun pow (x: int, y: int) =
fun cube (x: int) =
pow(x, 3)

val a1 = cube 4
(* = 4 * 4 * 4 *)

val a2 = pow(2, 2+2) + pow(4, 2) + cube(2) + 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(* Programming Languages A:SML/NJ *)
(* segment tuple *)

(* pairs *)
fun swap (pr : int*bool) = (* parameter1 = (x1:int, x2:bool *)
(#2 pr, #1 pr) (* return (x2:bool, x1:int) *)

fun sum_pair(pr : int * int) =
#1 pr + #2 pr

fun sum_two_pairs(pr1 : int * int, pr2 : int * int) =
(#1 pr1) + (#2 pr1) + #1 pr2 + #2 pr2

fun div_mod(x: int, y: int) =
(x div y, x mod y)


fun sort_pair(pr: int * int) =
if (#1 pr < #2 pr)
then (#1 pr, #2 pr)
else (#2 pr, #1 pr)


;; (* nested tuple (pair) *)
val x1 = (8, (true, 9))

val x2 = #1 (#2 x1)

val x3 = (#2 x1)

val x4 = ((3, 5), ((4, 8), (0, 0)))

0 comments on commit ea5ec11

Please sign in to comment.