help with Haskell programming

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

help with Haskell programming

Mujtaba Boori
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
Reply | Threaded
Open this post in threaded view
|

Re: help with Haskell programming

Thomas Davie
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
Reply | Threaded
Open this post in threaded view
|

Re: help with Haskell programming

Keith Sheppard
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
Reply | Threaded
Open this post in threaded view
|

Re: help with Haskell programming

Mujtaba Boori
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:
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
Reply | Threaded
Open this post in threaded view
|

Re: Re: help with Haskell programming

Keith Sheppard
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
Reply | Threaded
Open this post in threaded view
|

Re: Re: help with Haskell programming

Sean Leather
In reply to this post by Mujtaba Boori
 
This is the annoying part about Haskell . I can not understand composition .

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
Reply | Threaded
Open this post in threaded view
|

Re: Re: help with Haskell programming

Thomas Davie
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
Reply | Threaded
Open this post in threaded view
|

Re: Re: help with Haskell programming

Mujtaba Boori
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:

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




--
Mujtaba Ali Alboori

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Re: help with Haskell programming

Jason Dagit-2
In reply to this post by Sean Leather


On Sun, Apr 18, 2010 at 10:25 AM, Sean Leather <[hidden email]> wrote:
 
This is the annoying part about Haskell . I can not understand composition .

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.

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