I think you mean (bracket issue):

1
2
3
4
    let rec foo (x:'a) (y:'a) : (('a->bool) * bool) =
        if x=y
        then (foo x,true)
        else (foo y,false)

You can't actually make it recursive. There will always be a type mismatch between the return type of the rec declaration (which you want to be

1
((a->bool) * bool)

, that is a partially applied function and the result), and the type of the partially applied function which is

1
('a -> returntypeofrecfunction) * bool

Basically each recursion adds a level of indirection ( -> ) to the type:

1
('a -> (a->bool) * bool) * bool

... etcNow let me think how to rewrite it...

By on 1/25/2010 5:48 AM ()

Adding a constructor somewhere would do the trick, I suppose.

By on 1/25/2010 5:53 AM ()

Exactly. You could do something like:

1
2
3
4
5
6
7
    type Predicate<'a> =
        | Pred of ('a -> Predicate<'a>) * bool
        member p.Apply = match p with Pred(func, _) -> func
        member p.Value = match p with Pred(_, value) -> value
        
    let rec foo (x : 'a) (y : 'a) : Predicate<'a> =
        if x = y then (Pred(foo x, true)) else (Pred(foo y, false))

A bit verbose to use, but you could bend it to your needs :)

1
2
3
4
5
6
7
8
> foo 2 3;;
val it : Predicate<int> = Pred (<fun:foo@171-9>,false)

> (foo 2 3).Value;;
val it : bool = false

> (((foo 2 2).Apply 3).Apply 4).Value;;
val it : bool = false

Cheers

By on 1/25/2010 7:31 AM ()

thanks, that may negate my original intended usage though. What I want is the following pattern:

1
2
3
4
5
6
let normal_loop x y = if x = y then (normal_loop x,true) else (normal_loop y,false)

let init_fn y = (normal_loop_fn y,true)

let foo = foldback (fun x a -> a x) some_list init_fn

Such that I can distinguish the init stage from the normal looping stage. Currently, I am using Some/None but that means my (fun x a) needs to cater for this which really has nothing to do with the core business logic.

The alternative seems a bit verbal and I am not sure if it becomes more distraction(from a code maintainence perspective), needs to try and ask someone to read.

By on 1/25/2010 11:54 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper