wood burning stoves*
The moose likes Functional Programming and the fly likes Confuse in haskell output for " function" title="" /> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » Functional Programming
Bookmark "Confuse in haskell output for ">" function" Watch "Confuse in haskell output for ">" function" New topic
Author

Confuse in haskell output for ">" function

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 998
    
    2
well i jus started learning FP, referring http://learnyouahaskell.com.
can anyone tell how the output of this function would be true in haskell.


however haskell calculates it first element, second element and so on..

so in first step : 3>2 : True
in Second Step : 2>10 : False

how the final output would be True?
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1671
    
  14

The comparison checks each pair of values in turn and it can stop when an element in one list is not equal to its counterpart in the other list, because now it can answer the question "is X > Y?". It's like an alphabetical comparison e.g. "BBC" > "ACD".

I don't know how Haskell implements this check, but for your lists [3,2,1] and [2,10,100] the comparison might be something like:

Is 3 = 2? No, so check if 3 > 2: ==> true and we're done.

If your lists were [3,2,1] and [3,10,100], then the comparison might be:

Is 3 = 3? Yes, so check next element instead:

Is 2 = 10? No, so check if 2 > 10: ==> false

The real implementation may do the = and > checks in a different order and it will also handle empty lists, but the basic principle is clear.


No more Blub for me, thank you, Vicar.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 998
    
    2
chris webster wrote:The comparison checks each pair of values in turn and it can stop when an element in one list is not equal to its counterpart in the other list, because now it can answer the question "is X > Y?". It's like an alphabetical comparison e.g. "BBC" > "ACD".

I don't know how Haskell implements this check, but for your lists [3,2,1] and [2,10,100] the comparison might be something like:

Is 3 = 2? No, so check if 3 > 2: ==> true and we're done.

If your lists were [3,2,1] and [3,10,100], then the comparison might be:

Is 3 = 3? Yes, so check next element instead:

Is 2 = 10? No, so check if 2 > 10: ==> false

The real implementation may do the = and > checks in a different order and it will also handle empty lists, but the basic principle is clear.



looks like in each (ie. <, >), it first checks for =.
but what is the use for that, however i want to check for >.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1671
    
  14

It doesn't really matter, because you still need to do the same checks in most cases. If element X is not > element Y, then you still need to know if X = Y, in which case you need to check the next pair of elements, or if X < Y, in which case you know that list X is not > list Y.

So let's try it your way with lists [3, 2, 1] and [2, 10, 100]:

Is [3, 2, 1] > [2, 10, 100]?

Is 3 > 2? Yes, so return true and stop. List [3, 2, 1] is > list [2, 10, 100].

But if you try it with lists [3, 2, 1] and [3, 10, 100]:

Is 3 > 3? No, so check: is 3 = 3?
Yes, 3 = 3, so now we have to check the next element in the lists to see which is greater.
Is 2 > 10? No, so check: is 2 = 10? No, so 2 must be < 10, which means we can return false and stop, because list [3, 2, 1] is not > list [3, 10, 100].

Haskell compares each pair of elements in the lists in turn, and stops as soon as it can answer the question you asked i.e. is list X > list Y? Or when it reaches the end of one of the lists.

It's really not that hard. Think about how you compare two strings (lists of characters) alphabetically - it's the same principle. In fact, Learn You A Haskell tells you this:

Learn You A Haskell wrote:When using <, <=, > and >= to compare lists, they are compared in lexicographical order. First the heads are compared. If they are equal then the second elements are compared, etc.
Jason Reaves
Greenhorn

Joined: Feb 15, 2013
Posts: 3

As was said it's due to how the list are compared.
If you want to run the > operator against each pair from the lists you can use zipWith or in the case of [Int] you can sum them and then compare them.

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 998
    
    2
ohk thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confuse in haskell output for ">" function