View Full Version : haskell help
scarface
10-20-2008, 09:14 PM
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
NXArmada
10-21-2008, 02:26 PM
http://www.haskell.org/
orderWords = unwords . sort . wordsorderWords 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. :)
scarface
10-22-2008, 11:24 AM
thank you Twey. Was really helpful, got that thing done now.
And ye NXArmada didn't help me much(not at all)
guvdave123
10-22-2008, 11:25 PM
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
scarface
10-23-2008, 08:00 PM
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
10-24-2008, 12:00 AM
thanks mate
Trinithis
10-24-2008, 12:14 AM
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:
import Data.List (sort)
orderWords = unwords . sort . words
guvdave123
10-24-2008, 01:47 PM
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?
You'll need Data.Char.toLower :: Char -> Char, Prelude.map :: (a -> b) -> [a] -> [b], Data.List.sortBy :: (Ord a) => (a -> a -> Ordering) -> [a] -> [a], and Data.Ord.comparing :: (Ord a) => (b -> a) -> b -> b -> Ordering:
import Data.List (sortBy)
import Data.Char (toLower)
import Data.Ord (comparing)
orderWords = unwords . sortBy (comparing $ map toLower) . words
guvdave123
10-26-2008, 11:43 AM
You'll need Data.Char.toLower :: Char -> Char, Prelude.map :: (a -> b) -> [a] -> [b], Data.List.sortBy :: (Ord a) => (a -> a -> Ordering) -> [a] -> [a], and Data.Ord.comparing :: (Ord a) => (b -> a) -> b -> b -> Ordering:
import Data.List (sortBy)
import Data.Char (toLower)
import Data.Ord (comparing)
orderWords = unwords . sortBy (comparing $ map toLower) . words
twey is there anyway i can email you, just wanted to ask you summit about my code, but dont want to post it on here cause its part of my project and might get plagerised. tried your email but email keeps bouncing back and cant pm eitha. or any one else who can spare 5 mins! thanks
After you've replied to this message, you should be able to PM me (requires five posts first, to keep the spammers out).
I must say, I thought my email account for this forum was still active. If not, no matter, PM me. You can also reach my main email account by removing the '-dd' from the email address I use here.
guvdave123
10-26-2008, 01:04 PM
thanks mate
rjhelpdesk
03-28-2009, 04:56 PM
ok ------------------------------------
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.