mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
44 lines
949 B
Plaintext
44 lines
949 B
Plaintext
(deftype (Pair a b) [a a b b])
|
|
|
|
(defmodule PairRef
|
|
(defn = [p1 p2]
|
|
(and (= (Pair.a p1) (Pair.a p2))
|
|
(= (Pair.b p1) (Pair.b p2))))
|
|
|
|
(defn /= [p1 p2]
|
|
(not (PairRef.= p1 p2)))
|
|
|
|
(defn < [p1 p2]
|
|
(if (= (Pair.a p1) (Pair.a p2))
|
|
(< (Pair.b p1) (Pair.b p2))
|
|
(< (Pair.a p1) (Pair.a p2))))
|
|
|
|
(defn > [p1 p2]
|
|
(PairRef.< p2 p1)))
|
|
|
|
(defmodule Pair
|
|
(defn init-from-refs [r1 r2]
|
|
(Pair.init @r1 @r2))
|
|
|
|
(defn = [p1 p2]
|
|
(and (= (Pair.a &p1) (Pair.a &p2))
|
|
(= (Pair.b &p1) (Pair.b &p2))))
|
|
|
|
(defn /= [p1 p2]
|
|
(not (Pair.= p1 p2)))
|
|
|
|
(defn < [p1 p2]
|
|
(PairRef.< &p1 &p2))
|
|
|
|
(defn > [p1 p2]
|
|
(PairRef.> &p1 &p2))
|
|
|
|
(doc reverse "reverses a `Pair` `p` such that its first member is its second member and vice versa.")
|
|
(defn reverse [p]
|
|
(Pair.init @(Pair.b p) @(Pair.a p)))
|
|
|
|
(doc zero "return default values of the type inside `Pair`")
|
|
(defn zero []
|
|
(Pair.init (zero) (zero)))
|
|
)
|