Skip to content

Instantly share code, notes, and snippets.

@DevWurm
Created December 6, 2015 19:19
Show Gist options
  • Save DevWurm/47059fd5e0d46f1ef44e to your computer and use it in GitHub Desktop.
Save DevWurm/47059fd5e0d46f1ef44e to your computer and use it in GitHub Desktop.
Scheme procedures, which sum up numbers, for demonstrating the difference between functional and imperative paradigm
; calculating the sum of all numbers between start and end (including start and end)
(define functionalSum
(lambda (start end)
(if (= end start)
end
(+ end
(functionalSum
start
(- end 1))))))
; calculating the sum of all numbers between start and end (including start and end)
(define imperativeSum
(lambda (start end)
(begin
(define sum 0)
(while
(<= start end)
(begin
(set! sum
(+ sum start))
(set! start
(+ start 1))))
sum)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment