[Haskell-beginners] function multiple args

Stuart Dootson stuart.dootson at gmail.com
Mon Dec 18 16:00:39 UTC 2017


Something like the code below - populate a list with partially applied
functions, then use foldl to apply the functions in turn to the input list?

v :: Int -> [a] -> [a]
v = drop

w :: Int -> Int -> [a] -> [a]
w keep reject = (take keep).(drop reject)

-- Apply the list of functions (in turn) to the list, yielding a list
apply_ops :: [ [a] -> [a] ] -> [a] -> [a]
apply_ops fns l = foldl (flip ($)) l fns

-- Partially apply functions with non-list arguments to get list of
functions of type [a] -> [a]
ops :: [ [a] -> [a] ]
ops = [v 3, w 2 1]

-- result = (w 2 1) ((v 3)[1,2,3,4,5,6,7,8])
--        = w [4,5,6,7,8]
--        = [5,6]
result :: [Int]
result = apply_ops ops [1,2,3,4,5,6,7,8]

main =
  do
    print result

On 17 December 2017 at 09:32, mike h <mike_k_houghton at yahoo.co.uk> wrote:

> Hi,
>
> I have a number of functions like these:
>
> v :: Int -> [a] -> [a]
> w :: Int -> Int -> [a] -> [a]
> x :: a -> a -> [a] -> [a]
>
> y :: a -> b ->…  <other args>... ->  [a] -> [a]
> z…
> etc.
>
> where there are any number of args of different types but the last two are
> common to all the functions i.e. they all have  [a] -> [a]
>
> What I’m trying to do is build a collection (ordered) of such functions
> and then apply each in turn to a [a] to get the final [a]. What would be
> the best approach to this?
>
> Thanks
>
> Mike
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20171218/b1581f77/attachment.html>


More information about the Beginners mailing list