How do you develop a functional program?

From the first day of my programming career, I started with object-oriented programming. However, I am interested in exploring other paradigms (something that I talked about here on SO several times is good, but I did not have time to do). I think that I am not only ready, but also getting time, so I will start functional programming with F #.

However, I'm not sure how to structure much less design applications. I use the ideas one-class-per-file and class-nouns / verb functions in OO programming. How do you develop and structure functional applications?

+52
design functional-programming f #
Oct 10 '08 at 16:15
source share
8 answers

Read the SICP .

In addition, there is a PDF version .

+19
Oct 10 '08 at 16:21
source share

You might want to check out my recent blog post: How does functional programming affect the structure of your code?

At a high level, the OO design methodology is still very useful for structuring the F # program, but you will discover this disruption (more exceptions to the rule) when you move to lower levels. At the physical level, “one class for each file” will not work in all cases, since mutually recursive types must be defined in one file ( type Class1 = ... and Class2 = ...), and part of your code may be in "free" functions that are not related to a particular class (this is what suits the F # module). Restrictions on the order of files in F # will also make you critical of the dependencies between the types of your program; this is a double-edged sword, as it may take more work / think to unravel high-level dependencies but it will give programs that are organized in such a way that they are always accessible (since the most primitive objects are always in the first place, and you can always read the program from top to bottom and introduce new things one by one, rather than just starting to look for a catalog with code files and don’t know where to start. "

+13
Oct 10 '08 at 19:27
source share

How to create programs is all about it (on a lean length, using Scheme instead of F #, but the principles carry over). In short, your code reflects your data types; this idea goes back to the old-fashioned "structured programming", only functional programming is more explicit about it and with more convenient data types.

+6
Oct 11 '08 at 7:48
source share

Since modern functional languages ​​(i.e., not lisps) by default use early bound polymorphic functions (efficiently), and that object orientation is just a special way of organizing polymorphic functions, this is not very different if you know how to correctly create encapsulated classes .

Lisps use late binding to achieve a similar effect. Honestly, there is not much difference, except that you do not explicitly declare the type structure.

If you programmed extensively using the functions of the C ++ template, you probably already have an idea.

In any case, the answer is small “classes”, and instead of changing the internal state, you need to return a new version with a different state.

+5
Oct. 10 '08 at 16:20
source share

F # provides the usual OO approaches for large-scale structured programming (e.g. interfaces) and does not attempt to provide experimental approaches first used in languages ​​such as OCaml (e.g. functors).

Consequently, the large-scale structuring of F # programs is essentially the same as in C # programs.

+2
Oct 19 '08 at 2:25
source share

Functional programming is a definite paradigm. Perhaps the easiest way to wrap your head around it is to insist that the design be laid out using a flowchart. Each function is different, not inherited, does not differ from polymorphism. Data is transferred from function to function to delete, update, insert, and create new data.

+1
Oct 10 '08 at 16:33
source share

About structuring functional programs:

While OO languages ​​structure code with classes, functional languages ​​structure it with modules. Objects contain state and methods, modules contain data types and functions. In both cases, structural units group data types together with the corresponding behavior. Both paradigms have tools to create and provide barriers to abstraction.

I would highly recommend choosing a functional programming language convenient for you (F #, OCaml, Haskell or Scheme) and looking at the structure of its standard library for a long time.

Compare, for example, the OCaml Stack module with System.Collections.Generic.Stack from .NET or a similar collection in Java.

+1
Sep 15 '09 at 17:44
source share

It's all about pure functions and how to create them to create great abstractions. This is actually a complex problem, which requires a reliable mathematical background . Fortunately, there are several models with deep formal and practical research. On Functional and Reactive Domain Modeling, Debasish Ghosh explores this topic further and combines several practical scenarios using pure functional templates:

Functional and reactive domain modeling teaches you to think of a domain model in terms of pure functions and how to construct them to build large abstractions. You will start with the basics of functional programming and gradually move on to advanced concepts and templates that you need to know to implement complex domain models. The book shows how advanced FP templates such as algebraic data types, glass-based design, and side effect isolation can make your model readable and verifiable.

-one
Jul 04 '16 at 11:14
source share



All Articles