I want to define a function ‘wordsLength’ that will calculate the length of the words in a given sentence - haskell

Sample Input – wordsLength “This is Arya”, Sample output – [4,2,4]
I can get the length for a list of string but not for the string itself.

This is the function you have:
length :: [a] -> Int
What you would like to do is write lengths which preserves the structure of the input list and applies length to each one of its elements.
lengths :: [[a]] -> [Int]
Now, look at the type of map, can you see how to combine length and map to get lengths?
map :: (a -> b) -> [a] -> [b]

length <$> words "This is Arya"

Related

Why is the type for [tail, init, reverse], [[a] -> [a]]?

This question is in one of the Haskell textbook exercises, not so smart question.
Question: What are the types of the following values?
[tail, init, reverse]
Solution: [[a] -> [a]]
But why?
tail, init and reverse are functions defined on a generic list, [a]. You read [a] as "a list of any type abstracted as a", in particular, if a = Int, you get [Int] - list of integers.
Now, function that takes a list and outputs a list of the same type has signature [c] -> [c] or, if c = a, [a] -> [a]. The letter picked up does not matter as long as you have the same latter on both sides, because [a] -> [b] would mean that you take somehow a list of as and map it to a list of bs - which - in general - a different type. You could say that a = Int and b = String and therefore you'd have to map Int to String anyhow; also you could say that b = a an thus reduce to the [a] -> [a] case.
The last one: [tail, init, reverse] is obviously yet another list, where a = [b] -> [b]. So you got [ [b] -> [b] ] type or, equivalently, [ [a] -> [a] ] or [ [k] -> [k] ]. Again: particular letter does not matter as long as you stick to the chosen one.
The type of the following value, not "values". singular. List of three elements is a value. Or did you mean "each of the values in the given list"? then they all must have the same type. since a list has a type [t] for some t. which implies all its elements have that same type t, whatever that is. then since tail :: [a] -> [a], so must be the other two that appear in the same list.
On their own they could each refer to a different letter i.e. "type variable". but inside the same list, they all must be in agreement. thus the type of the list as a whole is [ [a] -> [a] ].

Re-write 'map intToDigit' with a fold...

So this one seems like it should be super-simple... but I'm not sure where to stick the 'fold' in (obviously you could fold either way)...
It says "write a function ( intToString :: [Int] -> [Char] ) using a fold, that mimics this map:
map intToDigit [5,2,8,3,4] == "52834"
And then says, "For the conversion, use intToDigit :: Int -> Char from Data.Char."
I'm not entirely sure I get the point... but yet it doesn't seem like it should be that hard -- you're just reading in the list (folding it in, I get how folds work in general) from either the left or right and doing the conversion... but I'm not sure how to set it up.
It is not difficult, think about the definition foldr (or foldl) of List:
foldr::(a -> b -> b) -> b -> [a] -> b
Here (a->b->b) is the step function which will be applied on each element of list [a], b is your target.
Now, you have a list of Int ([Int]), and need to convert to [Char] (or String).
Relpace [a] by [5,2,8,3,4], b by []::[Char] (your target) and (a->b->b) by step function :
foldr step ([]::[Char]) [5,2,8,3,4]
We have known that step function has type (a->b->b), specifically, (Int->[Char]->[Char]), the job need to do just convert Int to [Char], as mentioned in question: intToDigit can be helped to convert Int to Char and (:) operator can append element at the head of List so:
step x s = intToDigit x : s
where s is [Char] (or String), put them all:
foldr (\x s->intToDigit x : s) [] [5,2,8,3,4]

Match type error when counting palindromes in a list in Haskell

I'm getting a match type [Char] with String -> Bool error:
isPalindrome :: String -> Bool
isPalindrome w = w == reverse w
countPalindromes :: [String] -> Int
countPalindromes ss = length (filter (== isPalindrome) ss)
countPalindromes uses isPalindrome to check if a string is a palindrome.
I now have a different issue than in my first question about this palindrome count task. The first issue was solved, and I was instructed to open a new question to solve the new issue. Which is why this is a different question.
isPalindrome :: String -> Bool, which is to say that it expects a string, then gives you a boolean to say whether or not that string is a palindrome.
(==) :: Eq a => a -> a -> Bool, which is to say that it expects two values of typeclass Eq (in other words: anything equatable) and tells you whether or not they are equal.
Pair those together and you get (== isPalindrome) :: (String -> Bool) -> Bool*. You've handed (==) a String -> Bool value, so it's expecting one more and will tell you if the two are equal. That's not quite what you want, though. You're not comparing two functions....
In fact, you're not comparing any two things at all. You only want to see which values passed to countPalindromes return True when called by isPalindrome. That's what filter is for!
filter :: (a -> Bool) -> [a] -> [a]
It's looking for an a -> Bool to pass as the first argument. This is the function that will be deciding what makes it through the filter and what doesn't, and in this case you want to use some derivation of isPalindrome. Looking again at isPalindrome in this context we see:
isPalindrome :: String -> Bool
that looks an awful lot like an a -> Bool function! Let's try substituting all the as in filter's type signature with String.
filter :: (String -> Bool) -> [String] -> [String]
That looks like it's exactly what you're wanting! Try, then, using filter isPalindrome instead of filter (== isPalindrome).
* nitpick: functions (more generally: values of type (-> r)) are not members of the Eq typeclass so you'd actually get an error saying that you can't tell if a String -> Bool is equal to another String -> Bool. It's not relevant to the problem at-hand, though, so I'm pretending that's not an issue and burying this explanation down here.

