Recent

Author Topic: functional programming  (Read 4185 times)

VTwin

  • Hero Member
  • *****
  • Posts: 855
  • Former Turbo Pascal 3 user
functional programming
« on: January 18, 2020, 03:28:18 am »
I am trying to understand the buzz about "functional programming". It sounds like using functions without modifying parameters, and no global side effects. This sounds old school to me. Anyone care to elucidate?
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn r62669 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

jamie

  • Hero Member
  • *****
  • Posts: 2317
Re: functional programming
« Reply #1 on: January 18, 2020, 03:31:21 am »
Its just noise, don't worry about it!  :D


And if you didn't read about it, you never would of been concerned about it!

So slack off on the reading of these new twisted buzz words describing the past one's
« Last Edit: January 18, 2020, 03:34:01 am by jamie »
Number 1 at blue screen app creations!

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5968
    • wiki
Re: functional programming
« Reply #2 on: January 18, 2020, 04:19:11 am »
Well, there is always some buzz.
If you want some good background on the paradigm, then maybe this will help: https://www.youtube.com/watch?v=sqV3pL5x8PI


@jamie: nice pun.

VTwin

  • Hero Member
  • *****
  • Posts: 855
  • Former Turbo Pascal 3 user
Re: functional programming
« Reply #3 on: January 18, 2020, 06:19:00 am »
Thanks. I looked at  that that and other referees including:

https://youtu.be/0if71HOyVjY

which, to me, explained nothing useful. It sounds more like programming style, constant input with no side effects.

I imagine there is more to it, but I do not see any reason to spend more time on it.

Cheers

“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn r62669 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

ur_naz

  • Newbie
  • Posts: 4
Re: functional programming
« Reply #4 on: January 18, 2020, 07:24:25 am »
With FP you can always code in functional style or semi-functional style or near-functional style or none-functional style as well.

julkas

  • Hero Member
  • *****
  • Posts: 538
  • KISS principle / Lazarus 2.0.6 / FPC 3.0.4
Re: functional programming
« Reply #5 on: January 18, 2020, 10:27:28 am »
With FP you can always code in functional style ...
Style - yes. But it would be just mimic or bad parody.
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

simone

  • Sr. Member
  • ****
  • Posts: 300
Re: functional programming
« Reply #6 on: January 18, 2020, 10:32:06 am »
I am trying to understand the buzz about "functional programming". It sounds like using functions without modifying parameters, and no global side effects. This sounds old school to me. Anyone care to elucidate?

You are right. Indeed Lisp was born in 1958!

PascalDragon

  • Hero Member
  • *****
  • Posts: 973
  • Compiler Developer
Re: functional programming
« Reply #7 on: January 18, 2020, 11:12:34 am »
I think an important part of functional programming is also function pointers to be first level citizens allowing them to be passed around easily (together with state), something that FPC will only gain once anonymous functions are implemented.

MarkMLl

  • Hero Member
  • *****
  • Posts: 639
Re: functional programming
« Reply #8 on: January 18, 2020, 11:13:54 am »
I am trying to understand the buzz about "functional programming". It sounds like using functions without modifying parameters, and no global side effects. This sounds old school to me. Anyone care to elucidate?

You are right. Indeed Lisp was born in 1958!

As I understand it, there's also various things like generating functions on-the-fly.  LISP was invented by people who were peripherally involved with designing and standardising ALGOL but apparently didn't quite "get it", FORTH tackled the same problems some 20 years later. By now "The LISP Way" is so far from mainstream applications and system programming that I really don't see its relevance.

The first three chapters of http://web.engr.oregonstate.edu/~budd/Books/leda/ are freely available and offer a good comparison of several different programming styles. The examples might be heavy going but (writing as an engineer with limited tolerance of "fancy CS stuff") are worth reading through carefully.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

jamie

  • Hero Member
  • *****
  • Posts: 2317
Re: functional programming
« Reply #9 on: January 18, 2020, 03:53:24 pm »
Code: Pascal  [Select]
  1. Function Sum(inputs:array of integer):Integer;
  2. Var
  3.   V:Integer;
  4. Begin
  5.   Result:=0;
  6.   For V in inputs do Result+=V;
  7. End;
  8.  
  9. procedure TForm1.Button1Click(Sender: TObject);
  10. begin
  11.  Caption := Sum([1,2,3,4,5]).ToString;
  12. end;                        
  13.  
  14.  

