Folding a list of functions?

I am trying to combine an arbitrary number of mathematical functions that are on the list.

I wrote a build function that combines them:

chain :: (c -> d) -> (a -> b -> c) -> a -> b -> d
g `chain` f = \a b -> g (f a b)

So, let's do something like

let b = (*) `chain` (+) `chain` (-)

Creates a function that takes bcd and does ((a & minus; b) + c) & times; d

> b 2 3 4 5
> 15

My question is that I want to be able to link a set of them based on an arbitrary list:

[(*),(+),(-)] will result in (*) `chain` (+) `chain` (-)

[(*),(+),(-),(*)] will result in (*) `chain` (+) `chain` (-) `chain` (*)

Is it possible? I tried to use folds, but I can not get it to work, because the type of battery changes after each use of the element.

For example, the type of three functions connected together:

b :: Integer -> Integer -> Integer -> Integer -> Integer

But for four this:

b :: Integer -> Integer -> Integer -> Integer -> Integer -> Integer

If someone can point me in the right direction or find out about a solution, that would be great! Thank.

EDIT:

My ultimate goal is to do something like this:

getZipList $ pure (function composing * and +) <*> ZipList [1,2,3] <*> ZipList [4,5,6] 
    <*> ZipList [7,8,9]

will result in:

[(1+4)*7, (2+5)*8, (3+6)*9]
+4
2

, , , ( ) . "" ; , .

. .,

ifxChain :: [a->a->a] -> [a] -> a

: zip , . :

ifxChain fs (p0:ps) = foldl' (flip ($)) p0 $ zipWith flip fs ps

, , .


( , !), . printf.

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances     #-}

class ChainedFunc f t where
  chainIfxs :: [t->t->t] -> t -> f
  chainIfxs fs i = chainIfxPre i id $ reverse fs
  chainIfxPre :: t -> (t->t) -> [t->t->t] -> f

instance ChainedFunc t t where
  chainIfxPre i f [] = f i

instance (ChainedFunc f t) => ChainedFunc (t->f) t where
  chainIfxPre i fin (f:fs) x0 = chainIfxPre i (flip f x0 . fin) fs

, . , , ... ...

Main > chainIfxs [(*), (+), (-)] (2:: Int) (3:: Int) (4:: Int) (5:: Int):: Int
15

+9

?

Prelude> :m +Data.List

Prelude Data.List> let fs = [(-),(+),(*)]

Prelude Data.List> foldl' (\accum (i,f) -> accum `f` i) 2 (zip [3..5] fs)
15
+2

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


All Articles