Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: haskell help

  1. #1
    Join Date
    Oct 2008
    Posts
    20
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default 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

  2. #2
    Join Date
    Apr 2006
    Posts
    190
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default

    Ryan
    Sevierville, TN

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. The Following User Says Thank You to Twey For This Useful Post:

    guvdave123 (10-23-2008)

  5. #4
    Join Date
    Oct 2008
    Posts
    20
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default

    thank you Twey. Was really helpful, got that thing done now.

    And ye NXArmada didn't help me much(not at all)

  6. The Following User Says Thank You to scarface For This Useful Post:

    guvdave123 (10-23-2008)

  7. #5
    Join Date
    Oct 2008
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    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
    Last edited by guvdave123; 10-22-2008 at 11:29 PM. Reason: more precise

  8. #6
    Join Date
    Oct 2008
    Posts
    20
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default

    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

  9. The Following User Says Thank You to scarface For This Useful Post:

    guvdave123 (10-23-2008)

  10. #7
    Join Date
    Oct 2008
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    thanks mate

  11. #8
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    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.
    Trinithis

  12. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  13. #10
    Join Date
    Oct 2008
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    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?

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •