Created
March 2, 2014 02:30
-
-
Save scvnc/9300985 to your computer and use it in GitHub Desktop.
Sum base-10 digits of number in racket
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
#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