Computing a Symbolic ConvolutionTheir direct style solution uses a generalization convolve' that produces the convolution of two lists where the first list may be shorter than the second, as well as the extra prefix of the second list. I've written it here in pseudo-Haskell with dependent type annotations representing the lengths of the lists:
Given two lists [x1,x2,...,xn-1,xn] and [y1,y2,...,yn-1,yn], where n is not known in advance, write a function that constructs
[(x1,yn),(x2,yn-1),...,(xn-1,y2),(xn,y1)]
in n recursive calls and with no auxiliary list.
What it's doing is recurring down the first list until it hits the end and then pairing up the elements as it returns; whatever elements are leftover from the beginning of the second list get placed in the second "return register."convolve' :: [a]m → [b]n → ([(a,b)]m, [b]n-m) -- n ≥ m
convolve' [] ys = ([], ys)
convolve' (x:xs) ys =
let (r, (y:ys')) = convolve' xs ys in
((x,y):r, ys')
convolve :: [a]n → [b]n → [(a,b)]n
convolve xs ys = r where (r, []) = convolve' xs ys
The other solution uses a similar generalization, but in CPS:
Same thing, really. The interesting part isn't the direct style vs. CPS, but rather the discovery of the invariant, i.e., the generalization of the problem. Once we know to write a function that a) allows the first list to be shorter than the second, and b) produces both the partial solution and the remaining elements of the second list, the rest is actually not so challenging.convolve' :: [a]m → [b]n → ([(a,b)]m → [b]n-m → c) → c -- n ≥ m
convolve' [] ys k = k [] ys
convolve' (x:xs) ys k =
convolve' xs ys (λr (y:ys') . k ((x,y):r) ys')
convolve :: [a]n → [b]n → [(a,b)]n
convolve xs ys = convolve' xs ys (λr [] . r)
Update: No, no, no... I described the purpose of convolve' wrong. It produces a pair containing 1) the convolution of a given suffix of the original first list with the corresponding prefix of the second list, and 2) the remaining suffix of the second list.
2 comments:
TomTom is a location technology specialist who uses unique technologies to navigate people to their destination with the help of GPS MAP. The continuous TomTom update is available for latest advancement so that user can get safer driver over any of place.
tomtom.com/getstarted |
tomtom get started |
www.tomtom.com/getstarted |
We understand the demands of the industry. That’s why we work tirelessly to ensure you have an efficient and smooth Stone Supplier Edmonton
Stone Supplier Edmonton
Post a Comment