NESL Exercise Solutions
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Exercise 6
Exercise 7
Exercise 1
function dotprod(a,b) = sum({x * y: x in a ; y in b}); dotprod([2,3,1],[6,1,4]);
Exercise 2
function is_palindrome(a,b) = if #a == #b then all({x == y: x in a; y in reverse(b)}) else f; is_palindrome("yes","no"); is_palindrome("now","won");
Exercise 3
function stock(a) = max_val({x - y : x in a; y in min_scan(a)}); stock([12, 11, 10, 8, 5, 8, 9, 6, 7, 7, 10, 7, 4, 2]);
Exercise 4
function remove_t(words) = {word in words | not(any({char == `t: char in word})) }; remove_t(["this","is","a","test","of","the", "remove_t","function"]);
Exercise 5
function primes(n) = {i in [2:n] | not(any({mod(i,j) == 0: j in [2:1+isqrt(i)]})) }; primes(100);
Exercise 6
function row_val(row,j) = let col = {v : (i,v) in row | i == j}; in if #col==0 then 0.0 else col[0]; function get_column(a,j) = {row_val(row,j) : row in A}; A = [[(0, 2.0), (1, -1.0) ], [(0, -1.0), (1, 2.0), (2, -3.0) ], [ (1, -1.0), (2, 2.0), (3, -1.0)], [ (2, -1.0), (3, 2.0)]]; get_column(A,2);
Exercise 7
function my_sum(a) = if #a == 1 then a[0] else let even = even_elts(a); odd = odd_elts(a); sums = {e + o : e in even ; o in odd}; in my_sum(sums); my_sum([2,1,9,-3,4,5,8,7]);
Guy Blelloch,
blelloch@cs.cmu.edu
.