import Data.List digits 0 = [] digits n = (digits (div n 10)) ++ [mod n 10] is_palindromic n = n == (foldr (\x y -> x + y * 10) 0 (digits n)) main = print (foldl1' max (filter is_palindromic [ x * y | x <- [100..999], y <- [100..999] ]))
こう書くのと似たように書きたい。
#include <iostream> #include "itertools.h" using namespace std; using namespace itertools; bool is_palindromic(int n) { return n == to_number(digits(n)); } int main() { cout << reduce1(max<int>, filter(is_palindromic, map([] (tuple<int,int> x) { return fst(x) * snd(x); }, product(range<>(100, 1000))))) << endl; }
なかなかご機嫌。Haskellよりは遅いがかなり速くなった。例外使っているから遅いのだろうが、毎回次があるかいちいち問い合わせれば速くなるだろうか。