We're a place where coders share, stay up-to-date and grow their careers.

Haskell:

main :: IO () main = print $ sumEvenFib 4000000 fibList :: [Integer] fibList = 0 : 1 : zipWith (+) fibList (tail fibList) sumEvenFib :: Integer -> Integer sumEvenFib x = sum . takeWhile (<x) . filter even $ fibList

Racket:

#lang scheme (define (fib n) (letrec ((fib-aux (lambda (n a b) (if (= n 0) a (fib-aux (- n 1) b (+ a b)))))) (fib-aux n 0 1))) (define (sum-of-even-fibs limit i sum) (let ((cur (fib i))) (cond ((> cur 4000000) sum) ((if (even? cur) (sum-of-even-fibs limit (+ 1 i) (+ cur sum)) (sum-of-even-fibs limit (+ 1 i) sum)))))) (print (sum-of-even-fibs 4000000 1 0))

Haskell:

Racket: