Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tacit programming style using F#

It's not a practically important issue, but I'd like to see an example of tacit programming in F# where my point-free functions can have multiple arguments (not in form of a list or tuple).

And secondly, how such functions can manipulate a complex data structure. I'm trying it out in F# Interactive, but have no success yet.

I tried, for instance:

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

Is that right way?

And:

> (fun _ -> (fun _ -> (+))) "a" "b" "c" "d";;  

val it : string = "cd"
like image 222
Bubba88 Avatar asked May 08 '10 07:05

Bubba88


People also ask

What is a tacit programming language?

Tacit programming, also called point-free style, is a programming paradigm in which function definitions do not identify the arguments (or "points") on which they operate. Instead the definitions merely compose other functions, among which are combinators that manipulate the arguments.

What is functional style of programming?

Functional programming is a programming paradigm in which we try to bind everything in pure mathematical functions style. It is a declarative type of programming style. Its main focus is on “what to solve” in contrast to an imperative style where the main focus is “how to solve”.

What is functional programming example?

Functional programming is based on mathematical functions. Some of the popular functional programming languages include: Lisp, Python, Erlang, Haskell, Clojure, etc. Pure Functional Languages − These types of functional languages support only the functional paradigms. For example − Haskell.

What is FP in computing?

Function points measure the size of an application system based on the functional view of the system. The size is determined by counting the number of inputs, outputs, queries, internal files and external files in the system and adjusting that total for the functional complexity of the system.


1 Answers

F# doesn't contain some of the basic functions that are available in Haskell (mainly because F# programmers usually prefer the explicit style of programming and use pointfree style only in the most obvious cases, where it doesn't hurt readability).

However you can define a few basic combinators like this:

// turns curried function into non-curried function and back
let curry f (a, b) = f a b
let uncurry f a b = f (a, b)

// applies the function to the first/second element of a tuple
let first f (a, b) = (f a, b)
let second f (a, b) = (a, f b)

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

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

This constructs two functions that apply String.length to first/second element of a tuple, then composes them and then adds the elements of the tuple using +. The whole thing is wrapped in uncurry, so you get a function of type string -> string -> int.

like image 162
Tomas Petricek Avatar answered Oct 23 '22 04:10

Tomas Petricek