Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: function multiple args (Ovidiu Deac)
2. Re: function multiple args (Stuart Dootson)
3. Re: function multiple args (mike h)
----------------------------------------------------------------------
Message: 1
Date: Mon, 18 Dec 2017 17:34:32 +0200
From: Ovidiu Deac <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] function multiple args
Message-ID:
<cakvse7txgagttjdbe+h0aqroxmqbae6ifd91k9ftvpy30c0...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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 <[email protected]>
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
> [email protected]
> 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/cd1e0387/attachment-0001.html>
------------------------------
Message: 2
Date: Mon, 18 Dec 2017 16:00:39 +0000
From: Stuart Dootson <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] function multiple args
Message-ID:
<CA+L+Jksk4OUVfC1YHDa6Q=3XRL8Be0zmXDtWKj+Z_ivObFH=f...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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 <[email protected]> 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
> [email protected]
> 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-0001.html>
------------------------------
Message: 3
Date: Tue, 19 Dec 2017 10:04:42 +0000
From: mike h <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] function multiple args
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Thanks Stuart, Ovidiu. Yes, partially applied functions is the way.
Mike
> On 18 Dec 2017, at 16:00, Stuart Dootson <[email protected]> wrote:
>
> 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 <[email protected]
> <mailto:[email protected]>> 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
> [email protected] <mailto:[email protected]>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20171219/921cac80/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 114, Issue 26
******************************************