Checking for all lowercase & Combining a Word with a List - Haskell

I am having some trouble writing 2 separate functions. One of which "isWord s" is to take in a word and return "true" if all characters of the word are lowercase, otherwise return "false". Below is an example:
isWord "foo" = True; isWord "fo3o" = False
It must use this definition:
isWord :: String -> Bool
isWord s =
The other function "prefixWords str strs" which takes a word and a list of words and combines the two in a list with a ":" between each word. Shown below as an example:
prefixWords "word:" ["aa", "a3", "foo"] = ["word:aa", "word:foo"]
It must use this definition:
prefixWords :: String -> [String] -> [String]
prefixWords str strs =
Any help is greatly appreciated as I am brand new to Haskell and don't have any idea of where to start. Thanks!
Since this is clearly homework, I won't give you the answers, just a large nudge.
First off, String is just a synonym for [Char].
These functions may be helpful:
(++) :: [a] -> [a] -> [a]
This will concatenate two lists or strings.
import Data.Char
isLower :: Char -> Bool
This will test if a Char is lowercase or not
all :: (a -> Bool) -> [a] -> Bool
This will test whether or not all of a lists elements satisfy a predicate.
map :: (a -> b) -> [a] -> [b]
This applies a function to all elements of a list.
For your function isWord, you need to check that all of a strings elements are/isLower.
For your function prefixWords, every element is concatenated with a given string.

What to call a function that splits lists?

I want to write a function that splits lists into sublists according to what items satisfy a given property p. My question is what to call the function. I'll give examples in Haskell, but the same problem would come up in F# or ML.
split :: (a -> Bool) -> [a] -> [[a]] --- split lists into list of sublists
The sublists, concatenated, are the original list:
concat (split p xss) == xs
Every sublist satisfies the initial_p_only p property, which is to say (A) the sublist begins with an element satisfying p—and is therefore not empty, and (B) no other elements satisfy p:
initial_p_only :: (a -> Bool) -> [a] -> Bool
initial_p_only p [] = False
initial_p_only p (x:xs) = p x && all (not . p) xs
So to be precise about it,
all (initial_p_only p) (split p xss)
If the very first element in the original list does not satisfy p, split fails.
This function needs to be called something other than split. What should I call it??
I believe the function you're describing is breakBefore from the list-grouping package.
Data.List.Grouping: http://hackage.haskell.org/packages/archive/list-grouping/0.1.1/doc/html/Data-List-Grouping.html
ghci> breakBefore even [3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6]
[[3,1],[4,1,5,9],[2],[6,5,3,5],[8,9,7,9,3],[2,3],[8],[4],[6],[2],[6]]
I quite like some name based on the term "break" as adamse suggests. There are quite a few possible variants of the function. Here is what I'd expect (based on the naming used in F# libraries).
A function named just breakBefore would take an element before which it should break:
breakBefore :: Eq a => a -> [a] -> [[a]]
A function with the With suffix would take some kind of function that directly specifies when to break. In case of brekaing this is the function a -> Bool that you wanted:
breakBeforeWith :: (a -> Bool) -> [a] -> [[a]]
You could also imagine a function with By suffix would take a key selector and break when the key changes (which is a bit like group by, but you can have multiple groups with the same key):
breakBeforeBy :: Eq k => (a -> k) -> [a] -> [[a]]
I admit that the names are getting a bit long - and maybe the only function that is really useful is the one you wanted. However, F# libraries seem to be using this pattern quite consistently (e.g. there is sort, sortBy taking key selector and sortWith taking comparer function).
Perhaps it is possible to have these three variants for more of the list processing functions (and it's quite good idea to have some consistent naming pattern for these three types).

Resources