I'm playing with the lens package and I'm trying to capitalize a string using only lens.
Basically I want to call toUpper on each first element of every words. That seems to be easy to with it, but I can't figure out at all how to do it. Do I need a traversable ? How do I split by spaces etc ...
It's not really an isomorphism to call words then unwords because it'll convert repeated spaces to single ones, but let's pretend:
words :: Iso' String [String]
words = iso Prelude.words Prelude.unwords
Now we can capitalize words by building a lens which focuses on the first letter of each word and applying over and toUpper
capitalize :: String -> String
capitalize = over (words . traverse . _head) toUpper
capitalize xs = xs & words <&> _head %~ toUpper & unwords
Okay, that's the solution, but how to get there? Lets remove some lens parts. Exchange (<&>) with fmap and (&) with ($):
capitalize xs = unwords $ fmap (_head %~ toUpper) $ words $ xs
This looks familar. _head %~ f will apply f on the first element of the list. At the end, this is (almost*) equivalent to
capitalize xs = unwords $ fmap (\(x:xs) -> toUpper x : xs) $ words $ xs
which you are probably familiar with.
* _head also takes care of the empty list case
A solution that doesn't collapse repeated spaces:
import Control.Lens
import Data.List.Split
import Data.List.Split.Lens
import Data.Char
capitalize :: String -> String
capitalize = view $ splitting (whenElt isSpace) traversed.to (over _head toUpper)
Related
I'm just starting out in Haskell and this is like the third thing I'm writing, so, naturally, I'm finding myself a little stumped.
I'm trying to write a bit of code that will take a string, delete the spaces, and capitalize each letter of that string.
For example, if I input "this is a test", I would like to get back something like: "thisIsATest"
import qualified Data.Char as Char
toCaps :: String -> String
toCaps [] = []
toCaps xs = filter(/=' ') xs
toCaps (_:xs) = map Char.toUpper xs
I think the method I'm using is wrong. With my code in this order, I am able to remove all the spaces using the filter function, but nothing becomes capitalize.
When I move the filter bit to the very end of the code, I am able to use the map Char.toUpper bit. When I map that function Char.toUpper, it just capitalizes everything "HISISATEST", for example.
I was trying to make use of an if function to say something similar to
if ' ' then map Char.toUpper xs else Char.toLower xs, but that didn't work out for me. I haven't utilized if in Haskell yet, and I don't think I'm doing it correctly. I also know using "xs" is wrong, but I'm not sure how to fix it.
Can anyone offer any pointers on this particular problem?
I think it might be better if you split the problem into smaller subproblems. First we can make a function that, for a given word will capitalize the first character. For camel case, we thus can implement this as:
import Data.Char(toUpper)
capWord :: String -> String
capWord "" = ""
capWord (c:cs) = toUpper c : cs
We can then use words to obtain the list of words:
toCaps :: String -> String
toCaps = go . words
where go [] = ""
go (w:ws) = concat (w : map capWord ws)
For example:
Prelude Data.Char> toCaps "this is a test"
"thisIsATest"
For Pascal case, we can make use of concatMap instead:
toCaps :: String -> String
toCaps = concatMap capWord . words
Inspired by this answer from Will Ness, here's a way to do it that avoids unnecessary Booleans and comparisons:
import qualified Data.Char as Char
toCaps :: String -> String
toCaps = flip (foldr go (const [])) id
where go ' ' acc _ = acc Char.toUpper
go x acc f = f x:acc id
Or more understandably, but perhaps slightly less efficient:
import qualified Data.Char as Char
toCaps :: String -> String
toCaps = go id
where go _ [] = []
go _ (' ':xs) = go Char.toUpper xs
go f (x :xs) = f x:go id xs
There are a number of ways of doing it, but if I were trying to keep it as close to how you've set up your example, I might do something like:
import Data.Char (toUpper)
toCaps :: String -> String
toCaps [] = [] -- base case
toCaps (' ':c:cs) = toUpper c : toCaps cs -- throws out the space and capitalizes next letter
toCaps (c:cs) = c : toCaps cs -- anything else is left as is
This is just using basic recursion, dealing with a character (element of the list) at a time, but if you wanted to use higher-order functions such as map or filter that work on the entire list, then you would probably want to compose them (the way that Willem suggested is one way) and in that case you could probably do without using recursion at all.
It should be noted that this solution is brittle in the sense that it assumes the input string does not contain leading, trailing, or multiple consecutive spaces.
Inspired by Joseph Sible 's answer, a coroutines solution:
import Data.Char
toCamelCase :: String -> String
toCamelCase [] = []
toCamelCase (' ': xs) = toPascalCase xs
toCamelCase (x : xs) = x : toCamelCase xs
toPascalCase :: String -> String
toPascalCase [] = []
toPascalCase (' ': xs) = toPascalCase xs
toPascalCase (x : xs) = toUpper x : toCamelCase xs
Be careful to not start the input string with a space, or you'll get the first word capitalized as well.
So basically I want to split my string with two conditions , when have a empty space or a diferent letter from the next one.
An example:
if I have this string ,"AAA ADDD DD", I want to split to this, ["AAA","A","DDD","DD"]
So I made this code:
sliceIt :: String -> [String]
sliceIt xs = words xs
But it only splits the inicial string when an empty space exists.
How can I also split when a caracter is next to a diferent one?
Can this problem be solve easier with recursion?
So you want to split by words and then group equal elements in each split. You have the functions for doing so,
import Data.List
sliceIt :: String -> [String]
sliceIt s = concatMap group $ words s
sliceItPointFree = concatMap group . words -- Point free notation. Same but cooler
split :: String -> [String]
split [] = []
split (' ':xs) = split xs
split (x:xs) = (takeWhile (== x) (x:xs)) : (split $ dropWhile (== x) (x:xs))
So this is a recursive definition where there are 2 cases:
If head is a space then ignore it.
Otherwise, take as many of the same characters as you can, then call the function on the remaining part of the string.
I am very new to Haskell. I am trying to return a list of strings from a given string (which could contain non-letter characters) but I get a single string in the list.
The below code shows What I have tried so far:
toLowerStr xs = map toLower xs
--drop non-letters characters
dropNonLetters xs = words $ (filter (\x -> x `elem` ['a'..'z'])) $ toLowerStr xs
lowercase all the characters by using toLower function
remove non-letter characters by using filter function
return a list of strings by using words function
I think the filter function is removing the white spaces and therefore it becomes a single string. I tried using isSpace function but I don't know exactly how to implement it in this case.
What is it that I am doing wrong? I get this output:
λ> dropNonLetters "ORANGE, apple! APPLE!!"
["orangeappleapple"]
But I want to achieve the below output:
λ> dropNonLetters "ORANGE, apple! APPLE!!"
["orange","apple","apple"]
I think the filter function is removing the white spaces and therefore it becomes a single string.
That is correct. As filter predicate you write \x -> x `elem` ['a'..'z']. ['a'..'z'] is a list that contains lowercase letters, so for whitespace, the predicate will fail, and thus you should allow spaces as well.
We can for instance add the space character to the list:
dropNonLetters xs = words $ (filter (\x -> x `elem` (' ':['a'..'z'])))) $ toLowerStr xs
But this is inelegant and does not really explain itself. The Data.Char module however ships with two functions that are interesting here: isLower :: Char -> Bool, and isSpace :: Char -> Bool. We can use this like:
dropNonLetters xs = words $ (filter (\x -> isLower x || isSpace x)) $ toLowerStr xs
isLower and isSpace are not only more "descriptive" and elegant. Usually these functions will be faster than a membership check (which will usually be done in O(n)), and furthermore it will also take into account tabs, new lines, etc.
We can also perform an eta-reduction on the function:
dropNonLetters = words . (filter (\x -> isLower x || isSpace x)) . toLowerStr
This then produces:
Prelude Data.Char> dropNonLetters "ORANGE, apple! APPLE!!"
["orange","apple","apple"]
I advise you to rename the function dropNonLetters, since now it does not fully explain that it will generate a list of words. Based on the name, I would think that it only drops non-letters, not that it converts the string to lowercase nor that it constructs words.
here's an example of separating characters into separate string lists:
sortNumbers :: [Char] -> [String]
sortNumbers args = filter (\strings ->strings/= "") $ zipWith (\x numbers -> filter (\char -> char == numbers) x) (repeat args)
['1'..'9']
I want to split a String in Haskell.
My inicial String would look something like
["Split a String in Haskell"]
and my expected output would be:
["Split","a","String","in","Haskell"].
From what i've seen, words and lines don't work here, because i have the type [String] instead of just String.
I've tried Data.List.Split, but no luck there either.
import Data.List
split = (>>= words)
main = print $ split ["Split a String in Haskell"]
map words makes [["Split","a","String","in","Haskell"]] from ["Split a String in Haskell"], and concat makes [x] from [[x]]. And concat (map f xs) is equal to xs >>= f. And h xs = xs >>= f is equal to h = (>>= f).
Another way, more simple would be
split = words . head
I'm trying to make a Haskell program using interact, that returns the most used word and the # of times it appears. I've seen examples with sort- but I don't need to know the counts for all words, I only need the most repeated word. So far I have:
import Data.List -- (sort)
import Data.Char -- (isAlpha, toLower)
import Data.Ord -- (maximumBy)
main =
interact
$ unwords
-- comment: here show the size of the list and the word (probably head)
. maximumBy(comparing length)
. group
. sort
. words
. map (\char -> if isAlpha char then toLower char else ' ')
The above compiles. maximumBy gives the most used word like this:
[the, the, the, the, the, the, the, the...]
for the number of times the word "the" appears in the text; and I have verified that "the" is the most used word for the text I've supplied.
What I want to output is something like this: "the, 318"
I tried the following which only gives the first letter "t" and 3:
import Data.List -- sort
import Data.Char -- isAlpha, toLower
import Data.Ord -- maximumBy
main =
interact
$ unwords
. map (\(n, w) -> show n ++ ", " ++ show w)
. map (\s -> (length s, head s))
. maximumBy(comparing length)
. group
. sort
. words
. map (\char -> if isAlpha char then toLower char else ' ')
Which gives the output:
"3, 't' 3, 't' 3, 't' 3, 't' ..."
Anyone know what I'm doing wrong?
The map in map (\s -> (length s, head s)) means that the function \s -> (length s, head s) is applied to each "the" instead of to the list of "the"'s, repeatedly giving the length and the first character of "the". So removing the map should work better. You will also need to fix up the final two steps (remove the unwords and the map):
$ (\(n, w) -> show n ++ ", " ++ show w)
. (\s -> (length s, head s))
. maximumBy(comparing length)
More efficiently, you can apply map (\s -> (length s, head s)) earlier in the pipeline than the maximum, which allows you to
Avoid recomputing the length in each comparison the maximum function does
Use just plain maximum instead of maximumBy. (This may be slightly different in which word is chosen if there are two equally frequent ones, since it then compares the actual strings.)
In other words, you can use
$ (\(n, w) -> show n ++ ", " ++ show w)
. maximum
. map (\s -> (length s, head s))
Or to put it all together:
import Data.List (group, sort)
import Data.Char (isAlpha, toLower)
main =
interact
$ (\(n, w) -> show n ++ ", " ++ show w)
. maximum
. map (\s -> (length s, head s))
. group
. sort
. words
. map (\char -> if isAlpha char then toLower char else ' ')
Note also how I changed the import statements to use the official syntax for explicitly naming what you are importing. I'd strongly recommend that over using comments, as this actually gave me error messages pointing out one function (group) you had missed and one (maximumBy) you had listed with the wrong module.