Math questions

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

Math questions

Mujtaba Boori
Hello 
I am try to solve this equation

Define a higher order function  that tests whether two functions , both defined on integers , coincide for all integers between 1 and 100

 how can I solve this ?
is there any thing in Haskell library to solve this kind ?    

--
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: Math questions

caseyh
Quoting Mujtaba Boori <[hidden email]>:

> Hello
> I am try to solve this equation
>
> Define a higher order function  that tests whether two functions , both
> defined on integers , coincide for all integers between 1 and 100
>
>  how can I solve this ?
> is there any thing in Haskell library to solve this kind ?
>
> --
> Mujtaba Ali Alboori
>

This sounds like homework.

In any case, won't your HOF take as input the two functions and the  
input range to check.
Then think about how you might compare the output of the two functions.


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

Re: Math questions

Daniel Fischer-4
In reply to this post by Mujtaba Boori
On Tuesday 25 May 2010 23:47:30, Mujtaba Boori wrote:
> Hello
> I am try to solve this equation
>
> Define a higher order function  that tests whether two functions , both
> defined on integers , coincide for all integers between 1 and 100
>
>  how can I solve this ?
> is there any thing in Haskell library to solve this kind ?

Sure. Lots of things to do it in different ways. All boil down to
- for each integer from 1 to 100
- check whether f i == g i

Look at
http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Prelude.html
, there are  useful functions for this. You will probably want to use 'and'
or 'all'.

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

Re: Math questions

Mujtaba Boori
Thank you all you , that is really impressive .


I will read your response carefully , and  I think this it is enough to understand it.

On 25 May 2010 23:43, Daniel Fischer <[hidden email]> wrote:
On Tuesday 25 May 2010 23:47:30, Mujtaba Boori wrote:
> Hello
> I am try to solve this equation
>
> Define a higher order function  that tests whether two functions , both
> defined on integers , coincide for all integers between 1 and 100
>
>  how can I solve this ?
> is there any thing in Haskell library to solve this kind ?

Sure. Lots of things to do it in different ways. All boil down to
- for each integer from 1 to 100
- check whether f i == g i

Look at
http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Prelude.html
, there are  useful functions for this. You will probably want to use 'and'
or 'all'.




--
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: Math questions

Bugzilla from 1@234.cx
In reply to this post by Mujtaba Boori
Mujtaba Boori wrote:

> Define a higher order function  that tests whether two functions , both
> defined on integers , coincide for all integers between 1 and 100

If this really is a homework question, I dare you to submit this
solution.  Try it for yourself, it works fine. :-)

module Main where

import Control.Monad
import Data.IORef
import System.IO.Unsafe

forLoop :: Int -> Int -> (Int -> IO ()) -> IO ()
forLoop start stop body = mapM_ body [start..stop]

test :: (Eq a) => (Int -> a) -> (Int -> a) -> Bool
test f1 f2 = unsafePerformIO $ do
     goodSoFar <- newIORef True
     forLoop 1 100 $ \i ->
       when (f1 i /= f2 i) $ writeIORef goodSoFar False
     readIORef goodSoFar

testFunc1 27 = "numpty"
testFunc1 i = show i

testFunc2 270 = "numpty"
testFunc2 i = show i

main = do
   print $ test show testFunc1
   print $ test show testFunc2

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

Re: Re: Math questions

Dan Doel
On Wednesday 26 May 2010 5:38:57 pm Pete Chown wrote:
> test :: (Eq a) => (Int -> a) -> (Int -> a) -> Bool
> test f1 f2 = unsafePerformIO $ do
>      goodSoFar <- newIORef True
>      forLoop 1 100 $ \i ->
>        when (f1 i /= f2 i) $ writeIORef goodSoFar False
>      readIORef goodSoFar

The problem with this algorithm is that it needlessly tests f1 against f2 for
all i, even when a non-matching value has has already been found. Using the
power of call-with-current-continuation, I have constructed an algorithm that
lacks this deficiency:

    import Control.Monad.Cont

    test f g = flip runCont id . callCC $ \escape ->
      do forM_ [1..100] $ \n ->
           when (f n /= g n) $
             escape False
         return True

This should perform almost 75% less work in the testFunc1 case! It certainly
feels much snappier.

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

Re: Re: Math questions

Mujtaba Boori
Pete Chown and Dan Doel. Thank you for your solution. I actually It was not homework . It was just a a past exam question trying to answer . 
but your solution is very long , so I don't think he wants answer this long in the exams. I think this answer agree100 f g = map f xs == map g xs where xs = [1..100] from  Richard O'Keefe  
is do the job.

Anyway , your solution help understand more about Haskell not only this question . Many thanks to you guys. 

On 27 May 2010 02:04, Dan Doel <[hidden email]> wrote:
On Wednesday 26 May 2010 5:38:57 pm Pete Chown wrote:
> test :: (Eq a) => (Int -> a) -> (Int -> a) -> Bool
> test f1 f2 = unsafePerformIO $ do
>      goodSoFar <- newIORef True
>      forLoop 1 100 $ \i ->
>        when (f1 i /= f2 i) $ writeIORef goodSoFar False
>      readIORef goodSoFar

