87133

Sum of the first elem of a 3-tuples list

I'm new around programming stuff :/

I need to make a function that retrieve the sum of the first element from a 3-tuple list.

I have something like:

tuples = [(11,"11","11"),(22,"22","22"),(33,"33","33"),(44,"44","44"),(55,"55","55"),(66,"66","66")]

And I need the sum of the first element of each 3-tuple from the list. = 11+22+33+44+55

Pattern matching maybe? map?

Answer1:

Use sum with a list comprehension:

sum [x | (x, _, _) <- tuples]

Answer2:

If you want something pointfree, you could try:

> let f = sum . map (\(x, _, _) -> x) > f [(11,"11","11"),(22,"22","22"),(33,"33","33")] 66

Note: this has point x, which we can not avoid because of the fst3 :: (a,b,c) -> a built-in lack

Recommend

  • What's the difference between [String] vs. [(String)]?
  • Einsum optimize fails for basic operation
  • Invoking a controller's action by button in View without redirecting to any view
  • Grouping by blank nodes
  • OpenCV Python: Draw minAreaRect ( RotatedRect not implemented)
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • Problems with matplotlib.pyplot.xticks()
  • Regex for Specific Tag
  • can variables be set randomly when declaring them again?
  • Is it possible to define rest argument in OCaml?
  • C# - Most efficient way to iterate through multiple arrays/list
  • Possible to “watch” both HAML and SASS at the same time?
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • How to specify input and output paths from cmd.exe for a PowerShell script?
  • ThreadStatic in asynchronous ASP.NET Web API
  • Approximate Order-Preserving Huffman Code
  • Python pickle not one-to-one: different pickles give same object
  • Why use database factory in asp.net mvc?
  • Is it possible to open regedit and navigate to straight to a specific key using process.start?
  • Not able to aggregate on nested fields in elasticsearch
  • C: Incompatible pointer type initializing
  • Grails calculated field in SQL
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Swift: Switch statement fallthrough behavior
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Is possible to count alias result on mysql
  • WinForms: two way TextBox problem
  • Weird JavaScript statement, what does it mean?
  • R: gsub and capture
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • Django query for large number of relationships
  • Binding checkboxes to object values in AngularJs
  • Unable to use reactive element in my shiny app
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?
  • How do I use LINQ to get all the Items that have a particular SubItem?