# Math questions

19 messages
Open this post in threaded view
|

## Math questions

 Hello I am try to solve this equationDefine 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
Open this post in threaded view
|

## Re: Math questions

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

## Re: Math questions

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

## Re: Math questions

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

## Re: Math questions

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

## Re: Re: Math questions

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

## Re: Re: Math questions

Open this post in threaded view
|

## Re: Re: Math questions

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

## Re: Re: Math questions

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

## Re: Re: Math questions

 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=onhttp://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
Open this post in threaded view
|

## Re: Re: Math questions

 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=forYes, 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
Open this post in threaded view
|

## Re: Re: Math questions

Open this post in threaded view
|

## Re: Re: Math questions

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

## Re: Re: Math questions

 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=forfor 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
Open this post in threaded view
|

## Re: Re: Math questions

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

## Re: Re: Math questions

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

## Re: Re: Math questions

 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 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