Define a primitive data type: Difference between revisions

Content deleted Content added
No edit summary
Line 238: Line 238:


=={{header|OCaml}}==
=={{header|OCaml}}==
<ocaml>
<lang ocaml>
exception Out_of_bounds
exception Out_of_bounds


Line 266: Line 266:
;;
;;
(** val op : ('a -> 'a -> 'a) -> 'a bounded -> 'a bounded -> 'a bounded *)
(** val op : ('a -> 'a -> 'a) -> 'a bounded -> 'a bounded -> 'a bounded *)
</ocaml>
</lang>


Using in the interactive top level:
Using in the interactive top level:
<ocaml>
<lang ocaml>
# let range = mk_bounds 1 10 ;;
# let range = mk_bounds 1 10 ;;
val range : int bounds = {min = 1; max = 10}
val range : int bounds = {min = 1; max = 10}
Line 284: Line 284:
# op ( + ) a b ;;
# op ( + ) a b ;;
- : int bounded = {value = 7; bounds = {min = 1; max = 10}}
- : int bounded = {value = 7; bounds = {min = 1; max = 10}}
</ocaml>
</lang>


which can be used with floats in the same way:
which can be used with floats in the same way:
<ocaml>
<lang ocaml>
# let rf = mk_bounds 1.0 10.0 ;;
# let rf = mk_bounds 1.0 10.0 ;;
val rf : float bounds = {min = 1.; max = 10.}
val rf : float bounds = {min = 1.; max = 10.}
Line 295: Line 295:
op ( +. ) a b ;;
op ( +. ) a b ;;
- : float bounded = {value = 7.8; bounds = {min = 1.; max = 10.}}
- : float bounded = {value = 7.8; bounds = {min = 1.; max = 10.}}
</ocaml>
</lang>


=={{header|Perl}}==
=={{header|Perl}}==