Haskell Alternating Casing in Strings - haskell

I need to use the functions from Data.Char in order to create a function called camelcaser, which does what sounds like it does: uppercases and lowercases every letter in the pattern upper→lower→upper→lower.
I've attempted to use lexLitChar, to pull out an element then uppercase it, skip the next element until the entire string is completed. I also feel like this could be done with recursion but the type conversions seemed too difficult.
What I was going to do: String -> [(char,string)] -> [char,(char,string)] -> [char] -> String
camelcaser str = foldr f "" str
where f x y = (LexLit x) ++ y
LexLit x = Uppercase (lexlitChar x)
Uppercase (y,z) = toUpper y
I'm kind of stumped on where I'm going with this, anyone care to direct me in the correct path?

Well, if the resulting string is supposed to alternate between uppercase & lowercase, then you can just take the characters of the string two at a time: uppercase the first one, lowercase the second one, and then take the next pair. For example:
camelcaser (a:b:str) = toUpper a : toLower b : camelcaser str
You still need to work out how to handle strings of fewer than two characters, but that should be easy, and this answer has already gone past the basic nudging hint that I wanted it to be.

Here's another possibility. Maybe you can have some fun trying to figure out how it works.
camelCaser = map (\(i,c) -> if even i then toUpper c else toLower c) . zip [0..]

Related

How can I go through a list of numbers and compare it with an another list I am going through?

I want to input two strings, "Hello" and "Hi" for example, and I want to go through every element in the two strings simultaneously and compare every character one by one. The two strings length in the actual program should be 5, if a char is equal to the other, return true, otherwise return false.
The program I have in mind should see 'H' in "Hello" and 'H' from "Hi" and return true, then I want it to check the 'e' in "Hello" and the 'i' in "Hi" and return false. I want it to keep on doing that till there is nothing to compare. I think I might need to use recursion but I am not sure how to implement it in this program really.
I tried using x:xs with the maximum range of 5 so [0..5], but it didn't work at all.
My code (not working):
uncurryingString :: String -> Int -> Char
uncurryingString a b = a !! b
match :: String -> String -> [Int] -> Bool
match a b (x:xs)
| uncurryingString a [x+1 | x <- xs] == uncurryingString b [x+1 | x <- xs] = True
| otherwise = False
You're thinking about this way too complicated.
First, as a rule of thumb, you should never use !! (whether directly or via some helper – uncurryingString is in fact exactly the same as !! itself). If direct indexing is required, a list is not the right data structure. But very often direct indexing is not required, it's just what's customarily used in some other programming languages that don't have pattern matching to do it more elegantly.
In your application, you're deconstructing both strings in parallel. Well, you should express that with a pattern match:
match (a:as) (b:bs) (x:xs) = ...
And now you can simply compare a and b directly, no need for messing about with any list comprehensions or indexing operators. In the example input "Hello" and "Hi", both a and b will be 'H' here.
But you probably don't want to return True right there, because there's still the rest of the string to be matched. That's where recursion comes in.
Finally, you need to clauses in case not all of the input lists are nonempty. Try to figure that out yourself.
What you probably don't need at all is the extra [Int] argument (x:xs). It could make sense to have a depth-limiting argument, but that could be simply an Int. You would then do something like
match :: Eq a => [a] -> [a] -> Int -> Bool
match _ _ 0 = True -- lists are equal up to the specified max depth
match (a:as) (b:bs) n = ...
...
Using a list comprehension is often a good way to compute things in Haskell, but not in this particular case.
I could try this code:
[a==b | a <- "Hello", b <- "Hi"]
What does this do? You might think that this returns True, because the 'H' letters match - or you might thing that this returns False, because the other letters don't match. In fact, it does both - in effect, it is running multiple nested loops.
[True,False,False,False,False,False,False,False,False,False]
So, the takeaway is to use list comprehensions when you want nested loops, or just working through one dataset. Here we just want a single loop that works through the two words in parallel. Hence it must be recursion.

Comparing each element of a list to each element of another list

