Download presentation
Presentation is loading. Please wait.
1
Tutorial 7
2
add_bin_list : bool list list -> bool list
Week 7 Exercise 1 Implement a function add_bin_list that adds a list of binary numbers. add_bin_list : bool list list -> bool list example: add_bin_list [ [true; true; false; true]; [false; false; true]; [true; false; true]] = [false; false; true; false; true; false; false]
3
Least significant bit (LSB) Most significant bit (MSB)
Week 7 Exercise 1 Implement a function add_bin_list that adds a list of binary numbers. add_bin_list : bool list list -> bool list example: add_bin_list [ [true; true; false; true]; [false; false; true]; [true; false; true]] = [false; false; true; false; true; false; false] Least significant bit (LSB) Most significant bit (MSB)
4
Least significant bit (LSB) Most significant bit (MSB)
Week 7 Exercise 1 1011 Implement a function add_bin_list that adds a list of binary numbers. add_bin_list : bool list list -> bool list example: add_bin_list [ [true; true; false; true]; [false; false; true]; [true; false; true]] = [false; false; true; false; true; false; false] Least significant bit (LSB) Most significant bit (MSB)
5
From Lecture let add_3 b1 b2 b3 = match b1, b2, b3 with
| true, true, true -> true, true | true, true, false | true, false, true | false, true, true -> true, false | true, false, false | false, true, false | false, false, true -> false, true | false, false, false -> false, false;; let rec add_bin' n1 n2 carry = match n1, n2 with | [], [] -> [carry] | [], n2 -> add_bin' [false] n2 carry | n1, [] -> add_bin' n1 [false] carry | d1 :: d1s, d2 :: d2s -> let (carry, d) = add_3 d1 d2 carry in d :: (add_bin' d1s d2s carry);; let rec add_bin n1 n2 = add_bin' n1 n2 false;;
6
solution let add_bin_list = List.fold_left add_bin [false];;
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> ’a List.fold_left f a [b1; ...; bn] is f (... (f (f a b1) b2) ...) bn.
7
mul_bin_pow : bool list -> int -> bool list
Week 7 Exercise 2 Write a function mul_bin_pow n k which multiplies a binary number n by the number k-th power of 2. mul_bin_pow : bool list -> int -> bool list For example: mul_bin_pow [true; true; false; true] 3 = [false; false; false; true; true; false; true]
8
solution Multiplying a number by the k-th power of its base
Shift the digits/bits k places to the left Pad with zeros Example: in decimal multiplying any number by 10 555 *10 =5550 The same with binary: multiplying any number by 2, shift bits to the left and add zero to the LSB 111 *(1000)=111000 2^3
9
solution let rec nfalse = function | 0 -> []
| n -> false :: (nfalse (n - 1));; let mul_bin_pow n k = (nfalse n;;
10
mul_bin : bool list -> bool list -> bool list
Week 7 Problem Implement the function mul_bin which multiplies two binary numbers. mul_bin : bool list -> bool list -> bool list For example: mul_bin [false; true; true] [true; false; true] = [false; true; true; true; true]
11
solution let rec bin_to_pows n = function | [] -> []
| true :: ns -> n :: (bin_to_pows (n + 1) ns) | false :: ns -> (bin_to_pows (n + 1) ns);; val bin_to_pows : int -> bool list -> int list = <fun> let shift bs ps = List.map (fun k -> mul_bin_pow bs k) ps;; val shift : bool list -> int list -> bool list list = <fun> let rec mul_bin m n = add_bin_list (shift m (bin_to_pows 0 n));; val mul_bin : bool list -> bool list -> bool list = <fun>
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.