The tactful style of programming using F #

This is not a very important issue, but I would like to see an example of tacit programming in F #, where my point functions can have several arguments (not as a list or tuple).

And secondly, how such functions can manipulate a complex data structure. I am trying in F # Interactive, but have not yet succeeded.

I tried for example:

> (fun _ -> (fun _ -> (+))) 333 222 111 555 

Is it correct?

and

 > (fun _ -> (fun _ -> (+))) "a" "b" "c" "d";; val it : string = "cd" 
+6
f # tacit-programming
source share
2 answers

F # does not contain some of the basic functions available in Haskell (mainly because F # programmers usually prefer an explicit programming style and use the pointfree style only in the most obvious cases where this does not impair readability).

However, you can define several basic combinators, for example:

 // turns curried function into non-curried function and back let curry f (a, b) = fab let uncurry fab = f (a, b) // applies the function to the first/second element of a tuple let first f (a, b) = (fa, b) let second f (a, b) = (a, fb) 

Now you can implement a function to add the lengths of two lines using combinators as follows:

 let addLengths = uncurry (( (first String.length) >> (second String.length) ) >> (curry (+))) 

This creates two functions that apply String.length to the first / second element of the tuple, then writes them, and then adds the elements of the tuple with + . All this is wrapped in uncurry , so you get a function like string -> string -> int .

+4
source share

In F #, the arity of functions is fixed, so you cannot write both

 (op) 1 2 

and

 (op) 1 2 3 4 

for any given op operator. You will need to use a list or other data structure if you want to. If you are just trying to avoid named variables, you can always do "1 + 2 + 3 + 4". The most idiomatic way to add a list of numbers to F # is List.sum [1;2;3;4] , which also avoids variables.

+2
source share

All Articles