Write a function using recursion or higher order functions?

F a =True |otherwise= x:f a xs We see that your type is exists :: (t -> Bool) -> t -> Bool So exists must take two parameters, one predicate function of type (t -> Bool) and one list of type t. It returns a Bool. This seem okay as per our intention of the specification.

Let us look at the first line of your terms: exists f a = error This function suddenly takes three parameters. The f and the empty list constructor looks okay, but the a is not mentioned in the type specification. Hence, we prune it out: exists f = error Now, the error returned is not of boolean value.

But the spec says it must be. Let us suppose we are asking exists (Then would a natural answer to the question be True or False? Or paraphrased, is there any element x in satisfying the predicate f x?

On to the next line, exists f a (x:xs) |if x? F a =True |otherwise= x:f a xs We learned that the a has to go by the type specification, so let us prune it. Since we have now grown a natural dislike for the a, why not prune it everywhere it occur.

Also, since the if will produce a syntax error, lets rid ourselves of that too: exists f (x:xs) | x? F = True | otherwise = x:f xs The x? F does not make much sense, but f x does.

The guard variant will be taken if f x returns true. Now, the True which is returned here sounds about right. It signifies that we have found an element in the list matching the predicate - and lo n' behold, x might be it!

So we turn our attention to the final line. The otherwise means that the guard f x did not return True. As a consequence, the x is not satisfying the predicate, so we must search the rest of the list.

The Right-hand-side x : f xs is peculiar. The : means that we will try to return a list, but the return type of the function is something of type Bool. The type checker won't like us if we try this.

Furthermore, we have no reason to look at the x anymore since we just determined it does not satisfy the predicate. The key thing you are missing is that we need recursion at this point. We need to search the tail xs of the list somehow - and recursion means to invoke the exists function on the tail.

Your general track is right, but ask again if something is unclear. One trick might be to go by the types for the recursion case: "What do I have to supply exists for it to return a Bool value? ".

Dan caught that the :: operator is :, fixed. – I GIVE CRAP ANSWERS Jan 24 '11 at 11:57.

Your desired example usage is this ghci>exists (>2) 1,2,3 True Stop. Hoogle time. ( Bool) and the second is a list a.

The desired result is a Bool Hoogling that type signature, (a -> Bool) -> a -> Bool, the top hits are any, all, and find. As Andrew has noted, any is the one that behaves like the "exists" function. As a side note, my first thought was to use find, which returns a Maybe a, and then pattern match.

If it returns Nothing, then the result would be False, otherwise True. As another side note, the actual implementation is simply any p = or . Map p.

The third side note is probably the answer to your actual question. How is map defined? Hoogle is once again your friend.

Search for the method's name and you can find a page that links to the source. I suggest you do this for map and or, but will only show map here. Map _ = map f (x:xs) = f x : map f xs That's the basic way to recurse over a list.

RecursiveCall f (x:xs) = f x : recursiveCall f xs But if it can be written with map, filter, or foldl/foldr, then you should do it with these recursive methods. (Stop. Hoogle time.

Search for those method names and check out the source; it's pretty straightforward. ).

I like the answer, even though it might be over the head of the questioner. The cool thing is that you can now come back to the this question and learn something new as your skill level improves. – I GIVE CRAP ANSWERS Jan 24 '11 at 11:59 1 +1 for a great answer and "Stop.

Hoogle time. " Made my morning :-) – Zach L Jan 24 '11 at 13:57 +1 for Hoogle. I didn't know about it.

– Stephane Rolland Jan 24 '11 at 14:20.

I think the function you want already exists -- any: Prelude> :t any any :: (a -> Bool) -> a -> Bool Prelude> any ( any ( any (.

Actually your original version wasn't too far from working. To fix it, write: exists :: (t -> Bool) -> t -> Bool exists _ = False exists f (x:xs) | f x = True | otherwise = exists f xs.

Alternately, exists f (x:xs) = f x || exists f xs . See hackage.haskell.org/packages/archive/bas...… – steamer25 Jan 25 '11 at 23:55 Yes, that's much nicer, but the point was to get the original code "working". – Landei Jan 27 '11 at 21:29.

Instead of using x in f, just apply f to x using f x as the predicate in the if statement. Your otherwise clause should also return a Bool: the result of exists on the rest of the list.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions