Skip to content

Instantly share code, notes, and snippets.

@scvnc
Created March 2, 2014 02:30
Show Gist options
  • Save scvnc/9300985 to your computer and use it in GitHub Desktop.
Save scvnc/9300985 to your computer and use it in GitHub Desktop.
Sum base-10 digits of number in racket
#lang racket
;; Racket is a trip
;; ASCI-Value minus 48
(define (parseDigit c)
(- (char->integer c) 48))
;; Sums a list of characters as integers
(define (addDigitString charList)
(cond
[(= 1 (length charList))
(parseDigit (first charList))]
[ else ( +
(parseDigit (first charList))
(addDigitString (rest charList)))]
)
)
;; Sums the base-10 digits of a number
(define (addDigits num)
(addDigitString (string->list (number->string num))))
(addDigits 123);; 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment