Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

haskell sorting

How can it be done in most simply way to write (or maybe there is something embedded in haskell) function which takse as arguments list of tuples (String, Int) and Int x and return top x tuples as list according to x value.

I wonder if its possible to write a function which also takes 3 argument which is the name of (or index) of filed in tuple according to which sorting has to be done.

What are best solutions to make it quite generic?

like image 487
gruber Avatar asked May 07 '10 11:05

gruber


People also ask

Is Haskell sort stable?

The sort is stable. If stability is not required, unstableSort can be slightly faster. A Haskell sorting toolkit A library of general-purpose sorting utilities.

How do you use sortBy in Haskell?

The sortBy function is the non-overloaded version of sort. >>> sortBy (\(a,_) (b,_) -> compare a b) [(2, "world"), (4, "!"), (1, "Hello")] [(1,"Hello"),(2,"world"),(4,"!")] sortBy sorts the specified Seq according to the specified comparator.

What does group do in Haskell?

The group function takes a stream and returns a list of streams such that flattening the resulting list is equal to the argument. Moreover, each stream in the resulting list contains only equal elements.

How do I remove an item from a list in Haskell?

You first compare the head of the list ( y ) to the item you want to remove and correctly return the item or an empty list using areTheySame . Then you want to recursively continue using removeItem on the rest of the list ( ys ). The resulting list needs to be concatenated using ++ .


1 Answers

take x $ sortBy (compare `on` fst) [("asd", 1), ...]

take x takes the first x items from the sorted list. sortBy sorts the list given as second argument using the sorting function given as the first argument. (compare `on` fst) compares the first values of each tuple. Note that this example compares the first value of each tuple for sorting. To sort by the second value, replace fst with snd.

You see that the sortBy function is very generic, as it lets you define the function used to compare the values. The function takes two arguments and should return one of LT, EQ or GT. Note that the function compare requires both arguments to derive from Ord. The helper function on can be found in the module Data.Function. The function sortBy is in the module Data.List.

EDIT: Here is a complete working example that sorts a list of tuples by comparing their first values and prints the first 2 tuples of the resulting list. Note that I replaced the on from the example above with a equivalent function that shows what on does internally.

import Data.Function
import Data.List

main = print $ mySort [("foo", 1), ("bar", 2), ("baz", 3), ("quux", 4)] 2

mySort list x = take x $ sortBy (\ x y -> compare (fst x) (fst y)) list

EDIT: As Tom Lokhorst pointed out in his comment, the function comparing from the module Data.Ord is a more readable replacement/shortcut for on compare, so the above could also be written as sortBy (comparing fst).

like image 107
jkramer Avatar answered Oct 02 '22 21:10

jkramer