Capitalize a string

637 views
Skip to first unread message

Vapid

unread,
Jan 27, 2015, 8:30:00 PM1/27/15
to elm-d...@googlegroups.com
I'm very new to functional programming. I'm trying to determine the best way to capitalize a string in Elm. I think I want to import string, char and then uncons, but I'm having trouble seeing how to pattern match against the returned monad to capitalize the first character and then glue the head back to the tail.

Thanks.
Message has been deleted

Sean Corfield

unread,
Jan 27, 2015, 8:59:03 PM1/27/15
to elm-d...@googlegroups.com
On Jan 27, 2015, at 5:29 PM, Vapid <vapid...@gmail.com> wrote:
> I'm very new to functional programming. I'm trying to determine the best way to capitalize a string in Elm. I think I want to import string, char and then uncons, but I'm having trouble seeing how to pattern match against the returned monad to capitalize the first character and then glue the head back to the tail.

When you use String.uncons you get back a value that is a Maybe data type, specifically Maybe (Char, String), and that can have one of two values: Just (c, s) - just a (character, string) tuple - or Nothing - no value. You can use case to pattern match on that:

case String.uncons s of
Just (c, ss) -> -- do something with c and ss
Nothing -> -- s was empty string

The second case is simple: the result should be s (the empty string) because capitalize "" should be "".

The first case is more interesting: the result should be the upper case version of c followed by (cons'd onto) the remainder of the string, which is ss:

Just (c, ss) -> String.cons (Char.toUpper c) ss

Putting that all together - and adding the ability to capitalize each word in a sentence - we get:

import Text
import String
import Char
import List

capitalize : String -> String
capitalize s =
case String.uncons s of
Just (c,ss) -> String.cons (Char.toUpper c) ss
Nothing -> s

capitalizeAll : String -> String
capitalizeAll s =
String.join " " <| List.map capitalize <| String.words s
-- or: String.join " " (List.map capitalize (String.words s))

main =
Text.plainText (capitalizeAll "hello, world!")

Sean Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)



Hassan Hayat

unread,
Jan 27, 2015, 9:00:22 PM1/27/15
to elm-d...@googlegroups.com
Sorry, I misunderstood your question initially. I mistook capitalizing for uppercasing.

This is the solution I came up with and I'll walk you through it:

import String (..)
import Char 

capitalize : String -> String
capitalize string = 
  case uncons string of
    Nothing -> ""
    Just (head, tail) -> 
      append (fromChar (Char.toUpper head)) tail

You're right on the idea. So, uncons gives you a Maybe (Char, String) where the char is the first letter and the String is the rest of the letters.

case is how you do pattern matching in Elm. So it reads, if uncons string is:

1) Nothing. If I get nothing from unconsing it must mean that I have the empty string. So, let's just return the empty string cuz there's nothing better to do.

2) Just (head, tail). The head in this case is the first letter, the one you want to capitalize. so we call Char.toUpper on it. But Char.toUpper returns a char, so we need to convert it to a string, so we call fromChar which converts a char to a string. Note: there's also a general purpose toString function but all that does is surround anything with double quotes, so if you use that you'll drag in the single quotes along for the ride and that's not what you want.

And then, finally, we append that uppercased letter onto the rest of the string.

Hassan Hayat

unread,
Jan 27, 2015, 9:03:07 PM1/27/15
to elm-d...@googlegroups.com
Alternatively, you can just use String.cons. Sorry, I missed that

import String (..)
import Char

capitalize : String -> String
capitalize string =
  case uncons string of
   Nothing -> ""
   Just (head, tail) ->
      cons (Char.toUpper head) tail

In this case, you just "cons" the uppercased character onto the tail.

Max Goldstein

unread,
Jan 27, 2015, 10:16:03 PM1/27/15
to elm-d...@googlegroups.com
We should probably have common string processing methods in a third party library somewhere.

Vapid Babble

unread,
Jan 28, 2015, 10:29:58 AM1/28/15
to elm-d...@googlegroups.com
Excellent. Thanks for the help and thorough explanation. 

-jmwise
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages