Created
December 6, 2015 19:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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