Help coderanch get a
new server
by contributing to the fundraiser
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Confuse in haskell output for ">" function

Ranch Hand
Posts: 1143
5
• Number of slices to send:
Optional 'thank-you' note:
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?

Bartender
Posts: 2407
36
• Number of slices to send:
Optional 'thank-you' note:
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.

Punit Jain
Ranch Hand
Posts: 1143
5
• Number of slices to send:
Optional 'thank-you' note:

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
Posts: 2407
36
• Number of slices to send:
Optional 'thank-you' note:
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.

Greenhorn
Posts: 3
• 1
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 1143
5
• Number of slices to send:
Optional 'thank-you' note:
ohk thanks.

 There is no "i" in denial. Tiny ad: We need your help - Coderanch server fundraiser https://coderanch.com/t/782867/Coderanch-server-fundraiser