Skip to content

Instantly share code, notes, and snippets.

@dave-maldonado
Created May 29, 2013 19:20
Show Gist options
  • Save dave-maldonado/5673015 to your computer and use it in GitHub Desktop.
Save dave-maldonado/5673015 to your computer and use it in GitHub Desktop.
Project Euler #2 in Racket
;; This is a solution in Racket for the second problem on Project Euler
;; 2013 Dave Maldonado
#lang racket
(define (count i n)
(if (= i n)
'()
(cons i (count (+ i 1) n))))
(define (list-sum lst)
(cond
((null? lst)
0)
((pair? (car lst))
(+(list-sum (car lst)) (list-sum (cdr lst))))
(else
(+ (car lst) (list-sum (cdr lst))))))
(define (fib x)
(define (fib-iter x a b)
(if (= x 0)
a
(fib-iter (- x 1) b (+ a b))))
(fib-iter x 0 1))
(define (even? x)
(or (= x 0)
(= 0 (modulo x 2))))
(define (my-filter pred lst)
(cond ((null? lst) '())
((pred (car lst))
(cons (car lst) (my-filter pred (cdr lst))))
(else (my-filter pred (cdr lst)))))
(list-sum (my-filter even? (map fib (count 0 34))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment