Skip to main content
added 366 characters in body
Source Link
chepner
  • 537.7k
  • 77
  • 594
  • 746

Consider the helper function

updateSet :: Set -> Int -> Set
updateSet (Set s xs) y = Set s (y:xs)

This simply adds a single value to a given set:

>>> updateSet (Set "One" [1]) 0
Set "One" [0,1]

Then addToSet is just a wrapper around zipWith:

addToSet :: [Set] -> [Int] -> [Set]
addToSet = zipWith updateSet

updateSet is called on the first set and the first integer, then the second set and the second integer, and so on, with the results combined in order in a single list.


Another way to think of this is mapping a function of type (Set, Int) -> Int over the result of zipping the two lists together:

updateSet' :: (Set, Int) -> Set
updateSet' (Set s xs, y) -> Set s (y:xs)

addToSet :: [Set] -> [Int] -> [Set]
addToSet ss is = map updateSet' (zip ss is)

Note that updateSet' = uncurry updateSet.

Consider the helper function

updateSet :: Set -> Int -> Set
updateSet (Set s xs) y = Set s (y:xs)

This simply adds a single value to a given set:

>>> updateSet (Set "One" [1]) 0
Set "One" [0,1]

Then addToSet is just a wrapper around zipWith:

addToSet :: [Set] -> [Int] -> [Set]
addToSet = zipWith updateSet

updateSet is called on the first set and the first integer, then the second set and the second integer, and so on, with the results combined in order in a single list.

Consider the helper function

updateSet :: Set -> Int -> Set
updateSet (Set s xs) y = Set s (y:xs)

This simply adds a single value to a given set:

>>> updateSet (Set "One" [1]) 0
Set "One" [0,1]

Then addToSet is just a wrapper around zipWith:

addToSet :: [Set] -> [Int] -> [Set]
addToSet = zipWith updateSet

updateSet is called on the first set and the first integer, then the second set and the second integer, and so on, with the results combined in order in a single list.


Another way to think of this is mapping a function of type (Set, Int) -> Int over the result of zipping the two lists together:

updateSet' :: (Set, Int) -> Set
updateSet' (Set s xs, y) -> Set s (y:xs)

addToSet :: [Set] -> [Int] -> [Set]
addToSet ss is = map updateSet' (zip ss is)

Note that updateSet' = uncurry updateSet.

Source Link
chepner
  • 537.7k
  • 77
  • 594
  • 746

Consider the helper function

updateSet :: Set -> Int -> Set
updateSet (Set s xs) y = Set s (y:xs)

This simply adds a single value to a given set:

>>> updateSet (Set "One" [1]) 0
Set "One" [0,1]

Then addToSet is just a wrapper around zipWith:

addToSet :: [Set] -> [Int] -> [Set]
addToSet = zipWith updateSet

updateSet is called on the first set and the first integer, then the second set and the second integer, and so on, with the results combined in order in a single list.