Wow. In addition to getting my question answered, I learned about 6 other
things. Thank you so much, everyone!
Glenn
--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an
Here's another:
(define/contract (ucfirst str)
(-> string? string?)
(match (regexp-match #px"^([^a-z]*)(.)(.+)" str)
[(list _ prefix first-letter rest-of-string)
(~a prefix (string-upcase first-letter) rest-of-string)]))
-> (ucfirst "cat dog")
"Cat dog"
-> (ucfirst " Cat dog")
"
Robby's answer was more idiomatic Racket, and mine was
idomatic-Scheme-and-also-OK-Racket, by habit. :)
I'd suggest reading both implementations. As you learn more Racket,
you'll start to get a feel for your preferred linguistic style(s), and
you'll notice different people have a lot more
On Mon, Jun 19, 2017 at 8:12 PM, Glenn Hoetker wrote:
> I'm quite new to Racket/LISP, so I hope this isn't breathtakingly obvious.
> Can someone please tell me the best way to capitalize just the first word in
> a multiword string. So, given the string "it was a dark and
Here's another way to implement it. Fun. :)
#lang racket
(provide
(contract-out
[cap-first (-> string? string?)]))
(define (cap-first s)
(apply
string
(for/list ([c (in-string s)]
[i (in-naturals)])
(if (= i 0)
(char-upcase c)
c
(module+ test
Welcome to Racket!
One intro-to-Racket-compared-to-some-other-languages thing I'll just say
upfront is that you don't want to modify the string itself, not that you
asked to. (Not all Racket strings are mutable. Plus, mutating
introduces a bunch more possibilities for bugs, and for string
I don't think there's a library function that does what you want, so you'd
need to define your own. Here's one way to do it:
(define (capitalize-first-letter str)
(cond
[(non-empty-string? str)
(define first-letter-str
(substring str 0 1))
(define rest-str
(substring
I'm quite new to Racket/LISP, so I hope this isn't breathtakingly obvious. Can
someone please tell me the best way to capitalize just the first word in a
multiword string. So, given the string "it was a dark and stormy night", I
would like to get "It was a dark and stormy night". I see
8 matches
Mail list logo