Recursive function causing a stack overflow

You’re being hit by filter‘s laziness. Change (filter …) to (doall (filter …)) in your recur form and the problem should go away. A more in-depth explanation: The call to filter returns a lazy seq, which materialises actual elements of the filtered seq as required. As written, your code stacks filter upon filter upon filter…, …

Read more

How to make a recursive lambda

Think about the difference between the auto version and the fully specified type version. The auto keyword infers its type from whatever it’s initialized with, but what you’re initializing it with needs to know what its type is (in this case, the lambda closure needs to know the types it’s capturing). Something of a chicken-and-egg …

Read more

Longest word chain from a list of words

You can use recursion to explore every “branch” that emerges when every possible letter containing the proper initial character is added to a running list: words = [‘giraffe’, ‘elephant’, ‘ant’, ‘tiger’, ‘racoon’, ‘cat’, ‘hedgehog’, ‘mouse’] def get_results(_start, _current, _seen): if all(c in _seen for c in words if c[0] == _start[-1]): yield _current else: for …

Read more

Algorithm for iterating over an outward spiral on a discrete 2D grid from the origin

There’s nothing wrong with direct, “ad-hoc” solution. It can be clean enough too. Just notice that spiral is built from segments. And you can get next segment from current one rotating it by 90 degrees. And each two rotations, length of segment grows by 1. edit Illustration, those segments numbered … 11 10 7 7 …

Read more