Hello I am kinda newbie in Haskell you can help help me with some programming
I am trying to make function like for example func :: (a -> Bool) -> (a -> Bool)
this function make calculation and return bool . I want to be able to make bool True when It is False and False when it is True while returning the a. Thank you -- Mujtaba Ali Alboori _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
I'm not certain exactly what you mean, but I *think* you mean:
func :: (a -> Bool) -> (a -> Bool) func = (not .) Bob On 18 Apr 2010, at 16:35, Mujtaba Boori wrote: > Hello I am kinda newbie in Haskell you can help help me with some programming > > I am trying to make function like for example > > func :: (a -> Bool) -> (a -> Bool) > > this function make calculation and return bool . I want to be able to make bool True when It is False and False when it is True while returning the a. > > Thank you > > -- > Mujtaba Ali Alboori > _______________________________________________ > Haskell-Cafe mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/haskell-cafe _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
In reply to this post by Mujtaba Boori
Hello Mujtaba,
I wonder is this homework? If that's the case there is nothing wrong with asking homework related questions but they should probably be marked as such. I think the most straight forward solution will use function composition (.) and the (not) function -keith On Sun, Apr 18, 2010 at 11:35 AM, Mujtaba Boori <[hidden email]> wrote: > Hello I am kinda newbie in Haskell you can help help me with some > programming > I am trying to make function like for example > func :: (a -> Bool) -> (a -> Bool) > this function make calculation and return bool . I want to be able to make > bool True when It is False and False when it is True while returning the a. > Thank you > -- > Mujtaba Ali Alboori > > _______________________________________________ > Haskell-Cafe mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/haskell-cafe > > -- keithsheppard.name _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
In reply to this post by Mujtaba Boori
Thanks for helping me but I have another problem (sorry for asking) . I tried to figure it out .
how about if I want to compare two kind with (&&) (||) for
func :: (a -> Bool) -> (a -> Bool) -> (a -> Bool) I tried some thing like func = ((||) .)
This is the annoying part about Haskell . I can not understand composition .
On Sun, Apr 18, 2010 at 4:35 PM, Mujtaba Boori <[hidden email]> wrote:
-- Mujtaba Ali Alboori _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
Using composition can be tricky with more than one arg. I just want to
be sure you're not really looking for something like: > func :: (a -> Bool) -> (b -> Bool) -> (a -> b -> Bool) keeping with your given type I think you're looking for something like: > func f1 f2 x = (f1 x) || (f2 x) I'm sure there is a nice way to do this with function composition but I still find composition less intuitive than explicit args in cases like this. On Sun, Apr 18, 2010 at 1:00 PM, Mujtaba Boori <[hidden email]> wrote: > Thanks for helping me but I have another problem (sorry for asking) . I > tried to figure it out . > how about if I want to compare two kind with (&&) (||) for > func :: (a -> Bool) -> (a -> Bool) -> (a -> Bool) > > I tried some thing like > func = ((||) .) > This is the annoying part about Haskell . I can not understand composition . > > On Sun, Apr 18, 2010 at 4:35 PM, Mujtaba Boori <[hidden email]> > wrote: >> >> Hello I am kinda newbie in Haskell you can help help me with some >> programming >> I am trying to make function like for example >> func :: (a -> Bool) -> (a -> Bool) >> this function make calculation and return bool . I want to be able to >> make bool True when It is False and False when it is True while returning >> the a. >> Thank you >> -- >> Mujtaba Ali Alboori > > > > -- > Mujtaba Ali Alboori > > _______________________________________________ > Haskell-Cafe mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/haskell-cafe > > -- keithsheppard.name _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
In reply to this post by Mujtaba Boori
One of the ways of understanding composition (and many other functions in Haskell) is by trying to understand its type. Here it is shown by looking at the type in the interpreter GHCi. *Main> :t (.) (.) :: (b -> c) -> (a -> b) -> a -> c It's a function that takes three arguments, the first two of which are functions, and the third is something else. Since (.) is a polymorphic function, we can tell everything about it (if we ignore certain other features of Haskell which are not immediately relevant). To give names to things, let's say composition is declared as follows: (.) f g x = ... We know that the type of x must be the same as the type of the argument to the function f. The result type of f is the same and the input type of g. The result type of g is the result type of (.). From these observations, we know that (.) applies g to x and f to the result of that. We can even write that definition down. (.) f g x = f (g x) But, in the case of (.), we don't need to look at the definition to understand how it works. We can get all the information from the type. The next step in understanding (.) is seeing how it's used. If you want to compose two functions, you can use their types to figure out what the result is. You mentioned (.) (||), so let's look at that first. Then, we can use GHCi to verify our understanding. The type of (||) is Bool -> Bool -> Bool or, equivalently, Bool -> (Bool -> Bool) (since -> is right associative). If we apply (.) to (||), then we can substitute parts of the type of (||) into the type of (.) to figure out the type of (.) (||) (which is equivalent to ((||) .). First, note the types of the two components: (.) :: (b -> c) -> (a -> b) -> a -> c (||) :: Bool -> (Bool -> Bool) Then, since (||) is plugged into the first argument of (.), we bind the right-hand sides below (from the type of (||)) to the left-hand sides (from (.)): b = Bool c = Bool -> Bool The resulting type is: (.) (||) :: (a -> Bool) -> a -> Bool -> Bool and GHCi agrees. If you are looking for something of this type, then you've found it. Otherwise, you need to rethink your definition. Regards, Sean _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
In reply to this post by Keith Sheppard
To do this, you need not just fmap (composition), but also ap, or the combined form, liftA2:
func = liftA2 (||) Bob On 18 Apr 2010, at 18:21, Keith Sheppard wrote: > Using composition can be tricky with more than one arg. I just want to > be sure you're not really looking for something like: > >> func :: (a -> Bool) -> (b -> Bool) -> (a -> b -> Bool) > > keeping with your given type I think you're looking for something like: > >> func f1 f2 x = (f1 x) || (f2 x) > > I'm sure there is a nice way to do this with function composition but > I still find composition less intuitive than explicit args in cases > like this. > > On Sun, Apr 18, 2010 at 1:00 PM, Mujtaba Boori <[hidden email]> wrote: >> Thanks for helping me but I have another problem (sorry for asking) . I >> tried to figure it out . >> how about if I want to compare two kind with (&&) (||) for >> func :: (a -> Bool) -> (a -> Bool) -> (a -> Bool) >> >> I tried some thing like >> func = ((||) .) >> This is the annoying part about Haskell . I can not understand composition . >> >> On Sun, Apr 18, 2010 at 4:35 PM, Mujtaba Boori <[hidden email]> >> wrote: >>> >>> Hello I am kinda newbie in Haskell you can help help me with some >>> programming >>> I am trying to make function like for example >>> func :: (a -> Bool) -> (a -> Bool) >>> this function make calculation and return bool . I want to be able to >>> make bool True when It is False and False when it is True while returning >>> the a. >>> Thank you >>> -- >>> Mujtaba Ali Alboori >> >> >> >> -- >> Mujtaba Ali Alboori >> >> _______________________________________________ >> Haskell-Cafe mailing list >> [hidden email] >> http://www.haskell.org/mailman/listinfo/haskell-cafe >> >> > > > > -- > keithsheppard.name > _______________________________________________ > Haskell-Cafe mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/haskell-cafe _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
Thanks a lot guys you were really helpful
func f1 f2 x = (f1 x) || (f2 x) is working for me
On Sun, Apr 18, 2010 at 6:27 PM, Thomas Davie <[hidden email]> wrote: To do this, you need not just fmap (composition), but also ap, or the combined form, liftA2: -- Mujtaba Ali Alboori _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
In reply to this post by Sean Leather
On Sun, Apr 18, 2010 at 10:25 AM, Sean Leather <[hidden email]> wrote:
As you mention below the function arrow (->), is right associative. For this reason I like to think of the above function as binary, instead of a function of 3 arguments. Making the associativity explicit we can rewrite it: (.) :: (b -> c) -> (a -> b) -> (a -> c) Now we can see that it is a binary function on functions. You're mileage may vary, but I find this form more intuitive. Jason _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
Free forum by Nabble | Edit this page |