I'm trying to write a function that takes the first character of the first string, compares it to all the characters of the second string and if it finds the same character, replaces with a "-". Then it moves on to the second character of the first string, does the same comparison with each character (except the first character - the one we already checked) on the second string and so on. I want it to return the first string, but with the repeating characters swapped with the symbol "-".
E.g. if I put in comparing "good morning" "good afternoon", I'd like it to return "-----m---i-g"
I hope I explained it clearly enough. So far I've got:
comparing :: String -> String -> String
comparing a b =
if a == "" then ""
else if head a == head b then "-" ++ (comparing (tail a) (tail b))
else [head a] ++ (comparing (tail a) b)
The problem with this is it does not go through the second string character by character and I'm not sure how to implement that. I think I would need to call a recursive function on the 4th line:
if head a == ***the first character of tail b*** then "-" ++ (comparing (tail a) (tail b))
What could that function look like? Or is there a better way to do this?
First, at each recursive call, while you're iterating over the string a, you are for some reason also iterating over the string b at the same time. Look: you're passing only tail b to the next call. This means that the next call won't be able to look through the whole string b, but only through its tail. Why are you doing this?
Second, in order to see if a character is present in a string, use elem:
elem 'x' "xyz" == True
elem 'x' "abc" == False
So the second line of your function should look like this:
else if elem (head a) b then "-" ++ (comparing (tail a) b)
On a somewhat related note, use of head and tail functions is somewhat frowned upon, because they're partial: they will crash if the string is empty. Yes, I see that you have checked to make sure that the string is not empty, but the compiler doesn't understand that, which means that it won't be able to catch you when you accidentally change this check in the future.
A better way to inspect data is via pattern matching:
-- Comparing an empty string with anything results in an empty string
comparing "" _ = ""
-- Comparing a string that starts with `a` and ends with `rest`
comparing (a:rest) b =
(if elem a b then "-" else a) ++ comparing rest b
Rather than writing the recursive logic manually, this looks like a classic use case for map. You just need a function that takes a character and returns either that character or '-' depending on its presence in the other list.
Written out fully, this would look like:
comparing first second = map replace first
where replace c = if c `elem` second then '-' else c

Haskell char quotes