Nothing new here ;)
Number 1 at blue screen app creations!

lucamar

  • Hero Member
  • *****
  • Posts: 2420
Re: functional programming
« Reply #10 on: January 18, 2020, 05:05:32 pm »
Nothing new here ;)

I'm also a little bit lost with functional programming but I don't think your code quite qualifies as that ;)

Of course, my (small) understanding comes basically from the Wikipedia article, nothing more fancy. :D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8143
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: functional programming
« Reply #11 on: January 18, 2020, 06:22:46 pm »
I am trying to understand the buzz about "functional programming". It sounds like using functions without modifying parameters, and no global side effects. This sounds old school to me. Anyone care to elucidate?
Partially it can be viewed that way, but functional programming can be viewed in a wider way. The go to language for functional programming, Haskell, is one of very few that implements functional programming as pure as possible.

First, order of evaluation is undefined, the compiler should be able to generate code that evaluate either way it considers the most efficient. e.g. if you do:
Code: Haskell  [Select]
  1. y = f x a b
  2.  
there's no guarantee whether x, a or b will be evaluated first. In fact, it's possible that none might be evaluated due to the next feature.

Second, it enforces lazy evaluation, i.e. an expression will never be evaluated until the time its value is required. e.g.:
Code: Haskell  [Select]
  1. y = f (x + 1) (z * 5)
  2.  
if either `x + 1` or `z + 5` isn't immediately needed, they will be passed around as is, as an expression. Even better:
Code: Haskell  [Select]
  1. y = f (x + 1) (z * 5)
  2. f a b = g (a - 1) (b / 5)
  3.  
will also evaluate neither `a - 1` nor `b / 5`. But once you add:
Code: Haskell  [Select]
  1. y = f (x + 1) (z * 5)
  2. f a b = g (a - 1) (b / 5)
  3. g a b = a + b
  4.  
all of those expressions will be calculated in one go, possibly at compile time depending on the values or at least curried to be the simplest expression ever. So instead of 1 assignment over 2 functions the compiler will only generate a single expression:
Code: Haskell  [Select]
  1. y = x + z
  2.  

Last but not least, (almost) full type inference (since sometimes you still have to specify one as the generic candidates might be too generic). This is not really possible in imperative languages due to the types are implemented (Haskell's type system could be a language in its own).

Some bits of functional programming can indeed be stolen and have been stolen by many imperative languages, such as: list comprehension, first class functions, etc.

Otto

  • Jr. Member
  • **
  • Posts: 56
Re: functional programming
« Reply #12 on: January 18, 2020, 06:48:41 pm »
Among the few programming languages that I usually use, C# is one that shows a constant tendency to introduce novelties. It introduced the possibility of using functional programming and then, to simplify its syntax, introduced the Lambda expression (In my opinion, they are useful from an expressive point of view, but they are not indispensable).
Searching the internet, with reliable search engines (duckduckgo.com) you can find useful examples.

Functional-programming-in-Csharp https://www.codeproject.com/Articles/375166/Functional-programming-in-Csharp

Otto.
« Last Edit: January 18, 2020, 06:59:53 pm by Otto »
Kind regards.
[I'm not a native English speaker.]

simone

  • Sr. Member
  • ****
  • Posts: 300
Re: functional programming
« Reply #13 on: January 18, 2020, 07:15:04 pm »
Functional programming has its hype in this period. In truth, it is a revival, not a novelty, given that the functional one is the first invented paradigm.

Personally I don't believe that pure functional languages can be used on a large scale in software production, but certainly presents interesting aspects for some contexts. For this reason, the current trend is to add functional characteristics to languages ​​that are not. Java and C # have followed this path. Even Delphi, though in a more limited way.

I would like to have the first class functions in FreePascal, in particular the possibility of using anonymous functions and closures.

MarkMLl

  • Hero Member
  • *****
  • Posts: 639
Re: functional programming
« Reply #14 on: January 18, 2020, 07:21:06 pm »
In truth, it is a revival, not a novelty, given that the functional one is the first invented paradigm.

How do you work that out? Straight imperative programming preceded functional, since it is the paradigm that underlies- among others assembly- and machine-code programming, and was explored by Babbage et al. in the mid C19th.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.