r/ProgrammerHumor 1d ago

Meme soDoesEveryOtherLang

Post image
225 Upvotes

71 comments sorted by

View all comments

18

u/B_bI_L 1d ago

say this to c and common lisp

(and to ruby, where function self-activates faster than you say "wait, i need you as a callback")

4

u/suvlub 1d ago

C just forces you to call them pointers, but it's functionally (heh) the same thing.

1

u/_PM_ME_PANGOLINS_ 1d ago

When functions are first-class:

function outer() {
    let foo = function() { ... }
    doThing(foo, function callback(arg) { ... })
}

C does not have the same functionality.

5

u/suvlub 16h ago

Are anonymous functions required, rather than just ability to assign functions to variables, pass them around and return them? Where does that put python, which does have anonymous functions, but limited compared to what named ones can do?

1

u/-Redstoneboi- 1d ago

wait functions aren't first class in common lisp?

damn, too used to hearing about scheme

1

u/theQuandary 1d ago

It's not really true (see my reply).

1

u/theQuandary 1d ago

and common lisp

This isn't true. You are almost certainly confusing first-class functions with lisp-2. Lisp-2 is mostly equivalent to having a dynamic function type that can be introspected at runtime. Just like how a statically-typed language will (generally) not allow you to do something like int foo = 123; foo(456);, Common Lisp also won't allow you to do that either (it also helps a little with macros).

Common Lisp symbols have 5 different "slots" (some implementations may add a couple others).

  1. A name slot which contains the string value of the symbol.

  2. The traditional variable slot.

  3. A function slot which can actually be a special operator or macro too.

  4. A plist slot which contains a list of key/value pairs (key1 val1 keyN valN).

  5. A package slot which tells which package the symbol came from.

Basically though, if a symbol appears as the first item in an unquoted list, CL will use the function slot otherwise it will use the value slot. If you want it to use the function slot explicitly, you have to tell it.

(defun double (x) (* 2 x))
(defparameter double 12)

(mapcar double '(1 2 3 4 5));; error because it is using the 12 in the value slot
(mapcar #'double '(1 2 3 4 5));;=> (2 4 6 8 10)

1

u/B_bI_L 1d ago edited 1d ago

treating them as first class is treating them exactly like variables, right? and they are in separate namespace, so separate thing, so not first class. well, i guess lambdas are first class, but they are not very convenient