"Display" scanl

Given the list :: [(Foo, Bar)] , I would like to run scanl1 on Bar s, but keep the Foo tags. "

those. I need a function with type :: [(a, b)] -> ([b] -> [c]) -> [(a, c)] , so I can pass curried scanl1 as the second argument.

I can write it recursively, but it seems like there is such a way to create higher order functions.

Is this possible with standard features?

+6
source share
3 answers

Instead of writing an unsatisfactory higher-order function, you can raise your union function to sink the Foo tags so that you can still use scanl1 , which you mean.

 keeptags :: (Bar -> Bar -> Bar) -> (Foo,Bar) -> (Foo,Bar) -> (Foo,Bar) keeptags g (_,b) (a',b') = (a',gb b') 

Now you can use scanl1 ; take your original qux :: Bar -> Bar -> Bar and do

 scanQux :: [(Foo,Bar)] -> [(Foo,Bar)] scanQux = scanl1 (keeptags qux) 

keeptags is simple and scanQux crystal clear.

For example, if

 type Foo = Char type Bar = Int qux = (+) 

then you get

 *Main> scanl1 qux [1..9] [1,3,6,10,15,21,28,36,45] *Main> zip "HELLO MUM" [1..9] [('H',1),('E',2),('L',3),('L',4),('O',5),(' ',6),('M',7),('U',8),('M',9)] *Main> scanQux $ zip "HELLO MUM" [1..9] [('H',1),('E',3),('L',6),('L',10),('O',15),(' ',21),('M',28),('U',36),('M',45)] 

as you hoped.

+11
source

Do you mean unzip ?

http://zvon.org/other/haskell/Outputprelude/unzip_f.html

 x = zip ac where (a, b) = unzip my_list c = what_ever_you_want_on b 
+4
source

My friend came up with the following implementation for keeptags :

 import Control.Arrow keeptags g (_,b) = second (gb) 

I think it is still readable.

+2
source

Source: https://habr.com/ru/post/924206/


All Articles