Skip to content

Instantly share code, notes, and snippets.

@ayato-p
Last active December 12, 2015 10:39
Show Gist options
  • Save ayato-p/4760414 to your computer and use it in GitHub Desktop.
Save ayato-p/4760414 to your computer and use it in GitHub Desktop.
named-letの練習。 空白文字はカウントしない。
(define (str-count str)
(let loop ((ls (string->list str))
(count 0))
(let ((c (if (pair? ls)
(car ls)
ls))
(inc (lambda (x) (+ x 1))))
(cond
((null? ls) count)
((char-whitespace? c) (loop (cdr ls) count))
(else
(loop (cdr ls) (inc count)))))))
;; Stringで受け取った数値文字列を数値のリストとして扱いたい
(define (string->int-list str)
(let loop ((char-list (string->list str))
(acc '()))
(cond
((null? char-list) (reverse acc))
(else (let ((c (car char-list))
(c2i (lambda (x) (- x 48))))
(loop (cdr char-list)
(cons (c2i (char->integer c)) acc)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment