Sure thing, as long as you don't mind passing the parameters as a list:

> let sum = List.fold1_left (+);;

val sum : (int list -> int)

> [1;2;3;4;5] |> sum;;

val it : int = 15

For strings, there is already a concat function:

["a";"b";"c"] |> String.concat ",";;

> val it : string = "a,b,c"

However, if you are trying to, for example, export a method with a variable number of arguments, similar to a function using C++ va_args, then I don't know if that's possible in F#.

By on 4/23/2008 9:47 PM ()

Thaks for your replies.

> I suppose that "any number" is limited (else, it becomes quite hard).

Yet, it should be any number, and even any combination of types.

I'm writing a library that generates values of a certain type (i.e. ints, strings,...). I'd like to be able to evaluate any function with arguments of those types.

I don't think any of the proposed solutions are possible, as all of them require me to enumerate explicitly the enormous amount of combinations of arguments that can occur.

Right now I can do this by forcing the user to tuple his/her arguments, and provide generator combinators to provide the appropriate generated type,

e.g.

val eval: 'a Gen -> 'a -> bool -> bool

eval (Gen.Tuple(Gen.Int, Gen.String)) (fun (i,s) -> f i s)

I'd like to turn this into simply:

eval f

Since in principle I should be able to determine the types of arguments that f expects and generate the appropriate values for them.

I neglected to say that we can assume the functions f always return bool.

What's the application? random/exhaustive testing of function properties, actually I am porting Haskell's QuickCheck. I'm quite far along, when something is working (next week or so), I plan to release it.

Kurt

By on 4/24/2008 8:19 AM ()

What's the goal exactly? Where do the arguments come from?

If you just want a function that takes a function and then the arguments, there's nothing to do and it works for any number of arguments. :)

1
2
3
4
5
6
let apply f = f


apply (+) 2 3

apply abs -4

I suppose that "any number" is limited (else, it becomes quite hard). You can also tag your functions. A type safe solution, with OCaml-style (I don't know if it fits for you):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
type ('a, 'b, 'c, 'd, 'e) Functions =

  | F1 of ('a -> 'b)

  | F2 of ('a -> 'b -> 'c)

  | F3 of ('a -> 'b -> 'c -> 'd)

  | F4 of ('a -> 'b -> 'c -> 'd -> 'e)


let apply = function

  | F1 of f -> f ...

  | ...

You could also try method overloading (like here: [link:cs.hubfs.net]

Please give more details if you dont' like these solutions.

Laurent

By on 4/23/2008 2:32 PM ()
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