Recent

Author Topic: functional programming  (Read 9209 times)

simone

  • Hero Member
  • *****
  • Posts: 571
Re: functional programming
« Reply #15 on: January 18, 2020, 07:35:17 pm »
I'm talking about computer science, not automatic calculation or programmable mechanical machines. I refer to the Lambda calculus of Alonzo Church, which precedes the Turing machine.
« Last Edit: January 18, 2020, 07:47:16 pm by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: functional programming
« Reply #16 on: January 18, 2020, 07:54:19 pm »
Interesting discussion.

@Leledumbo
That does require quite a different mindset than I am used to. Is that more about Haskell, or functional programming in general?

I just watched a short video on lambda calculus, the way they presented a function, a black box with input and output, is how I normally try to write a function.

I guess anonymous functions are another significant piece of it though. Apparently in Delphi, but not in Free Pascal yet?

@simone
Apparently Turing was one of Church's PhD students.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

simone

  • Hero Member
  • *****
  • Posts: 571
Re: functional programming
« Reply #17 on: January 18, 2020, 08:01:13 pm »
@VTwin
Yes.

@MarkMLI
However, leaving theoretical issues for a while, Fortran (imperative) was born in 1954, where Lisp (functional) in 1958. Thus, from this point of view, you are right :)
« Last Edit: January 18, 2020, 08:13:10 pm by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: functional programming
« Reply #18 on: January 18, 2020, 08:27:13 pm »
I used Lisp, actually AutoLISP, for a while in the 80s. The joke was that it stood for Lost In Stupid Parentheses. :) I had no idea at the time it was a "functional" language.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: functional programming
« Reply #19 on: January 18, 2020, 08:45:31 pm »
I would like to have the first class functions in FreePascal, in particular the possibility of using anonymous functions and closures.

I guess anonymous functions are another significant piece of it though. Apparently in Delphi, but not in Free Pascal yet?

It's a work in progress.

simone

  • Hero Member
  • *****
  • Posts: 571
Re: functional programming
« Reply #20 on: January 18, 2020, 08:52:55 pm »
@VTwin

Parentheses (and prefix notation) also discouraged me when I studied Lisp at university. However, not all functional languages ​​have such a strong use of parentheses. For example language that derive from ML (Haskell is one of these), that I prefer because are statically typed (conversely from Lisp, Scheme and others, that are dynamically typed).


@PascalDragon
Thanks for update. Good news for me.
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: functional programming
« Reply #21 on: January 18, 2020, 10:15:30 pm »
I would like to have the first class functions in FreePascal, in particular the possibility of using anonymous functions and closures.
Speaking of stateless way of development.
Could one explain on how to test an anonymous function and/or closure?

Here's what I mean. For a regular "stateless" (or "pure"), yet NAMED function
Code: Pascal  [Select][+][-]
  1. function foo(args...): integer;
  2.  
I can clearly create a stand-alone test and verify that it returns the expected results.
(It's quite often desired for regression testing, when the implementation of foo() changes this way or another... i.e. different optimizations).

But when using an anonymous function / closure... creating a test seems to be problematic, because the entire function body needs to be copied over to the test itself.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: functional programming
« Reply #22 on: January 18, 2020, 10:43:25 pm »
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

Thanks. Nice video, well explained.

As an educator, I appreciate someone who can explain something in 10 minutes, as opposed to someone who drones on for half an hour or more without conveying any information. Too much of my life has been expended on the latter.
« Last Edit: January 18, 2020, 11:29:22 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

simone

  • Hero Member
  • *****
  • Posts: 571
Re: functional programming
« Reply #23 on: January 18, 2020, 11:05:23 pm »
[But when using an anonymous function / closure... creating a test seems to be problematic, because the entire function body needs to be copied over to the test itself.

Your question is interesting and the response it's not trivial.

A black box test would require, also for an anonymous function, an external behavior that must be observable. For example, in the following very simple code:

(from http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/anonymousmethods_xml.html)

Code: Pascal  [Select][+][-]
  1. function MakeAdder(y: Integer): TFuncOfInt;
  2. begin
  3. Result := { start anonymous method } function(x: Integer)
  4.   begin
  5.     Result := x + y;
  6.   end; { end anonymous method }
  7. end;

You can test the anonymous method using the Result variable of MakeAdder, to which it is assigned. 

« Last Edit: January 18, 2020, 11:08:55 pm by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: functional programming
« Reply #24 on: January 18, 2020, 11:21:26 pm »
I would like to have the first class functions in FreePascal, in particular the possibility of using anonymous functions and closures.
Speaking of stateless way of development.
Could one explain on how to test an anonymous function and/or closure?
You have the same problems with nested functions today already. Essentially there is no good way to test them.
Though in comparison to nested functions you could pass them to the caller and then have the test harnish invoke them using RTTI (closures are essentially interfaces with a single Invoke method).

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: functional programming
« Reply #25 on: January 19, 2020, 10:02:06 am »
I'm talking about computer science, not automatic calculation or programmable mechanical machines. I refer to the Lambda calculus of Alonzo Church, which precedes the Turing machine.

So what you mean to say is that the term "functional programming" was coined by computer science at its inception to describe what Lisp does. I quite simply don't buy it as a description of the real world.

(Slightly later) Sorry, I've just seen your

> However, leaving theoretical issues for a while, Fortran (imperative) was born in 1954, where Lisp (functional) in 1958. Thus, from this point of view, you are right :)

I'd also say that I'm not /completely/ hostile to this sort of thing, and I've seen some quite interesting work which used Lisp as a low-level stepping stone to something that more closely resembled what today we'd call a "real" language. But I've studied the history of early programming languages sufficiently to be able to see exactly "where McCarthy was coming from" and what he was trying to do, and while I don't deny that some interesting work has come out of the Lisp camp it was, basically a dead-end approach: in much the same way that intersting concepts came out of Modula-2 while the language itself was a dead end.

MarkMLl
« Last Edit: January 19, 2020, 10:10:30 am by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

simone

  • Hero Member
  • *****
  • Posts: 571
Re: functional programming
« Reply #26 on: January 19, 2020, 11:37:29 am »
So what you mean to say is that the term "functional programming" was coined by computer science at its inception to describe what Lisp does. I quite simply don't buy it as a description of the real world.

Functional prgramming is a computational model that arises from lambda calculus, older than Turing's machine model.

(Slightly later) Sorry, I've just seen your

> However, leaving theoretical issues for a while, Fortran (imperative) was born in 1954, where Lisp (functional) in 1958. Thus, from this point of view, you are right :)

