Created
March 26, 2017 16:08
-
-
Save simonewebdesign/2fb7ec5d60d81d580c3675ce5e4ec5a7 to your computer and use it in GitHub Desktop.
tail-recursive sum in Erlang and LFE
This file contains hidden or 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
sum(L) -> sum(L,0). | |
sum([], Total) -> Total; | |
sum([H|T], Total) -> sum(T, H+Total). |
This file contains hidden or 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
(defun sum (l) (sum l 0)) | |
(defun sum | |
(('() total) total) | |
(((cons h t) total) (sum t (+ h total)))) | |
; or using a ``cons`` literal instead of the constructor form: | |
(defun sum (l) (sum l 0)) | |
(defun sum | |
(('() total) total) | |
((`(,h . ,t) total) (sum t (+ h total)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment