8 Replies - 1613 Views - Last Post: 13 August 2015 - 04:48 PM

#1 BLincolnGreen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 13-August 15

Functional Programming: Conciseness/Lack of Explicitness

Posted 13 August 2015 - 01:52 AM

Hi,

First time poster here. Please redirect me if this is the wrong place to post this.

I've been watching videos/online courses about functional programming. One of the things that has struck me is that the presenters are almost universally psyched about not specifying types for variables, interfaces, etc.

And the languages that I've seen presented, F#, Scheme, Scala, Java 8 Lambdas, Racket(?) all seem to be organized around a very spare syntax where a lot of what is happening is implicit to the language runtime.

The presenters never seem to tie the conciseness back to the other FP advantages, concurrency, testability/provability, lambda calculus, etc.

Is there a relationship that I am missing between conciseness and the other advantages of FP? Is the runtime able to do more because the programmer has not explicitly stated any intent beyond the composition at hand? Is it just that modern languages tend to favor conciseness?

I am assuming that it is not all motivated by a desire to save keystrokes.

Would love to hear your thoughts.

Is This A Good Question/Topic? 1
  • +

Replies To: Functional Programming: Conciseness/Lack of Explicitness

#2 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1253
  • View blog
  • Posts: 4,043
  • Joined: 12-May 09

Re: Functional Programming: Conciseness/Lack of Explicitness

Posted 13 August 2015 - 04:43 AM

F# and Scala are both, in fact, strongly typed. For other "functional" languages in that camp, look at OCaml, Haskell, SML, etc. Many use some variation of a system called type inference, which is very much in line with testability/provability.

Many of these languages actually *do* involve specifying types, just not all over the place, where it may be redundant at best.

In Elm, for example:
my_fun x y =
  x + y



There are no type annotations, but the compiler can infer that the arguments to my_fun are both of type Number, and that the return type is also of type Number.

Elm, therefore, won't allow:

my_fun "hello" 3



Because "hello" isn't a valid type for that function, even though I never told the compiler that. Further, most of these languages allow type annotations:

my_fun : Number -> Number -> Number



This is helpful for two reasons:
  • The annotation helps others who read the source understand my intent
  • If I made a mistake in the signature or in the implementation, the compiler will tell me

Most documentation tools will be able to get the signature without you having to annotate it, of course.

Even going beyond types, though, many of those languages have method of specifying interfaces. Have you seen Scala Traits? What about Clojure Protocols?

In this file I specify a protocol that abstracts application logic, and then I dispatch on the functions defined in that protocol:
https://github.com/b...c/echo/core.clj


I think conciseness is a major advantage - using a "map" function over Java's verbose streaming API, first class functions over having to attach a method to an object, etc are all powerful primitives that make programming more enjoyable. My personal opinion is that Haskell and Scala often take this too far, with syntax that is arcane and overly flexible, but I understand that people desire that flexibility at times.
Was This Post Helpful? 2
  • +
  • -

#3 BLincolnGreen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 13-August 15

Re: Functional Programming: Conciseness/Lack of Explicitness

Posted 13 August 2015 - 08:06 AM

This is just the explanation I was looking for. Thanks for the reply.

So far I have just watched presenters talking about FP. The advantages of conciseness will probably be clearer to me once I am using a language. I do see that it makes for very clean looking code.

However, beyond common things like map, reduce, filter, etc. it was not clear to me what was going on in most of the code except to the extent that the presenter explained it. Some of the presentations were recorded university classes so the explanation did allow me to figure out what was happening. But if I muted it I couldn't tell you what the code on a particular slide was doing.

I have been developing for a while and would feel pretty confident about following what was going on in most imperative code snippets. I notice that you know a lot of the functional languages. Does the same apply in FP?
Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1253
  • View blog
  • Posts: 4,043
  • Joined: 12-May 09

Re: Functional Programming: Conciseness/Lack of Explicitness

Posted 13 August 2015 - 10:03 AM

View PostBLincolnGreen, on 13 August 2015 - 11:06 AM, said:

I have been developing for a while and would feel pretty confident about following what was going on in most imperative code snippets. I notice that you know a lot of the functional languages. Does the same apply in FP?


After a while, functional does become more intuitive, especially because at some point you find other uses for the part of the brain that is responsible for tracking changes to state (ignoring Scala for the moment here). I do think some of the abstractions are hard to reason about at first, but I can get code-blind about both imperative code and functional code if the author hasn't broken things apart well or of the logic is otherwise hard to find. There are many code samples in threads in our Java forums here that I have to seriously work to understand, and sometimes can't, because everything is so complicated.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 6996
  • View blog
  • Posts: 14,635
  • Joined: 16-October 07

Re: Functional Programming: Conciseness/Lack of Explicitness

Posted 13 August 2015 - 12:11 PM

There are a few factors for conciseness in FP, I think.

There's the idea of conforming to, or perhaps evoking, mathematical expressions. A mathematician will start using friggin Greek characters before they add another letter to a variable name! So, there's that.

Second, go way back to the early days of programming and look at the code. We still use i and j for loop vars out of tradition, but there was a whole lot of less then transparent naming going on in the past. At some point programmers started calling for variable names that were actually descriptive.

Now, in most programming environments, it's considered exceptionally bad form to not use descriptive variable names. FP, however, never really got that memo.

For some vars, names aren't all that helpful; there's only so many ways to say thingy. For some it is nearly required, but in FP it seems a point of pride to avoid it. I'll admit, the x,y,xs,ys convention of Haskell/Scala/... has kind of rubbed off on me. Then again, I'll camelCase my way to clarity in those languages as well; it's a give and take thing.

Just accept that conciseness is a thing that some programmers are really into. It's not required and it is often not desired; to a point it's more a function of style than anything else.
Was This Post Helpful? 2
  • +
  • -

#6 BLincolnGreen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 13-August 15

Re: Functional Programming: Conciseness/Lack of Explicitness

Posted 13 August 2015 - 12:39 PM

And the light dawns. Wish I could give this more than one upvote. Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 BLincolnGreen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 13-August 15

Re: Functional Programming: Conciseness/Lack of Explicitness

Posted 13 August 2015 - 01:43 PM

Let's make sure I am looking at the right light. I concluded that the goal of the conciseness in FP was to look more like math, less like code.

In contrast, in the snippet that you posted, once I twigged that the "post" was a forum post, the meaning was perfectly clear because in OO the goal of method names is to make sense in relation to the object of which they are a member. You'd get totally flamed in code review if you had an object with a method named "x".

OTOH, there is a lot of boilerplate code in "post" that is not present in your quote but which we can presume to be present in the actual Java class. In FP the boilerplate code does seem to really be largely absent.

I was able to follow your protocol definition in GitHub even though I don't know what an EchoApp is. So presumably there is allowance for explicitness when useful.
Was This Post Helpful? 0
  • +
  • -

#8 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1253
  • View blog
  • Posts: 4,043
  • Joined: 12-May 09

Re: Functional Programming: Conciseness/Lack of Explicitness

Posted 13 August 2015 - 02:41 PM

View PostBLincolnGreen, on 13 August 2015 - 04:43 PM, said:

In contrast, in the snippet that you posted, once I twigged that the "post" was a forum post, the meaning was perfectly clear because in OO the goal of method names is to make sense in relation to the object of which they are a member. You'd get totally flamed in code review if you had an object with a method named "x".


I think one of the biggest problems in code is naming things correctly, whether it be functional or imperative. I'm happiest when my function or class name feel like they accurately reflect the intent of said construct.
Was This Post Helpful? 0
  • +
  • -

#9 BLincolnGreen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 13-August 15

Re: Functional Programming: Conciseness/Lack of Explicitness

Posted 13 August 2015 - 04:48 PM

Ok. I am starting to appreciate the conciseness. I started to pseudocode what for...yield does to answer another post. In a language that I am making up on the fly. That's a lot of code!

Functional Programming: Monads have state? Scala Option
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1