-
haskell help
hi, i am new to learning haskell and i am struggling a bit(a lot) with it. I need help with 1 exercise which i just cant get done. Searched the web a lot but found nothing useful (or maybe i just dont understand it). What i need to do is order the words in a string in alphabetical order.Might seem easy for some, but not me.For example if we have the string "Me no understand Haskell" the output should be "haskell me no understand". Can anyone point me to something or somewhere so i can get this thing done ?
thnx in advance
-
-
Code:
orderWords = unwords . sort . words
orderWords is of type String -> String: it takes a string and returns a string. So, in order, it splits the string into its component words (words :: String -> [String]), sorts them (sort :: (Ord a) => [a] -> [a]) and then puts them back together (unwords :: [String] -> String).
NXArmada: Not helpful. :)
-
thank you Twey. Was really helpful, got that thing done now.
And ye NXArmada didn't help me much(not at all)
-
how did you do this then..i kepp getting errors :(,
im pretty new to this aswel. thanks
""
orderwords :: String -> String
orderWords = unwords . sort . words
words :: String -> [String]
sort :: (Ord a) => [a] -> [a]
unwords :: [String] -> String
""
is what i tried but keep getting Missing binding for variable "orderwords" in type signature
-
hi, all you need to write is :
orderWords :: String -> String
orderWords = unwords. sort. words
copy and paste that code and see whether it works..if it gives you some error include
import Char
import List
at the top of your code so it looks like this:
import Char
import List
orderWords :: String -> String
orderWords = unwords. sort. words
hope it helps
-
-
guvdave123: Haskell is case-sensitive, and you messed up your case.
scarface: I would highly reccomend putting spaces on both sides of (.). It makes the code much easier to read.
For the record, you do not have to import Char to get words and unwords.
-
guvdave123: All the type signatures I provided were for information only, to help scarface understand the function. All you need is:
Code:
import Data.List (sort)
orderWords = unwords . sort . words
-
thanks for all your help so far,
is there a reason it put this string " Have and Nice car" in the order of "Have Nice and car"
i notice it putting capital words first. how can i avoid this. i assume it by making a function to make them all the same case?