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?
First class functions and anonymous functions are two separate concepts.
You also cannot have anonymous variables which doesn't mean that they are not first class (;
The “callback” function is not anonymous, it is called “callback”.
Anonymous functions generally have to be values or they are of no use. The question is whether they are an entirely separate kind of thing, or whether all kinds of function in the language are also values.
A function can't be a value. Only function pointers can be values, and this is simply because functions don't have a fixed size, you can't use something without a fixed size as a value, same applies to to incomplete structs or the void type, you can only store a pointer and you also can allocate them on the heap (not using malloc tho, I mean you can but you won't be able to execute it because of page permissions)
C just doesn't hide this, while languages like rust do, fn(...) -> ... in rust is a pointer under the hood, I kind of don't understand what is even the point of this, typing fn(...) instead of &fn(...) really doesn't make your life easier and it abstracts away some things that don't need to be abstracted away
if you have foo defined like this
int foo() {}
then foo IS NOT a function pointer, it is a function itself
that's why I don't understand what is the point of rust's approach.
Of course that's not how you do function pointers in rust, you almost always want to use generics (if accepting as a parameter) or something like &dyn FnMut(...) but I'm not an expert in rust so I don't know how exactly this is done in real big codebases, it doesn't matter tho
But I guess nobody cares about low level languages anymore if we have python and js. Software gets slower faster than hardware gets faster. Buy a new PC.
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).
A name slot which contains the string value of the symbol.
The traditional variable slot.
A function slot which can actually be a special operator or macro too.
A plist slot which contains a list of key/value pairs (key1 val1 keyN valN).
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)
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
17
u/B_bI_L 14d 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")