I'd also say that I'm not /completely/ hostile to this sort of thing, and I've seen some quite interesting work which used Lisp as a low-level stepping stone to something that more closely resembled what today we'd call a "real" language. But I've studied the history of early programming languages sufficiently to be able to see exactly "where McCarthy was coming from" and what he was trying to do, and while I don't deny that some interesting work has come out of the Lisp camp it was, basically a dead-end approach: in much the same way that intersting concepts came out of Modula-2 while the language itself was a dead end.

In spite of the appearance, we probably have similar ideas. Although the 'basically a dead-end approach' statement about Lisp seems too severe to me.


« Last Edit: January 19, 2020, 12:13:02 pm by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: functional programming
« Reply #27 on: January 19, 2020, 01:12:52 pm »
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
The video explains quite well what functional programming is, but the Java example is horrific.  Beside I don't understand why people use Java or other C-style languages to explain algorithms, you should use a language you know. But even fixing the bugs, the implementation of the SUM function is wrong in 3 or more ways.

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 ;)

I think it can be done better (I mean, more functional):
Code: Pascal  [Select][+][-]
  1. { FIXED: as PascalDragon noticed I used HIGH and LOW in the wrong way. }
  2. FUNCTION Sum (CONST Values: ARRAY OF INTEGER): INTEGER;
  3. BEGIN
  4.   IF Length (Values) = 1 THEN
  5.     RESULT := Values[LOW (Values)]
  6.   ELSE
  7.     RESULT := Sum (Slice (Values, Length (Values) - 1) + Values[HIGH (Values)]
  8. END;
  9.  

[side note] I think the RTL needs the SliceR function, same than Slice but using the right side.
« Last Edit: January 21, 2020, 10:41:51 pm by Ñuño_Martínez »
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

julkas

  • Guest
Re: functional programming
« Reply #28 on: January 19, 2020, 01:18:41 pm »

simone

  • Hero Member
  • *****
  • Posts: 571
Re: functional programming
« Reply #29 on: January 19, 2020, 01:39:15 pm »
I think it can be done better (I mean, more functional):
Code: Pascal  [Select][+][-]
  1. FUNCTION Sum (CONST Values: ARRAY OF INTEGER): INTEGER;
  2. BEGIN
  3.   IF Length (Values) = 1 THEN
  4.     RESULT := LOW (Values)
  5.   ELSE
  6.     RESULT := Sum (Slice (Values, Length (Values) - 1) + HIGH (Values)
  7. END;
  8.  

I agree. Indeed, this is the equivalent in Haskell (from: https://stackoverflow.com/questions/51279298/haskell-sum-up-the-first-n-elements-of-a-list).

Code: Haskell  [Select][+][-]
  1. sumList :: [Int] -> Int
  2. sumList [] = 0
  3. sumList (x:xs) = x + sumList xs
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

 

TinyPortal © 2005-2018