+ Reply to Thread
Results 1 to 4 of 4

Thread: Could somebody help me with understanding this basic Haskell code? Thanks!

  1. #1
    Newbie AlexY is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    2

    Could somebody help me with understanding this basic Haskell code? Thanks!

    The question is:
    1. (a) The function ord :: Char -> Int converts a character to its corresponding
    ASCII code. For example,
    ord ’A’ = 65 ord ’B’ = 66 ... ord ’Z’ = 90
    ord ’a’ = 97 ord ’b’ = 98 ... ord ’z’ = 122
    Using ord, write a function f :: Char -> Int that converts a letter, lower case
    or upper case, to its ordinal position in the alphabet. For example,
    f ’A’ = 0 f ’B’ = 1 ... f ’Z’ = 25
    f ’a’ = 0 f ’b’ = 1 ... f ’z’ = 25
    For any character that is not a letter, f should return an error.

    The solution is below and I stop understanding it right after ''a' <= x && x <= 'z' =' on the first line. Could somebody please walk me through exactly what is happening here? Thank you in advance.
    -- 1a.

    f :: Char -> Int
    f x | 'a' <= x && x <= 'z' = ord x - ord 'a'
    | 'A' <= x && x <= 'Z' = ord x - ord 'A'

    x1a = f 'A' == 0 && f 'B' == 1 && f 'Z' == 25 &&
    f 'a' == 0 && f 'b' == 1 && f 'z' == 25

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,673
    Blog Entries
    57

    Re: Could somebody help me with understanding this basic Haskell code? Thanks!

    What it's doing is looking at two cases.
    Case 1: x between 'a' and 'z', returns ord x - ord 'a'
    Case 2: x between 'A' and 'Z', returns ord x - ord 'A'
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Newbie AlexY is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    2

    Re: Could somebody help me with understanding this basic Haskell code? Thanks!

    Thank you. I understand it now I was just a little confused. Thanks for the help. One last question

    c :: [Int] -> [Int] -> [Int]
    c xs ys | length xs == length ys = [ x-y | (x,y) <- zip xs ys ]

    I understand that C takes two lists of xs and ys and then there is a guard(?) after which it makes sure that the length of the two lists are equal. Then it becomes a list comprehension?
    I am getting a little confused with the syntax, because I thought that length xs == length ys would be the condition or 'check' to makes sure that the two lists of equal, that it should go inside the square brackets. So something like this: (additionally why is x not drawn from xs and y not drawn from ys?)
    c xs ys = [ x-y | x<-xs, y<-ys, length xs == length ys]

  4. #4
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,673
    Blog Entries
    57

    Re: Could somebody help me with understanding this basic Haskell code? Thanks!

    The best thing you can do is compare your two results on sample data. In general, zip pairs the elements of xs and ys in order, whereas grabbing the individual elements will generate all possible pairings.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Replies: 1
    Last Post: 08-03-2009, 01:19 PM
  2. Replies: 0
    Last Post: 12-21-2008, 06:20 PM
  3. Basic Calculator
    By AfTriX in forum VB Tutorials
    Replies: 3
    Last Post: 02-29-2008, 08:53 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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