Skip to content

Instantly share code, notes, and snippets.

@simonewebdesign
Created March 26, 2017 16:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonewebdesign/2fb7ec5d60d81d580c3675ce5e4ec5a7 to your computer and use it in GitHub Desktop.
Save simonewebdesign/2fb7ec5d60d81d580c3675ce5e4ec5a7 to your computer and use it in GitHub Desktop.
tail-recursive sum in Erlang and LFE
sum(L) -> sum(L,0).
sum([], Total) -> Total;
sum([H|T], Total) -> sum(T, H+Total).
(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