sum
operation is defined to have
constant depth (I'll motivate this in class).
In the following code replace "Fill in"
with the appropriate
code.
This should return 19.
You might find the Nesl Quick Reference Guide helpful.
Hint: you might find the function all
useful. To test
if two characters are equal, you can use ==
.
The Nesl Quick Reference Guide.
stock([12, 11, 10, 8, 5, 8, 9, 6, 7, 7, 10, 7, 4, 2]);would return 5 since we can buy at 5 on day 5 and sell at 10 on day 11. This has a simple linear time serial solution. Write a NESL program to solve the stock market problem with work complexity O(n) and constant depth. Hint: you might try one of the scan operations (the scan operations are defined to take constant depth).
The Nesl Quick Reference Guide.
t
in
it. The expression
(char == `t)will test if
char
is the letter t
. Note that
it is a back quote, not a forward quote.
It should return
["is","a","of"]
The Nesl Quick Reference Guide.
[2...1+sqrt(i)]
.
The function isqrt
takes the square root of an integer.
Hint: you can use the expression [2:n]
to
generate a sequences of integers from 2 to n. The function mod(i,j)
takes the modulus of i
and j
(ie. the remainder).
The Nesl Quick Reference Guide.
i
.
For example, for a matrix:
the functionA = [[(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)
should extract column 2
and return
[0.0, -3.0, 2.0, -1.0]
.
Hint: You might find the
construct [0:n]
useful.
The Nesl Quick Reference Guide.
sum
operation, which sums the elements of a sequences.
Here is one way to write it:
Another way to write it would be to take the odd indexed elements
and the even indexed elements of a
, sum them elementwise
and then recursively sum this new sequence. Write a my_sum
function that uses this other method.
You can assume the length of the input is a power of 2.
Hint: you might find the odd_elts
and even_elts
functions useful.
The Nesl Quick Reference Guide.
Here are the solutions to the exercises. You should try them all, however, before looking at the solutions.