{"id":16672,"date":"2022-10-21T20:54:43","date_gmt":"2022-10-21T15:24:43","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/"},"modified":"2022-10-21T20:54:43","modified_gmt":"2022-10-21T15:24:43","slug":"solved-f1-results-with-haskell","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/","title":{"rendered":"[Solved] F1 Results with Haskell"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-22580945\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"22580945\" data-parentid=\"22580319\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You have a constant for what the scoring is<\/p>\n<pre><code>scoring :: [Int]\nscoring = [25, 18, 15, 12, 10, 8, 6, 4, 2, 1]\n<\/code><\/pre>\n<p>Then you need a way for pairing up a driver with the score they got.  Whenever you&#8217;re pairing two things in Haskell, the canonical choice is to use a tuple.  The easiest way to construct a list of tuples from two lists is the <code>zip<\/code> function:<\/p>\n<pre><code>zip :: [a] -&gt; [b] -&gt; [(a, b)]\n<\/code><\/pre>\n<p>And in this case can be used to assign scores for a race:<\/p>\n<pre><code>assignScores :: [String] -&gt; [(String, Int)]\nassignScores race = zip race scoring\n<\/code><\/pre>\n<p>Now, we need a way to total up the scores for a driver for each race.  We want to be able to turn something like<\/p>\n<pre><code>[(\"Bob\", 12), (\"Joe\", 10), (\"Bob\", 18), (\"Joe\", 25)]\n<\/code><\/pre>\n<p>into<\/p>\n<pre><code>[(\"Bob\", 30), (\"Joe\", 35)]\n<\/code><\/pre>\n<p>The easiest way would be to make a single list of all the scores for all the races<\/p>\n<pre><code>assignAllScores :: [[String]] -&gt; [(String, Int)]\nassignAllScores races = concatMap assignScores races\n<\/code><\/pre>\n<p>Then we can use <code>sortBy<\/code> from <code>Data.List<\/code> to get all the same names next to each other<\/p>\n<pre><code>sortBy :: (a -&gt; a -&gt; Ordering) -&gt; [a] -&gt; [a]\ncompare :: Ord a =&gt; a -&gt; a -&gt; Ordering\n\nsortByDriver :: [(String, Int)] -&gt; [(String, Int)]\nsortByDriver races = sortBy (\\(n1, s1) (n2, s2) -&gt; compare n1 n2) races\n<\/code><\/pre>\n<p>Then we can use <code>groupBy<\/code> (also from <code>Data.List<\/code>) to group them all by name<\/p>\n<pre><code>groupBy :: (a -&gt; a -&gt; Bool) -&gt; [a] -&gt; [[a]]\n\ngroupByDriver :: [(String, Int)] -&gt; [[(String, Int)]]\ngroupByDriver races = groupBy (\\(n1, s1) (n2, s2) -&gt; n1 == n2) races\n<\/code><\/pre>\n<p>But this gives us a list like<\/p>\n<pre><code>[[(\"Bob\", 12), (\"Bob\", 18)], [(\"Joe\", 10), (\"Joe\", 25)]]\n<\/code><\/pre>\n<p>We now need a way to convert this into the form<\/p>\n<pre><code>[(\"Bob\", [12, 18]), (\"Joe\", [10, 25])]\n<\/code><\/pre>\n<p>where all the scores are aggregated back into a list, and we don&#8217;t repeat the names at all.  This is left as an exercise.<\/p>\n<pre><code>aggregateScores :: [[(String, Int)]] -&gt; [(String, [Int])]\n<\/code><\/pre>\n<p>Then we can finally calculate the sum of these scores<\/p>\n<pre><code>sumScores :: [(String, [Int])] -&gt; [(String, Int)]\nsumScores races = map (\\(name, scores) -&gt; (name, sum scores)) races\n<\/code><\/pre>\n<p>Then finally we can sort by the scores to get everyone in order<\/p>\n<pre><code>sortByScore :: [(String, Int)] -&gt; [(String, Int)]\nsortByScore races = sortBy (\\(n1, s1) (n2, s2) -&gt; compare s2 s1) races\n<\/code><\/pre>\n<p>Notice that I have <code>compare s2 s1<\/code> instead of <code>compare s1 s2<\/code>, this means it will be sorted in descending order instead of ascending order.<\/p>\n<p>The last step is to strip out the scores, now we have our list of drivers in order from winner to loser<\/p>\n<pre><code>removeScores :: [(String, Int)] -&gt; [String]\nremoveScores races = map fst races\n<\/code><\/pre>\n<p>So to combine everything together into one function<\/p>\n<pre><code>f1Results :: [[String]] -&gt; [String]\nf1Results races =\n    removeScores $\n    sortByScore  $\n    sumScores    $\n    aggregateScores $\n    groupByDriver $\n    sortByDriver $\n    assignAllScores races\n<\/code><\/pre>\n<hr>\n<p>There are several tricks that can make this code shorter, namely <code>Data.Function.on<\/code>, <code>Data.Ord.comparing<\/code>, and a fun operator from <code>Control.Arrow<\/code>.  Don&#8217;t turn this in as homework, I just wanted to show an alternative that uses less code.<\/p>\n<pre><code>import Data.List\nimport Data.Function (on)\nimport Data.Ord (comparing)\n\nscoring :: [Int]\nscoring = [25, 18, 15, 12, 10, 8, 6, 4, 2, 1]\n\nf1Results :: [[String]] -&gt; [String]\nf1Results =\n    map fst . sortBy (on (flip compare) snd) .\n    map ((head *** sum) . unzip) .\n    groupBy (on (==) fst) . sortBy (comparing fst) .\n    concatMap (`zip` scoring)\n<\/code><\/pre>\n<p>Or using <code>Data.Map<\/code>:<\/p>\n<pre><code>import Data.Map (assocs, fromListWith)\nimport Data.List\nimport Data.Ord (comparing)\n\nscoring :: [Int]\nscoring = [25, 18, 15, 12, 10, 8, 6, 4, 2, 1]\n\nf1Results :: [[String]] -&gt; [String]\nf1Results =\n    reverse . map fst . sortBy (comparing snd) .\n    assocs . fromListWith (+) .\n    concatMap (`zip` scoring)\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">8<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved F1 Results with Haskell <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You have a constant for what the scoring is scoring :: [Int] scoring = [25, 18, 15, 12, 10, 8, 6, 4, 2, 1] Then you need a way for pairing up a driver with the score they got. Whenever you&#8217;re pairing two things in Haskell, the canonical choice is to use a tuple. &#8230; <a title=\"[Solved] F1 Results with Haskell\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/\" aria-label=\"More on [Solved] F1 Results with Haskell\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[1150,540],"class_list":["post-16672","post","type-post","status-publish","format-standard","hentry","category-solved","tag-haskell","tag-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] F1 Results with Haskell - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] F1 Results with Haskell - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You have a constant for what the scoring is scoring :: [Int] scoring = [25, 18, 15, 12, 10, 8, 6, 4, 2, 1] Then you need a way for pairing up a driver with the score they got. Whenever you&#8217;re pairing two things in Haskell, the canonical choice is to use a tuple. ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-21T15:24:43+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-f1-results-with-haskell\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-f1-results-with-haskell\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] F1 Results with Haskell\",\"datePublished\":\"2022-10-21T15:24:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-f1-results-with-haskell\\\/\"},\"wordCount\":302,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"haskell\",\"list\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-f1-results-with-haskell\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-f1-results-with-haskell\\\/\",\"name\":\"[Solved] F1 Results with Haskell - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-10-21T15:24:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-f1-results-with-haskell\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-f1-results-with-haskell\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-f1-results-with-haskell\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] F1 Results with Haskell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] F1 Results with Haskell - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] F1 Results with Haskell - JassWeb","og_description":"[ad_1] You have a constant for what the scoring is scoring :: [Int] scoring = [25, 18, 15, 12, 10, 8, 6, 4, 2, 1] Then you need a way for pairing up a driver with the score they got. Whenever you&#8217;re pairing two things in Haskell, the canonical choice is to use a tuple. ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/","og_site_name":"JassWeb","article_published_time":"2022-10-21T15:24:43+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] F1 Results with Haskell","datePublished":"2022-10-21T15:24:43+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/"},"wordCount":302,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["haskell","list"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/","url":"https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/","name":"[Solved] F1 Results with Haskell - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-21T15:24:43+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-f1-results-with-haskell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] F1 Results with Haskell"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=16672"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16672\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=16672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=16672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=16672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}