Go Back   CodeCall Programming Forum > Software Development > General Programming
Register Blogs Search Today's Posts Mark Forums Read

General Programming Non language specific and anything not covered in other topics. Talk about Programming Theory here.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-22-2009, 12:31 PM
Newbie
 
Join Date: Oct 2009
Posts: 2
AlexY is an unknown quantity at this point
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 10-22-2009, 12:55 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-22-2009, 01:17 PM
Newbie
 
Join Date: Oct 2009
Posts: 2
AlexY is an unknown quantity at this point
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]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-22-2009, 05:10 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
The spirit of MVVM (ViewModel), it’s not a code counting exercise. Kernel News 1 08-03-2009 02:19 PM
Functional Programming Unit Testing - Part 4 Kernel News 0 12-21-2008 07:20 PM
Basic Calculator AfTriX VB Tutorials 3 02-29-2008 09:53 AM


All times are GMT -5. The time now is 08:21 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0