Changeset 63 for Deliverables/D4.1/Nibble.ml
- Timestamp:
- Sep 13, 2010, 11:04:55 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Deliverables/D4.1/Nibble.ml
r62 r63 1 1 (*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*) 2 (* FILENAME: Nibble.ml *) 3 (* DESCRIPTION: An ADT implementing nibbles, and common operations on them. *) 4 (* CREATED: 10/09/2010, Dominic Mulligan *) 2 (* FILENAME: Nibble.ml *) 3 (* DESCRIPTION: An ADT implementing 4 bit nibbles, and common operations on *) 4 (* them. *) 5 (* CREATED: 10/09/2010, Dominic Mulligan *) 5 6 (* BUGS: *) 6 7 (*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*) … … 13 14 type nibble 14 15 15 val from_bits: bit -> bit -> nibble 16 val from_bits: bit -> bit -> bit -> bit -> nibble 17 val from_bit: bit -> nibble 16 18 val from_int: int -> nibble option 19 val from_string: string -> nibble option 20 21 val to_bits: nibble -> (bit * bit * bit * bit) 22 val to_bit: nibble -> bit option 23 val to_int: nibble -> int 24 val to_string: nibble -> string 17 25 18 26 val get_bit_at: int -> nibble -> bit option … … 29 37 val iter_bit: (bit -> string) -> nibble -> string 30 38 val zip_bit: (bit -> bit -> bit) -> nibble -> nibble -> nibble 31 val pretty: nibble -> string32 39 end 33 40 … … 35 42 struct 36 43 type bit = Bit.bit 37 type nibble = bit * bit 44 type nibble = bit * bit * bit * bit 38 45 39 let from_bits l h = (l, h) 46 let from_bits b1 b2 b3 b4 = (b1, b2, b3, b4) 47 let from_bit h = (Bit.from_bool false, Bit.from_bool false, Bit.from_bool false, h) 40 48 let from_int int_val = 41 49 match int_val with 42 0 -> Some (Bit.from_int 0, Bit.from_int 0) 43 | 1 -> Some (Bit.from_int 0, Bit.from_int 1) 44 | 2 -> Some (Bit.from_int 1, Bit.from_int 0) 45 | 3 -> Some (Bit.from_int 1, Bit.from_int 1) 50 0 -> Some (Bit.from_int 0, Bit.from_int 0, Bit.from_int 0, Bit.from_int 0) 51 | 1 -> Some (Bit.from_int 0, Bit.from_int 0, Bit.from_int 0, Bit.from_int 1) 52 | 2 -> Some (Bit.from_int 0, Bit.from_int 0, Bit.from_int 1, Bit.from_int 0) 53 | 3 -> Some (Bit.from_int 0, Bit.from_int 0, Bit.from_int 1, Bit.from_int 1) 54 | 4 -> Some (Bit.from_int 0, Bit.from_int 1, Bit.from_int 0, Bit.from_int 0) 55 | 5 -> Some (Bit.from_int 0, Bit.from_int 1, Bit.from_int 0, Bit.from_int 1) 56 | 6 -> Some (Bit.from_int 0, Bit.from_int 1, Bit.from_int 1, Bit.from_int 0) 57 | 7 -> Some (Bit.from_int 0, Bit.from_int 1, Bit.from_int 1, Bit.from_int 1) 58 | 8 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 0, Bit.from_int 0) 59 | 9 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 0, Bit.from_int 1) 60 | 10 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 1, Bit.from_int 0) 61 | 11 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 1, Bit.from_int 1) 62 | 12 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 0, Bit.from_int 0) 63 | 13 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 0, Bit.from_int 1) 64 | 14 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 1, Bit.from_int 0) 65 | 15 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 1, Bit.from_int 1) 46 66 | _ -> None 67 let from_string 68 69 let to_bit (l, h) = 70 let (bl, bh) = Bit.to_bool l, Bit.to_bool h in 71 match (bl, bh) with 72 (false, b) -> Some (Bit.from_bool b) 73 | (true, b) -> None 74 let to_int (l, h) = (2 * Bit.to_int l) + Bit.to_int h 75 let to_string = iter_bit Bit.to_string 47 76 48 77 let get_bit_at index (l, h) = … … 79 108 str_l ^ str_h 80 109 let zip_bit f (l1, h1) (l2, h2) = (f l1 l2, f l2 h2) 81 let pretty = iter_bit Bit.pretty82 110 end 83 111
Note: See TracChangeset
for help on using the changeset viewer.