The problem with this algorithm is that it needlessly tests f1 against f2 for
all i, even when a non-matching value has has already been found. Using the
power of call-with-current-continuation, I have constructed an algorithm that
lacks this deficiency:

   import Control.Monad.Cont

   test f g = flip runCont id . callCC $ \escape ->
     do forM_ [1..100] $ \n ->
          when (f n /= g n) $
            escape False
        return True

This should perform almost 75% less work in the testFunc1 case! It certainly
feels much snappier.

-- Dan
_______________________________________________
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: Math questions

Yitzchak Gale
Mujtaba Boori wrote:
> I think this answer agree100 f g = map f xs == map g xs where
> xs = [1..100] from  Richard O'Keefe
> is do the job.

agree100 = (==) `on` for [1..100]

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

Re: Re: Math questions

Richard A. O'Keefe

On May 27, 2010, at 11:50 PM, Yitzchak Gale wrote:

> Mujtaba Boori wrote:
>> I think this answer agree100 f g = map f xs == map g xs where
>> xs = [1..100] from  Richard O'Keefe
>> is do the job.
>
> agree100 = (==) `on` for [1..100]

Search for "on" and "for" in the Haskell 98 Report and you
will not find them.  If you want to tell someone to use them,
you ought to tell them where to find them.

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

Re: Re: Math questions

Ivan Lazar Miljenovic
On 28 May 2010 09:37, Richard O'Keefe <[hidden email]> wrote:
>
> On May 27, 2010, at 11:50 PM, Yitzchak Gale wrote:
>>
>> agree100 = (==) `on` for [1..100]
>
> Search for "on" and "for" in the Haskell 98 Report and you
> will not find them.  If you want to tell someone to use them,
> you ought to tell them where to find them.

You mean like this?

http://haskell.org/hoogle/?hoogle=on
http://haskell.org/hoogle/?hoogle=for

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

Re: Re: Math questions

Richard A. O'Keefe

On May 28, 2010, at 1:05 PM, Ivan Miljenovic wrote:

> On 28 May 2010 09:37, Richard O'Keefe <[hidden email]> wrote:
>>
>> On May 27, 2010, at 11:50 PM, Yitzchak Gale wrote:
>>>
>>> agree100 = (==) `on` for [1..100]
>>
>> Search for "on" and "for" in the Haskell 98 Report and you
>> will not find them.  If you want to tell someone to use them,
>> you ought to tell them where to find them.
>
> You mean like this?
>
> http://haskell.org/hoogle/?hoogle=on
> http://haskell.org/hoogle/?hoogle=for

Yes, that kind of thing.
Remember, this was a BEGINNER-type question.
If one is giving a *serious* answer, it has to be an answer a
beginner (who has almost certainly never heard of Traversable)
can make sense of, and if it uses functions that are pretty
much certain not to be in said beginner's text book, said
beginner has to be told quite clearly where to find them.


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

Re: Re: Math questions

Ivan Lazar Miljenovic
On 28 May 2010 14:52, Richard O'Keefe <[hidden email]> wrote:> Yes,
that kind of thing.
> Remember, this was a BEGINNER-type question.
> If one is giving a *serious* answer, it has to be an answer a
> beginner (who has almost certainly never heard of Traversable)
> can make sense of, and if it uses functions that are pretty
> much certain not to be in said beginner's text book, said
> beginner has to be told quite clearly where to find them.

I think this is an example of the "Haskell effect" (more typically
seen on #haskell), which can be categorised as follows:

1) Someone asks a (usually rather simple) question.

2) People discuss this and provide several straightforward and relevant answers.

3) Out of curiosity (and probably a fair dose of masochism) others
then start to golf the solution into the most interesting approach
possible.

4) The person that asked the question initially gets lost (but is
quite often awed at all the amazing stuff that's going on around them
zooming past at light speed).

5) People suddenly remember that there was an initial question and
make an attempt to explain the more advanced solutions to the person
that asked the question in the first place.

6) They smile and nod and pretend they understand whilst putting a
note onto their copious TODO list to someday sit down and try to work
out wtf was going on.

7) ???

8) Profit!!!

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

Re: Re: Math questions

caseyh
This is an effect with any language that offers a very high degree of
abstraction.

>
>I think this is an example of the "Haskell effect" (more typically
>seen on #haskell), which can be categorised as follows:
>
>1) Someone asks a (usually rather simple) question.
>
>2) People discuss this and provide several straightforward and relevant answers.
>
>3) Out of curiosity (and probably a fair dose of masochism) others
>then start to golf the solution into the most interesting approach
>possible.
>
>4) The person that asked the question initially gets lost (but is
>quite often awed at all the amazing stuff that's going on around them
>zooming past at light speed).
>
>5) People suddenly remember that there was an initial question and
>make an attempt to explain the more advanced solutions to the person
>that asked the question in the first place.
>
>6) They smile and nod and pretend they understand whilst putting a
>note onto their copious TODO list to someday sit down and try to work
>out wtf was going on.
>
>7) ???
>
>8) Profit!!!

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

