22

With the renewed interest in functional programming languages, I've seen some similitudes between Smalltalk and FPL, namely closures ( BlockClosures in Smalltalk ) Yet, Smalltalk is not a FPL?

What would be needed to consider it as such?

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • 17
    Because Smalltalk is the poster boy for object oriented languages? – CurtainDog Aug 20 '10 at 02:30
  • 6
    Being able to use functions as first-class objects doesn't make a language "functional" any more than being able to write procedures makes a language "procedural". – Gabe Aug 20 '10 at 05:27
  • 4
    @Gabe: Some would argue that this one property alone is sufficient for a language to wear a "functional programming language" tag. – missingfaktor Aug 20 '10 at 05:50
  • 14
    @CurtainDog: OO and functional are not mutually exclusive paradigms. – Frank Shearar Aug 20 '10 at 12:46
  • 2
    @Frank Shearar - Conceptually I would maintain that they are mutually exclusive. Code-wise we're dealing with turing complete languages here, so there's nothing (other than sanity) stopping us from an ad hoc mixture of the two. – CurtainDog Aug 22 '10 at 23:11
  • 5
    @CurtainDog that's a nice academic position to take, but practically it makes no sense: I'm not talking about a paper someone wrote, I'm talking about how Smalltalkers _do actually write_, which is a very functional manner, using objects. Methods that are side effect free, methods/closures as first-class entities (objects, just like Common Lisp, another functional programming language). No surprise, given Smalltalk's heavy Lisp influence. – Frank Shearar Aug 23 '10 at 06:25
  • @CurtainDog: >>"Conceptually I would maintain that they are mutually exclusive."<< No, they are not. Look at Scala for a huge counterexample. – missingfaktor Aug 23 '10 at 08:46
  • @Frank Shearar >> a very functional manner, using objects << Gosh! Darn! When we write sin() in an utterly "functional manner" in VisualBasic does that make VisualBasic a functional programming language? – igouy Aug 23 '10 at 16:21
  • 1
    @Missing Faktor - "[Scala] smoothly integrates features of object-oriented and functional languages" - Doesn't that suggest Scala is designed to be a hybrid? – igouy Aug 23 '10 at 16:23
  • 1
    @igouy Well, if it was natural and idiomatic to write in a functional manner in VB, yes, I'd say it was a functional programming language. – Frank Shearar Aug 23 '10 at 18:39
  • @Frank Shearar - Why evade my straightforward question instead of taking the opportunity to find areas of agreement and disagreement? When you can't even bring yourself to say - No, having a sin function in VB doesn't make VB an FPL - it seems to me that you quarrelling for the sake of quarrelling. – igouy Aug 23 '10 at 22:54
  • 1
    @igouy, I've said a number of times: if a language allows you naturally to program in an FP manner then, in my book, it's an FPL. I have no experience programming in VB, so I can't comment on whether it is or is not an FPL. Does it support higher order functions? closures? Is immutable state the norm, or an afterthought? But while we're about straight answers: how is a method not a function? (My position: it is a function. A lambda bound to a symbol in an object's method dictionary, rather than a lambda bound to a symbol in a package's symbol table.) – Frank Shearar Aug 24 '10 at 10:34
  • @Frank Shearar >> I have no experience programming in VB << Do you understand what a sin function is? – igouy Aug 24 '10 at 13:55
  • 2
    @igouy Yes thanks, I know what a sin function is. Please, do me a favour and read what I added to my edited answer. So in return, please address _my_ questions, instead of trying to turn my arguments into a straw man. – Frank Shearar Aug 24 '10 at 16:20
  • 1
    @Frank Shearar - So why would you need to know anything about VB before you could tell us whether being able to write that function in VB would make VB a functional programming language? (You don't seem to have marked the edits in your edited answer.) – igouy Aug 24 '10 at 20:26
  • 1
    @igouy: Examine the revision history. I summarised arguments for and against Smalltalk being an FPL. So, again: why are "named functions" _not bound to a class_ so important for an FPL, that the presence of closures, HOFs, etc., _don't_ let Smalltalk be regarded as an FPL? – Frank Shearar Aug 24 '10 at 20:37
  • @Frank Shearar >> why are "named functions" not bound to a class so important for an FPL << I'm curious, did you look at "Synthesizing Object-Oriented and Functional Design to Promote Re-Use"? – igouy Aug 25 '10 at 01:05
  • Just happened to trip over the following link and thought it might prove interesting in the context of this discussion: [Functional Programming in Smalltalk](http://onsmalltalk.com/functional-programming-in-smalltalk). Best of luck. – Bob Jarvis - Слава Україні Jan 03 '16 at 19:54

9 Answers9

28

There's no accepted definition of functional programming language.

If you define functional language as the language that supports first class functions, then yes, Smalltalk *is* a functional language.

If you also consider the factors like support for immutability, algebraic data types, pattern matching, partial application etc then no, Smalltalk *is not* a functional language.


I'd encourage you to read the following related blog posts (and also the comments below them):

missingfaktor
  • 90,905
  • 62
  • 285
  • 365
  • 3
    Indeed, Smalltalk is an almost functional language. There are extensions to Smalltalk that provide functional pattern matching, e.g. http://map.squeak.org/package/3772b420-ba02-4fbd-ae30-8eadfc323b7b. Furthermore, Newspeak (http://newspeaklanguage.org/) is a programming language in the tradition of Self and Smalltalk that supports immutability as one of its core concepts. – Lukas Renggli Aug 20 '10 at 11:44
  • 1
    Doesn't that definition make C#, Javascript and pretty much every language from this century functional? – Diego Mijelshon Aug 20 '10 at 14:04
  • 1
    @Diego: As I said, there's no accepted definition of the term "functional programming language". So, by first definition, yes. By second definition, no. – missingfaktor Aug 20 '10 at 14:16
  • 1
    @Missing Faktor - Does Smalltalk even provide a way to declare named functions? – igouy Aug 21 '10 at 17:06
  • @igouy: I might be wrong but isn't `func` in this example a named function? – missingfaktor Aug 21 '10 at 19:50
  • @Missing Faktor - What example? – igouy Aug 21 '10 at 21:11
  • @iguoy: I had included a link in my comment. It seems StackOverflow ate it. Anyway here it is again: http://ideone.com/NoB1C – missingfaktor Aug 22 '10 at 04:22
  • @iguoy: A better example: http://ideone.com/4NNOv (Thanks to [@SergeStinckwich on Twitter](http://twitter.com/SergeStinckwich/status/21822876187)). – missingfaktor Aug 22 '10 at 11:20
  • @Missing Faktor - comment fields don't provide enough flexibility, so my answer is now edited into my reply to OscarRyz. – igouy Aug 22 '10 at 16:27
  • Re partial evaluation: see http://blog.3plus4.org/2007/03/23/currying-in-smalltalk/ – Frank Shearar May 01 '11 at 14:00
  • Of course there is an accepted definition of what makes a programming language functional. Are functions first class citizens? Yes or no? If yes, then its a functional language. So what does (first class citizen) that mean? Can you return a function as result? Can you have "named" top level functions? Can you pass functions as arguments to methods or other functions? Can you "compose" functions? In other words are functions useable as "values"? If all this is the case, the language is functional. Hence: Smalltalk is not a functional language as the top level construct are CLASSES! – Angel O'Sphere Sep 21 '11 at 12:07
  • @AngelO'Sphere, you are wrong on so many levels. As I said, there is no universally agreed upon (or even widely agreed upon) definition of the term functional language. Just because you like some definition better does not make it a standard definition. – missingfaktor Oct 24 '12 at 20:25
  • 3
    @AngelO'Sphere, I am intimately familiar with what first class functions are. (Scala and Haskell are my primary languages.) Thank you for your gratuitous explanation of the term anyway. Now: Having classes at top level is *orthogonal* to having first class functions. Scala, Smalltalk, and many others have both characteristics. Or are you now going to ammend you definition with "must support top level function definitions"? – missingfaktor Oct 24 '12 at 20:27
  • @missingfactor I don't get about what you are arguing. Your comment started with this sentence: "There's no accepted definition of functional programming language." And I corrected this. Thre is certainly a general accepted definition of "functional languages". Read wikipedia e.g. – Angel O'Sphere Jun 14 '13 at 14:25
24

Smalltalk might not be a "pure functional language" (whatever that might be). However, natural use of Smalltalk often results in functional code. (What I guess the Scala people would call "object-functional".)

For example, Squeak's Point class has a functional API: methods like 1@1 translateBy: 1@1 return new Points with the new state, instead of mutating internal state. (This makes Point practically immutable, since the only way to change the internal state of the object is through reflection mechanisms like #instVarAt:.)

It's quite normal to use higher order functions in Smalltalk like maps, filters, folds, and the like. Given that these are baked into the Collections API, it's often easier to write code that uses Collections in a functional way than not.

So many people have so many definitions of "functional programming language" that the question "is Foo a FPL" is as useless a question to ask as "is Foo an object oriented language".

Having said that, here's my two cents: a functional programming language is a language in which it is natural and idiomatic to employ functional techniques: first-class functions, avoidance of mutable state, side-effect free functions, higher-order functions.

By that description, Smalltalk is a functional programming language. It calls first-order functions "methods" or "blocks" depending on whether or they're named or anonymous. Objects store state in instance variables. Higher order functions are simply methods that take blocks as parameters.

Having said that, yes, you can write Smalltalk in a non-functional manner. It does allow for mutable state. Does that stop Smalltalk from being called a functional language? If so, neither are these languages functional: Common Lisp, Erlang (shared state through the process dictionary, ets/dets).

So, in brief, Smalltalk is an FPL because:

  • Functions are first-class entities (objects, in Smalltalk - CompiledMethods or BlockClosures, to be precise).
  • Smalltalk supports closures (it calls them blocks).
  • Smalltalk allows one to write functionally in a natural, idiomatic manner.

and Smalltalk is not an FPL because:

  • You as programmer must ensure that your data structures (objects) are immutable (by having setters/mutators return copies of the object with the mutated state).
  • You as programmer must ensure that your methods (functions) are side-effect free.

(Some Smalltalks do apparently support immutable objects. My experience is limited to Squeak, which does not support VM-level immutability.)

Edit: I don't understand igouy's need for named function definitions other than through defining methods on an object. But anyway, here we go:

Smalltalk at: #func put: [:x | x + 1].
func value: 1.
Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
  • >> Smalltalk might not be a "pure functional language" (whatever that might be) << Is Smalltalk even an impure functional language? – igouy Aug 22 '10 at 17:22
  • 3
    In Smalltalk the program is organized into objects but in a functional programming language the program is organized into functions. There's a well understood and fundamental difference in how we structure programs in OOP and FP. For an example, see "Synthesizing Object-Oriented and Functional Design to Promote Re-Use" http://www.cs.brown.edu/~sk/Publications/Papers/Published/kff-synth-fp-oo/ – igouy Aug 22 '10 at 17:28
  • >> Squeak's Point class has a functional API << You say the behaviour is provided by accessing instances of Point class, not defined in a family of functions. – igouy Aug 22 '10 at 17:41
  • 2
    @igouy: "Organised into objects" is not an argument against Smalltalk not being a functional language. See Common Lisp (and CLOS in particular). You say "object" and I say "a set of functions that work on the same kind of struct, in their own namespace." – Frank Shearar Aug 22 '10 at 19:01
  • 1
    @iguoy: >> Squeak's Point class has a functional API << Did you read the class? Behaviour is provided by functional methods (i.e., no side effects, no mutable state). It's defined in a family of functions that we happen to call methods. – Frank Shearar Aug 22 '10 at 19:04
  • >> "Organised into objects" is not an argument against Smalltalk not being a functional language << Smalltalk is not organised into functions - that is an argument against Smalltalk being a functional language. – igouy Aug 22 '10 at 22:59
  • >> Smalltalk at: #func put: [:x | x + 1]. << Is that in fact how Smalltalk programs are written? Is that in fact how the Smalltalk libraries are written? – igouy Aug 22 '10 at 23:24
  • Did you read "Synthesizing Object-Oriented and Functional Design to Promote Re-Use" ? – igouy Aug 22 '10 at 23:27
  • Did you read the Point class? I wish I could find that paper I read recently that described Scheme as the first proper object-oriented language other than Simula-67. Yes, Scheme. Just like Erlang's a functional programming language and an OO language. (Just ask Joe Armstrong.) – Frank Shearar Aug 23 '10 at 06:27
  • Years ago I had a brief email discusson with Joe Armstrong about his article "Why OO Sucks" - trying to understand his point of view and clear up what seemed to me misunderstandings. "Why OO Sucks" is still cached by Google - http://webcache.googleusercontent.com/search?q=cache:M-JWFee4i6MJ:www.sics.se/~joe/bluetail/vol1/v1_oo.html+joe+armstrong+oo – igouy Aug 23 '10 at 15:23
  • >> Did you read the Point class? << What revelation do you expect me to experience on seeing methods that return values rather than change state? – igouy Aug 23 '10 at 15:33
  • Yes, and Armstrong changed his mind: "Erlang may be the ONLY object oriented language." - http://www.infoq.com/interviews/johnson-armstrong-oop (And we're straying off-topic now) – Frank Shearar Aug 23 '10 at 18:35
  • @igouy re Point class: that you have a bunch of named, side-effect functions. That you have a functional API for an object. That, in fact, OO and FP overlap. Really, I expect _you_ to know this. – Frank Shearar Aug 23 '10 at 18:40
  • @Frank Shearar - my guess is that the FORTRAN I used in 1975 allowed me to declare side effect free functions - Do you think that makes FORTRAN a functional programming language? – igouy Aug 23 '10 at 23:01
  • @igouy, you haven't defined what your idea of an FPL is. Mine is simple: if your language naturally leads you to write in an FP style, then it's an FPL. Not "oh you can fake it by passing void * around", but "oh, it's idiomatic." – Frank Shearar Aug 24 '10 at 05:59
  • @Frank Shearar - So please say whether being able to declare side effect free functions in FORTRAN is enough in itself, according to your simple idea, to make FORTRAN an FPL? – igouy Aug 24 '10 at 14:02
  • You're twisting my words. At no time have I claimed this. I said that IF FORTRAN supported ALL OF: first class functions, closures, support for immutable state, NATURAL AND IDIOMATIC USE OF FP, THEN I would call FORTRAN an FPL. I don't speak FORTRAN, so tell me: does FORTRAN support all these things? Does it make writing in FP easy? – Frank Shearar Aug 24 '10 at 16:12
  • @Frank Shearar >> You're twisting my words. At no time have I claimed this. << And I haven't said you did, I have asked a very very simple question - please say whether being able to declare side effect free functions in FORTRAN is enough in itself, according to your simple idea, to make FORTRAN an FPL? – igouy Aug 24 '10 at 20:11
  • No. Simply having side-effect free functions is _not_ sufficient to make an FPL. (Counter-example: C.) So what do you regard as the sufficient conditions for an FPL? – Frank Shearar Aug 24 '10 at 20:19
  • Given that - having side-effect free functions is not sufficient to make an FPL - if for sake of argument we ignore the fact that the code is object orientated and say that the methods on Squeak's Point class are functions, then would that be sufficient to make Smalltalk an FPL? – igouy Aug 25 '10 at 00:24
  • Clearly not - you can do this in C. What I don't understand is why you aren't attacking the "Smalltalk is an FPL" position from a strong angle - if you said "Hey, it doesn't support immutable data. You have to ensure immutability through convention, and Smalltalk doesn't even warn you that something has side effects, like in Common Lisp where you at least see nconc or in Scheme you see set! then I could see your point. What I fail to see is how organising functions/methods in a class, versus organising them in a module/package, makes any difference to anything. – Frank Shearar Aug 25 '10 at 06:06
  • @Frank Shearar >> Clearly not - you can do this in C. << So you understand that your previous repeated references to Squeak's Point class were irrelevant to the point of being bogus. – igouy Aug 25 '10 at 06:14
  • @Frank Shearar >> What I fail to see is how organising functions/methods... << I'm curious, did you look at "Synthesizing Object-Oriented and Functional Design to Promote Re-Use"? – igouy Aug 25 '10 at 06:15
  • @igouy Yes, it's an interesting, neat paper. And? It doesn't explain why you think that (a) a method is not a function and (b) how grouping these methods/functions in something called a class is somehow less FP than grouping them in a package or module. What the paper does illustrate is how Visitor is a hack in both the complimentary and pejorative senses of the word. – Frank Shearar Aug 25 '10 at 19:48
  • @igouy re Point: I was trying to address the _real_ issue about Smalltalk being/not being an FPL, which is management of state. So please, attack that, because that's actually a valid argument. Saying "Smalltalk's not an FPL because it lacks functions" is nonsense. I am starting to think Smalltalk's not an FPL because you, the programmer, have to supply the immutability, and that's error prone. _State_, not "methods aren't functions". – Frank Shearar Aug 25 '10 at 19:49
  • @Frank Shearar >> the immutability << 2 days ago you said - "Yes, you CAN write mutable state stuff in Smalltalk. You can in Common Lisp too, and that's commonly regarded as a functional programming language." And some purely functional languages provide for destructive transformation of state so perhaps the phrase you're looking for is referential transparency. – igouy Aug 25 '10 at 21:35
  • @Frank Shearar >> ... an interesting, neat paper ... It doesn't explain why you think that ... << Does it state a difference between what functional and object-oriented enable? – igouy Aug 25 '10 at 21:41
  • @Frank Shearar >> how grouping these methods/functions in something called a class is somehow less FP than grouping them in a package or module << For that question FP is a distraction, it's mostly "Object-Oriented Programming Versus Abstract Data Types" 1990 pdf http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBIQFjAA&url=http%3A%2F%2Fwww.cs.utexas.edu%2Fusers%2Fwcook%2Fpapers%2FOOPvsADT%2FCookOOPvsADT90.pdf&ei=vZh1TIC7KZH_4AbXzNyWBg&usg=AFQjCNETvaR2PIoD7lFaMvS8GJJFlrfNnw – igouy Aug 25 '10 at 22:43
  • @igouy thanks for the reference. I'm busy working my way through it. – Frank Shearar Aug 27 '10 at 07:45
  • @Frank Shearar - Oh! you're reading it! :-) You might want to skip forward 20 years to "On Understanding Data Abstraction, Revisited" (pdf) http://www.cs.utexas.edu/users/wcook/Drafts/2009/essay.pdf – igouy Aug 27 '10 at 23:55
  • @igouy: :P Yes, and that paper's on my to-read list as well, when Steele referenced it recently (http://projectfortress.sun.com/Projects/Community/blog/ObjectOrientedTailRecursion). (This is when you say "well, if you had, we wouldn't be arguing"? :) ) – Frank Shearar Aug 28 '10 at 11:41
  • @igouy I finally found that Scheme/OO paper I'd been trying to remember: http://www.ccs.neu.edu/home/matthias/Presentations/ecoop2004.pdf At any rate, I have a lot more reading to do before I can continue the discussion. – Frank Shearar Aug 30 '10 at 08:12
  • @igouy I don't see how Cook says that Smalltalk's not an FPL. I see that ADT != object, and he says that either may be written in a functional or imperative manner. Two independent axes! At this point, I don't think we're ever going to find common ground, so I'll bow out with the kernel of my claim: "However, natural use of Smalltalk often results in functional code." and leave it at that. – Frank Shearar Sep 03 '10 at 12:19
  • @Frank Shearar - To which the chorus responds, natural use of Smalltalk often does not result in "functional code". – igouy Sep 03 '10 at 22:20
  • @Frank Shear: '"Organised into objects" is not an argument against Smalltalk not being a functional language.' Of course it is! A block is not a function! Being unable to have a named function on top level (just like a class) makes it "non functional"! Having immutable objects does not make it functional! Your nicely upvoted answer is unfortunately simply wrong. – Angel O'Sphere Sep 21 '11 at 12:12
  • Of course a block is a function. To be precise, it's a closure. There's no difference _at all_ between foo(self, 1) and self.foo(1). Please read William Cook's _On Understanding Data Abstraction, Revisited_, and Van Roy's _Programming Paradigms for Dummies_. They clearly demonstrate the difference between imperative and functional on one hand, and abstract data types and objects on the other. Orthogonal axes! – Frank Shearar Sep 21 '11 at 13:07
13

Programming with the Object Oriented paradigm is creating a program by identifying and modeling the Problem Domain Entities as objects, and then make them collaborate between themselves to solve each problem instance. Programming with the Functional paradigm is modeling the problem as a mathematical problem, and creating a mathematical function (by composing other functions) that for each problem instance, computes the problem solution.

In my opinion, a functional programming language is a language that given a solution for a problem solved using the functional programming paradigm, the language can express that solution exactly as it was thought. If you need to "transform" some part of your solution so it fits in what the language can express, then it doesn't fully support the paradigm you used to think the solution.

Smalltalk can express in most cases all the solutions created using the Object Oriented Programming paradigm, but it cannot expresss primitively a lot of solutions created using the Functional Programming paradigm. That's why I don't consider it a FPL. Despite not primitively be able to express every solution that a FPL can, Smalltalk is extremelly extensible, and you can extend it to be able to express all the solutions a FPL can.

  • 3
    Can you name some examples of FPL things that Smalltalk can't express "primitively"? – Frank Shearar Aug 22 '10 at 15:04
  • 5
    Even though this is an old thread, I take issue with the definition of OO presented here. OO is merely expressing a program in the form of entities exchanging messages and encapsulating state; "modeling" the problem domain directly as objects doesn't enter the definition but rather - IMO - is a tool or even a trick. In that sense, a functional program can be seen as entities encapsulating zero state and interacting through very simple messages ("apply"). This would make the functional model a subset of OO. – cdegroot Aug 10 '13 at 16:35
7

Smalltalk is a purely Object Oriented Language, where almost all code is basically objects interchanging messages. Functional programming is oriented to function calls, and compositions between functions to create more complex behavior (avoiding state for data, as opposed to internal states that objects have in any Object Oriented language).

Manuel Araoz
  • 15,962
  • 24
  • 71
  • 95
  • 3
    Well, "almost all" is anything other than expressions like "1" or "a := b". Also, composition of functions is exactly what I mean by "smalltalkers naturally write functionally in Smalltalk". Internal states (collections of instance variables) are identical to externally defined structs. Like a CONS cell, say. Or a list. Or a hashmap. (Important proviso: you've actually written the methods on your classes in a functional way, like the Point class example I give.) – Frank Shearar Aug 22 '10 at 19:07
  • >> Internal states (collections of instance variables) are identical to externally defined structs. << Except for the ways they are not, for example - their accessibility. – igouy Aug 22 '10 at 23:17
  • Er. Yes, you CAN write mutable state stuff in Smalltalk. You can in Common Lisp too, and that's commonly regarded as a functional programming language. So perhaps your definition of an FPL is "a language in which you can ONLY write functionally"? That rules out a whole bunch of languages that most people regard as functional: Common Lisp, Scheme, Erlang, ... – Frank Shearar Aug 23 '10 at 06:28
  • @Frank Shearar - I didn't mention mutable state so presumably your comment is directed to Manuel? – igouy Aug 23 '10 at 16:51
  • @igouy sorry, not my clearest of comments. "mutable state" was directed at Manuel, but "your definition of an FPL" was directed to you. – Frank Shearar Aug 23 '10 at 20:03
5

Purely functional languages usually have immutable variables, making them more like the variables in a mathematical sense.

Matthew Schinckel
  • 35,041
  • 6
  • 86
  • 121
  • What's immutability of a variable in a mathematical sense? For example, in the equation x^2 - 1 = 0 is x immutable? Does the iteration through all possible values of x ({-1, 1}) make it mutable? For example, the following code in Scala: val results: Set[Double] = solve("x^2 - 1 = 0") returns all possible values of x to results, but the function 'solve' CAN do some mutations of an internal variable, so in Smalltalk any instance can mutate its internal state to elaborate the answer. Anybody could also write a code responding with multiple results in one object in Smalltalk. – Dmitry Ovchinnikov Feb 08 '21 at 15:46
5

A language does not become a functional language by having named functions - by that definition, C would be functional ! More important is the functional semantics in the mathematical sense, that the result depends on the arguments alone (in particular: no side effects). By this definition, objects which can be modified by setters are contrary to a functional programming style. However, as already pointed out, even objects can be used in a functional style (Rectangle example) if you forbid side effects. And, btw. there is a duality between objects with methods and a set of functions which are defined in a closure over a private state (see SICP for details). They can simulate each other mutually:

(def (make_foo initVal1 ... initValN)
   (let ((instVar1 initVal1) ... (instVarN initValN))
      (define (method1 args) ...)
      ...
      (define (methodN args) ...)
      (define (lookup selector)
          (cond ((eq? selector 'method1) method1)
             ...
             ((eq? selector 'methodN) methodN)
             (true doesNotUnderstandCode)))
      lookup)) ; returns the lookup function (provides "access into the closure") !

(defMacro (method1 receiver.args)
    ((receiver selector) args))

(define foo (make_foo 1 2 ...))
(method1 foo ...)

the above is a simulation of "pure" functional objects, and semantically the same as a lot of Smalltalk code! However, to implement a setter-method, you have to add a method which does a (set! instVar newValue). And, because set! is non-functional, breaks the "functionality" of the scheme.

Summary: look at the semantics, not the source, luke !

blabla999
  • 3,130
  • 22
  • 24
  • 1
    You can have setters, as long as you have your setters return a new object with the required state rather than mutating the receiver. – Frank Shearar Aug 23 '10 at 15:02
  • >> there is a duality between objects with methods and a set of functions << Yes, they are duals - they are not the same thing. – igouy Aug 23 '10 at 16:44
  • >> A language does not become a functional language by having named functions << Agreed, but when a language does not have named functions why would we think that language was trying to be a functional programming language? – igouy Aug 23 '10 at 16:46
  • 1
    Smalltalk does have named functions. They're called methods. They're lambdas bound to a symbol in a dictionary local to a class. You haven't explained how that's any difference to lambdas bound to symbols in, say, a package. – Frank Shearar Aug 23 '10 at 18:41
  • @Frank Shearar >> You haven't explained how that's any difference to... << What have I repeatedly said is different? http://www.cs.brown.edu/~sk/Publications/Papers/Published/kff-synth-fp-oo/ – igouy Aug 23 '10 at 23:27
  • 3
    cool down: lets try to define some common base we all agree upon: 1) functional style is using side-effect free functions; 2) a functional language is one where the functional style is the most natural or preferred way of doing things. This makes lisp a functional language, although it supports non functional operations (set!, for example) and objects (clos or similar). This also makes Smalltalk a non-functional language, although it supports all that functional stuff as well. 3) you can program oo-style in lisp and also functional style in smalltalk – blabla999 Aug 24 '10 at 08:41
  • @blabla999, according to you, Prolog is a functional language. For example, x(Y) :- some_func(56, 78, L), some_func2(L, 98, M), some_func3(M, 345, Y). – Dmitry Ovchinnikov Feb 08 '21 at 15:53
  • @blabla999, On the contrary, in Scala the immutability can be guaranteed only by implementor. You cannot say (without looking at code or scaladocs) the given func is side-effect free or not. Moreover, anybody CAN write programs in a functional style in Smalltalk: to use fresh instances everywhere, to pass fresh instances of objects, to not share objects, etc. I want to say that Smalltalk can model the functional behaviour and nothing prevents anybody from writing programs in a functional style in Smalltalk. – Dmitry Ovchinnikov Feb 08 '21 at 16:08
2

Thank you all for all the answers.

I'll add mine here which basically is my ( probably miss) understanding of yours.

I think the criteria to call something OO or FP is they way "stuff" is built using the language; not how easy or hard is to do it, I mean, the mental paradigm used.

For instance, as the links shown, it may be harder to type something in Scala than in OCaml, but that doesn't make it less FPL ( maybe incomplete or impure, but definitely functional ); because the aim of Scala is to use functions as primary building blocks, rather than objects.

On the other hand, making easy to write a function in a language doesn't make it functional, if the style or the aim is to use another artifacts. Smalltalk for instance, make it very easy to write an anonymous block or to provide a pluggable sorting comparator, but that doesn't make it ANY Functional at all, because the focus is to use objects and pass messages. Blockclosures are just a mechanism to encode these messages ( not functions ) even if they look like just like they were functions.

The confusion comes, because OO and FP are orthogonal ( as Rahul stated via twitter ) , so, what it looks like a encoded message in Smalltalk, looks pretty much like an anonymous function in Scala. But doing something similar doesn't converts a language of one paradigm into another.

To make it worst, Scala also uses the OO paradigm, to make it easier for mainstream ( Java, C++, C# ) programmers to give the jump, and if you see, it have had way more success than any of the others FPL's did. And of course this was thank to Java ( IMHO if Clojure has any success will be for exactly the same reason, the JVM )

In conclusion: A programming language may be classified by the paradigm it uses to build "things". There are languages that are pure like [Smalltalk : OO, Haskell: Functional, C: Procedural], or hybrids, like Scala or multi paradigm , like C++, Ruby, Python.

The fact you can write one style with the other language, doesn't make it that language of that style. Like simulating Objects with Lisp doesn't make it OO nor using functions with Java make it Functional.

To answer the question, to be considered functional, Smalltalk would need to use functions as building blocks, which it doesn't because it uses objects.

Community
  • 1
  • 1
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • Your last line pretty much says it. Playing with a language designed to be multi paradigm (I don't think that would be Ruby and maybe not Python and C++ is too big) gives a good feel for what it means for a language to have a dominant style. I found it very interesting playing with Mozart/Oz because reworking a program from one style to another is very interactive - you can start off with OOP rework it into an imperative style, iteratively take out the destructive assignment reworking into a declarative style, and then go crazy with concurrent logic programming. – igouy Aug 25 '10 at 00:45
1

I've seen some similitudes between Smalltalk and FPL, namely closures

There's a more basic similarity - every Smalltalk message always returns a value.

But now look at the Design Principles Behind Smalltalk

[Smalltalk] sends the name of the desired operation, along with any arguments, as a message to the number, with the understanding that the receiver knows best how to carry out the desired operation.

Does that describe what you think of as Functional Programming?

@Frank Shearar - That describes Erlang too. Is Erlang now non-functional?

Erlang is something of a chimera - sequential programming is based on functions, but concurrent programming is based on message passing between processes. So Erlang is not multi paradigm or hybrid in the way that other languages allow different styles to be used to achieve the same purpose - it uses different styles for different purposes.

igouy
  • 2,547
  • 17
  • 16
  • That describes Erlang too. Is Erlang now non-functional? – Frank Shearar Aug 23 '10 at 18:42
  • My previous comment reads a bit on the snarky side. I don't mean to be (too) snarky. The above also describes late binding, which I don't see as being incompatible with FP. – Frank Shearar Aug 23 '10 at 18:53
  • @Frank Shearar >> Is Erlang now non-functional? << Haven't you already quoted Joe Armstrong saying "I might think, though I'm not quite sure if I believe this or not, but Erlang might be the only object oriented language"? – igouy Aug 23 '10 at 23:11
  • @igouy: so it's your position that a language can EITHER be an FPL OR an OOPL, but never both? – Frank Shearar Aug 24 '10 at 06:18
  • @jgouy I miss your point. I'm stubborn as a mule, or do you mean the MUltiLingual Extension to Emacs might teach me something? – Frank Shearar Aug 25 '10 at 06:08
  • @Frank Shearar - I'll humour you - A hybrid between a Horse and a Donkey isn't a Horse and it isn't a Donkey, it's a Mule. – igouy Aug 25 '10 at 06:17
  • @igouy Thanks for clarifying. And I'm glad to see you already answered my next question, re Erlang :) I think in Erlang's case there's a neat split between OO and FP based on scale: within a process, you have a purely functional paradigm, while the communication between processes is probably best thought of in the Kay-style object oriented paradigm. (Kay's often said that people missed the point of Smalltalk - that the gem was message passing, not objects: http://lists.squeakfoundation.org/pipermail/squeak-dev/1998-October/017019.html) – Frank Shearar Aug 25 '10 at 07:54
  • @Frank Shearar - And a creature with a lion's head, a goat's body and a serpent's tail - isn't a lion, and isn't a goat and isn't a serpent - it's a chimera. – igouy Aug 25 '10 at 14:09
0

What would be needed to consider it as such?

  • a way to declare functions rather than a way to declare methods in a class

  • a way to structure a program around functions rather than around objects

@Missing Faktor - but isn't func in this example a named function?

|func v|
func := [:x | x + 1].
v := func value: 5.

No func is a local variable. You've bound an anonymous function to the local variable func.

|func v|
func := [:x | x + 1].
func := 2.
v := func value: 5.

To see the difference, look at this Erlang example which shows both an anonymous function and a named function.

igouy
  • 2,547
  • 17
  • 16
  • igouy, Common Lisp's defun binds an anonymous function to a symbol in a symbol table. AFAIK, Scheme's define does exactly the same. So what's your point? – Frank Shearar Aug 22 '10 at 19:23
  • 1
    It can only be a matter of moments before you reach for Turing equivalence. My point it's silly to talk about a language being a functional programming language when it doesn't provide named functions. Just like it would be silly to talk about C being an OO programming language when it doesn't provide objects. – igouy Aug 22 '10 at 23:14
  • 1
    There ARE named functions in Smalltalk, only they're called methods. Please explain how a method is NOT a named function. Yes, a method accesses state. In Point's case, those are the unreachable, immutable, instance variables. Just like any FPL you care to mention has state, in the form of ADTs or whatever. – Frank Shearar Aug 23 '10 at 06:31
  • 1
    @Frank Shearar - There's a well understood and fundamental difference in how we structure programs in OOP and FP - they are duals. – igouy Aug 23 '10 at 17:47
  • Well, I call that duality/dualism a fundamental _sameness_. Maybe that's why we're arguing. – Frank Shearar Aug 23 '10 at 19:23
  • @Frank Shearar >> Maybe that's why we're arguing << Mostly you seem to be quarrelling by focusing on the similarities and ignoring the blatant differences. – igouy Aug 23 '10 at 23:14
  • 2
    As you no doubt know from hands-on experience, working in Smalltalk is as much about figuring out commonality of actions (verbs, functions, methods) as it is about pulling out structure (defining structs, tuples, classes). FP emphasises verbs over nouns where OO emphasises nouns over verbs. I see no reason why a language cannot support both paradigms, and I assert that Smalltalkers do indeed practice both OO and FP when writing. Your definition of an FPL above includes C, which I do not regard as either an OOPL or an FPL. – Frank Shearar Aug 24 '10 at 06:22
  • 2
    An hour before this you wrote - "@igouy, you haven't defined what your idea of an FPL is..." - but now you say - "Your definition of an FPL above..." – igouy Aug 24 '10 at 14:08
  • @igouy C++ is just C in drag, ANYTHING that can be written in C++ can be written in C, OTOH C++ is not a particularly good example of an OO language. – Jasen Feb 06 '13 at 02:36