I've started to learn haskell for real recently, and I'm doing some exercises from wikibooks.
I'm doing exercise with RLE encoding, and I've come with solution like this:
import Data.List
rle :: String -> [(Int,Char)]
rle [] = []
rle xs = zip lengths chars
where
groups = group xs
lengths = map length groups
chars = map head groups
rle_toString :: [(Int, Char)] -> String
rle_toString [] = []
rle_toString (x:xs) = show (fst x ) ++ show (snd x) ++ rle_toString xs`
Not a very elegant solution, but it almost works. The problem is, that I get output like this: "7'a'8'b'7'j'6'q'3'i'7'q'1'p'1'a'16'z'2'n'". The single quotes with chars are not vetry elegant. How can I achieve output like: "7a8b7j6q3i7q1p1a16z2n"?
show is used to print values as they appear in Haskell source code, and thus puts single quotes around characters (and double quotes around strings, and so on). Use [snd x] instead to show just the character.
In Haskell, String is just shorthand for List of Char [Char]. For example, the String "Foo" can also be written like this: ['F','o','o']. So, to convert a single character to a string, just put in in brackets: [char].
The problem is your use of show on a character. show 'a' == "'a'".
The solution is to realize that strings are just lists of characters, so if c is a character, then the one-character string that contains c is just [c].

Iterating through a String and replacing single chars with substrings in haskell

I am trying to learn some Haskell and I find it difficult. I am having some issues with my
current project. The idea is that I have to go through a String and substitute certain chars
with new substrings. For instance if I have a String "FLXF" and I want to replace every F
with a substring called "FLF" the result should be "FLFLXFLF". Now I have been working on this
specific problem for hours. I have been reading up on types, different functions that might come in handy (map, fold, etc) and yet I have not been able to solve this problem.
The code below is some of the different tries I have had:
apply :: String -> String
apply [] = []
apply (x:xs) = if (x == 'F')
then do show "Hello"
apply xs
else (apply (xs))
This example here I was just trying to show hello every time I encountered a 'F', but all it shows is "", so this clearly does not work. I am really not sure an if else statement is the way to go here. I was also thinking the function map might do the trick. Here the code I was thinking about could look something like this:
map (\x y -> if y == 'F' then "FLD" else y) "FLF"
but that gives me a type error. So as you can see I am lost. Excuse me my poor knowledge to Haskell, but I am still new to it. I really hope some of you can help me out here or give me a push in the right direction. Feel free to ask questions if I have been unclear about something.
Thank you in advance!
John
map (\x y -> if y == 'F' then "FLD" else y) "FLF"
This is nearly right.
First... why does the function take two arguments?
map (\y -> if y == 'F' then "FLD" else y) "FLF"
The remaining type error is because the then branch gives a String, but the else branch gives a Char (the two branches must each give a value of the same type). So we'll make the else branch give a String instead (recall that String is a synonym for [Char]):
map (\y -> if y == 'F' then "FLD" else [y]) "FLF"
Now the problem is that this gives you a [String] value instead of a String. So we'll concatenate all those strings together:
concat (map (\y -> if y == 'F' then "FLD" else [y]) "FLF")
This combination of concat and map is common enough that there's a standard function that combines them.
concatMap (\y -> if y == 'F' then "FLD" else [y]) "FLF"
concatMap is the most intuitive thing here. This kind of combination between mapping over a data structure a function that does itself return the type of the data structure (in this case, a list) and combining the results back into a single "tight" list is indeed very common in Haskell, and indeed not only for lists.
I'd like to explain why your first attempt compiles at all, and what it actually does – because it's completely different from what you probably think!
apply (x:xs) = if (x == 'F')
that line is still perfectly clear: you just take the first char off the string and compare it to 'F'. At bit "pedestrian" to manually take the string apart, but fine. Well, the name you gave the function is not particularly great, but I'll stick with it here.
then do show "Hello"
now this is interesting. You probably think do starts a list of points, "first do this, then do that"... like in simple Hello, World-ish example programs. But always remember: in Haskell, there's normally no such thing as an order in which stuff is calculated. That only happens in the IO context. But there's no IO in your code!?!
Not sure if you've heard about what IO actually is, anyway here you go: it's a Monad. Those "mythical Haskell constructs you've only read about in story books"...
Indeed, though this might lead a bit far here, this question covers all there is to know about Monads! How is that?
Here's another (correct!) way do define your function.
apply' str = do
x <- str
if (x == 'F')
then "FLF"
else return x
So I'm using this weird do syntax, and it's not in IO, and it looks completely different from what you'd write in IO, but it works. How?
x <- str
In do notation, variable <- action always means something like "take one value out of this monadic thingy, and call it x". What you've probably seen is something like
response <- getLine
which means "take a user input out of the real world (out of the IO monad!) and call it response". In x <- str, it's a string that we have, not an IO action. So we take a character out of a string – nice and easy!
Actually, it's not quite right, though. "take a character" is what you do with apply (x:xs) = ..., which simply takes the first one. In contrast, x <- str actually takes all possible characters out of the string, one by one. If you're used to procedural languages, this may seem very inconsistent with response <- getLine, but in fact it's not: getLine also consists of every possible input that the user might give, and the program has to act according to this.
if (x == 'F')
nothing unexpected here, but
then "FLF"
whoah! Just like that? Let's first look at the next line
else return x
ok, this looks familiar, but actually it's not. In other languages, this would mean "we're done with our function, x is the result". But that's obviously not what happens here, because x is Char, and the "return type" of apply' is String. In Haskell, return actually has little to do with returning values from a function, instead it means "put that value into the monadic context that we're working in". If the monad were IO, that would be quite the same: give this value back to the real-world context (this does not mean to print the value or something, just to hand it on). But here, our context is a string, or rather a list (of chars, so it is a String).
Right, so if x is not 'F' we put it back into the string. That sounds reasonable enough, but what about then "FLF"? Note that I can also write it this way:
if (x == 'F')
then do
x' <- "FLF"
return x'
else return x
which means, I take all characters out of "FLW" and return them back into the overall result. But there's no need to only think about the final result, we can as well isolate only this part do { x' <- "FLF"; return x' } – and, quite obviously, its value is nothing but the string "FLF" itself!
So I hope you have now grasped why apply' works. Back to your version, though it actually doesn't make much sense...
then do
show "Hello"
apply xs
here we have a line that's not at the end of a do block, but doesn't have a <- in it. You normally see this in IO in something like
main = do
putStrLn "How ya doin'?"
response <- getLine
...
Remember that "output-only" actions have type IO() in Haskell, which means, they don't directly return any meaningful value, just the trivial value (). So you don't really care about this, but you could still evaluate it:
main = do
trivial <- putStrLn "Hello, let's see what this IO action returns:"
print trivial
compiles and outputs
Hello, let's see what this IO action returns:()
It would be stupid if we had to do this evaluating () all the time, so Haskell allows to just leave the () <- out. It's really just that!
So a line like show "Hello" in the middle of a do block basically means "take one character out of show "Hello" (which is simply a string with the value "\"Hello\""), but don't do anything else with this character / just throw it away".
The rest of your definition is just other recursive calls to apply, but because none of them does anything more interesting than throwing away characters, you eventually end up at apply [] = [], so that's the final result: an empty string.
if-then-else... I know that Haskell supports these, however, I'm very surprised that no one here removed them...
So below are my solutions for different cases of making replacements.
Replacing a character
Replacing words
Replacing through a function on each word
$ cat replace.hs
import Data.List (isPrefixOf)
replaceC :: Char -> Char -> String -> String
replaceC _ _ [] = []
replaceC a b (x:xs)
| x == a = b:replaceC a b xs
| otherwise = x:replaceC a b xs
replaceW :: String -> String -> String -> String
replaceW a b s = unwords . map replaceW' $ words s
where replaceW' x | x == a = b
| otherwise = x
replaceF :: (String -> String) -> String -> String
replaceF f = unwords . map f . words
string = "Hello world ^fg(blue)"
main = do
print string
print $ replaceC 'o' 'z' string
print $ replaceW "world" "kitty" string
print . replaceF f . replaceW "world" "kitty" $ replaceC 'H' 'Y' string
where f s | "^" `isPrefixOf` s = '^':'^':drop 1 s
| otherwise = s
$ runhaskell replace.hs
"Hello world ^fg(blue)"
"Hellz wzrld ^fg(blue)"
"Hello kitty ^fg(blue)"
"Yello kitty ^^fg(blue)"
Your basic error was that you wanted to replace a Char in a String with a String.
This is impossible because String is a list of Char and a Char is a Char and not a short String. Neither is a String ever a Char, even if its length is 1.
Hence, what you really wanted is to replace some Char with some other Chars. Your approach was promising and could have been completed like so:
replace [] = [] -- nothing to replace in an empty string
replace (c:cs) = if c == 'F' then 'F':'L':'F':replace cs
else c:replace cs

Character & strings

I am new in haskell and I have a problem (aka homework).
So, I have a list with a tuple – a string and an integer:
xxs :: [([Char], Integer)]
I need to know how many of the strings in xxs start with a given character.
Let me exemplify:
foo 'A' [("Abc",12),("Axx",34),("Zab",56)]
Output: 2
foo 'B' [("Abc",12),("Bxx",34),("Zab",56)]
Output: 1
My best attempt so far:
foo c xxs = length (foldl (\acc (x:xs) -> if x == c then c else x) [] xxs)
But, of course, there's something VERY wrong inside the lambda expression.
Any suggestion?
Thanks.
You can use a fold, but I would suggest another way, which breaks the problem in three steps:
transform the input list to the list of first letters. You can use map for this
filter out all elements not equal to the given Char
take the length of the remaining list
Obviously the first step is the hardest, but not as hard as it looks. For doing it you just have to combine somehow the functions fst and head, or even easier, map twice.
You can write this as a simple one-liner, but maybe you should start with a let:
foo c xxs = let strings = map ...
firstLetters = map ...
filteredLetters = filter ...
in length ...
There are a few problems with your attempt:
You plan to use foldl to construct a shorter list and then to take its length. While it is possible, filter function is much better suited for that task as #landei suggests
foldl can be used to accumulate the length without constructing a shorter list. See the answer of #WuXingbo - his answer is incorrect, but once you realize that length is not needed at all with his approach, it should be easy for you to come with correct solution.
Somewhat contradictory to common sense, in a lazy language foldr is faster and uses less memory than foldl. You should ask your teacher why.
I would rewrite foo as
foo :: Char -> [(String, Int)] -> Int
foo c = length . filter ((==c).head.fst)
fst fetches the first element of a two-element tuple.
(==c) is a one-argument function that compares its input with c (see http://www.haskell.org/tutorial/functions.html paragraph 3.2.1 for better explanation).

Resources