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 [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
I'm not sure I understand your intention but I will try to answer though. Since your functions have different types you won't be able to put them in the same collection. Considering these two: v :: Int -> [a] -> [a] w :: Int -> Int -> [a] -> [a] You have the following options: 1. You can have a sum type to wrap them data MyFunctionType a = Vtype (Int -> [a] -> [a]) | Wtype (Int -> Int -> [a] -> [a]) then you will have your collection myFunctions = [VType v, WType w,...] then, when you apply them you will have to match and apply the function properly. 2. Another way to put them in the same collection could be to apply each of the functions partially until you are left with functions having the type ([a] -> [a]) myFunctions = [v 10, w 1 2] Does that answer your question? On Sun, Dec 17, 2017 at 11:32 AM, mike h <[hidden email]> wrote: Hi, _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
In reply to this post by Mike Houghton
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 <[hidden email]> wrote: Hi, _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Thanks Stuart, Ovidiu. Yes, partially applied functions is the way.
Mike
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |