You can do
is_palindrome? = fn (n) -> s = to_string(n); s == String.reverse(s) end
try do
for x <- 999..100, y <- 999..x, n = x * y, is_palindrome?.(n), do: throw(n)
catch
n -> n
end
which is the equivalent of your second example.
However, that will not find the largest palindrome. To do that you want
Enum.reduce_while 999..100, 0, fn
x, highest when highest >= x * 999 -> {:halt, highest}
x, highest ->
highest =
Stream.map(999..x, &(x * &1))
|> Enum.reduce(highest, &(&1 > &2 && is_palindrome?.(&1) && &1 || &2))
{:cont, highest}
end






