Re: Re: Math questions

Dan Doel
In reply to this post by Ivan Lazar Miljenovic
On Thursday 27 May 2010 9:05:40 pm Ivan Miljenovic wrote:

> On 28 May 2010 09:37, Richard O'Keefe <[hidden email]> wrote:
> > On May 27, 2010, at 11:50 PM, Yitzchak Gale wrote:
> >> agree100 = (==) `on` for [1..100]
> >
> > Search for "on" and "for" in the Haskell 98 Report and you
> > will not find them.  If you want to tell someone to use them,
> > you ought to tell them where to find them.
>
> You mean like this?
>
> http://haskell.org/hoogle/?hoogle=on
> http://haskell.org/hoogle/?hoogle=for

for from Data.Traversable cannot be what was intended, because it would
require the functions to have type:

  a -> F b

for some Applicative F (which, moreover, should be providing the Eq instance
you want to check with). This works for the identity functor (assuming the
instance is declared), but the code is still not right, since the definition
given doesn't lift the functions.

Rather:

  for = flip map

presumably. But this definition isn't in any of the standard libraries (at
least visibly) to my knowledge.

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

Re: Re: Math questions

Yitzchak Gale
In reply to this post by Richard A. O'Keefe
Richard O'Keefe wrote:
> If one is giving a *serious* answer, it has to be an answer a
> beginner (who has almost certainly never heard of Traversable)
> can make sense of, and if it uses functions that are pretty
> much certain not to be in said beginner's text book, said
> beginner has to be told quite clearly where to find them.

You're right. It wasn't really a serious answer, it was meant
for fun. Definitely along the lines of Ivan's "Haskell Effect".
I think Mujtaba has been enjoying this discussion. I hope
we'll be seeing more of him around here.

Thanks to you and Ivan for pointing out sources, though.

My "for" function was indeed "flip map". Perhaps it's not
in any library, but it's often seen on the #haskell IRC
channel. :)

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

Re: Re: Math questions

Ivan Lazar Miljenovic
Yitzchak Gale <[hidden email]> writes:

> My "for" function was indeed "flip map". Perhaps it's not
> in any library, but it's often seen on the #haskell IRC
> channel. :)

Hmmm, I had never heard of this but going back through my logs I do
indeed find nornagon, jethr0 and jmcarthur all either stating this
definition or advocating it.  Alternate definitions that I found
include:

* pam (ski)

* -<< (vixey)

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

Re: Re: Math questions

Mujtaba Boori
sure I did enjoy the discussion here Yitzchak Gale.  I have already submitted several questions ,and you guys were very helpful. However , I am not sure how I will use Haskell other than my Haskell course that has just finished.  

On 28 May 2010 14:52, Ivan Lazar Miljenovic <[hidden email]> wrote:
Yitzchak Gale <[hidden email]> writes:

> My "for" function was indeed "flip map". Perhaps it's not
> in any library, but it's often seen on the #haskell IRC
> channel. :)

Hmmm, I had never heard of this but going back through my logs I do
indeed find nornagon, jethr0 and jmcarthur all either stating this
definition or advocating it.  Alternate definitions that I found
include:

* pam (ski)

* -<< (vixey)

--
Ivan Lazar Miljenovic
[hidden email]
IvanMiljenovic.wordpress.com



--
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: Math questions

Maciej Piechotka
In reply to this post by Mujtaba Boori
On Tue, 2010-05-25 at 22:47 +0100, Mujtaba Boori wrote:

> Hello
> I am try to solve this equation
>
>
> Define a higher order function  that tests whether two functions ,
> both defined on integers , coincide for all integers between 1 and 100
>
>
>  how can I solve this ?
> is there any thing in Haskell library to solve this kind ?    

Not so beginner answer (but not advanced I guess):

import Control.Applicative

check f g = all (liftA2 (==) f g) [1..100]

Regards

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

signature.asc (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Math questions

Bugzilla from 1@234.cx
In reply to this post by Mujtaba Boori
Mujtaba Boori wrote:

> sure I did enjoy the discussion here Yitzchak Gale. I have already
> submitted several questions ,and you guys were very helpful. However , I
> am not sure how I will use Haskell other than my Haskell course that has
> just finished.

I would very much recommend the Real World Haskell book if you want to
write real programs with it.  I was interested in Haskell for a long
time, but it was only after I read that book that I could see how to
structure larger programs.

I've only written a few real world programs with Haskell, but I do think
there are some situations where it reduces development time compared to
other languages.  Ideally, I suppose, you would have a type-safe
language with the flexibility of Python.  Haskell isn't quite that, but
it probably gets closer than other languages, thanks to its flexible
type system and support for type inference.  It's also particularly good
for parsing text (Parsec is much nicer than yacc) and multithreading
(the STM module is particularly good).

Downsides: the libraries aren't as complete or well tested as some other
languages.  Also I suppose it has to be said that functions which update
state can become clumsy.  The state monad helps with this, but you can
still find yourself writing more code than you would need in other
languages.

Pete

PS here is a more serious answer to the original question...

test f1 f2 = and [f1 i == f2 i | i <- [1..100]]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe