5 ms·
> Lisp looks like complete gobbledygook to me. Might that be Clojure, rather than Lisp, that you're looking at, without an extensive functional programming bac
by kazinator 8y ago
> Lisp looks like complete gobbledygook to me.
Might that be Clojure, rather than Lisp, that you're looking at, without an extensive functional programming background?
Some classic Lisp code, too, will look like gobbledygook if you've never had a tutorial, due to unfamiliar symbols. If you don't know what is a "cons cell" or the functions car and cdr, for instance.
Surely, you can guess what this is doing:
(let ((x 3) (y 4))
(print (+ x y)))
- zzzcpan 8y ago> Surely, you can guess what this is doing: Only if you got far enough in your background/education to understand prefix notation. Most people aren't there.
- kazinator 8y agoAll the other languages OP mentioned make a good deal of use of prefix notations. f(x,y) is a prefix notation and so are command languages: "command arg ...". Statements like "while (x) { y }" or "return z" are also a prefix notation, as well as declarations like "struct foo" or "int x, y".
- mcv 8y ago> unfamiliar symbols. If you don't know what is a "cons cell" or the functions car and cdr, for instance. Those short, cryptic names are definitely part of it. It's unfamiliar syntax, and overdose of brackets for added confusion, and in the middle of it all, words that mean nothing to me. Stuff like cout, puts or the printf syntax also take some getting used to if they're new to you, but you get more context with them. And yes, I can guess what your let and print do, but still, the excessive brackets, unusual assignments and prefix notation, give the whole thing a lot of noise.
- kazinator 8y agoBut, on the the bright side, that expression gives you a synopsis of all the syntax you will ever have to know. At least the principal organizing syntax for structuring the bulk of the code. What you don't see there are examples of various minor notations, like various kinds of literals and such. The good news is that parentheses disambiguate everything; if we remove some of them, we have to introduce hidden rules that determine what is a child or sibling of what. let can have fewer parentheses, and some dialects (like Arc, in which HN is written) have tried that: (let (x 1 y 2) ...) I've seen variations on a let1 operator which just binds one variable: (let1 x 1 ...) In Common Lisp, the inner parentheses are there because the initializing values are optional: (let (x y (z 3) w) ...) gives us four variables. x, y and w are initialized to nil; z to 3. Parentheses are needed around the variables so that they form a single argument position in the let syntax. Without that, we don't know where the body of the let begins. So that is to say, if we make it (let (x 1) (y 2) (print x)), how do we know (y 2) isn't supposed to be an invocation of function y on argument 2 rather than another variable binding. The parentheses are great for code layout; there are formatting rules to make all complex expressions look consistently good. The parentheses also facilitate good editor support.
- mcv 8y agoThis actually sounds far more imperative than I imagined Lisp to be. I thought it was mostly a functional language.
- kazinator 8y agoANSI Lisp, and related dialects, including most of its predecessors, are thoroughly imperative. If you have a program design in your head intended for Algol, you can probably implement in Lisp with essentially the same organization. In my own dialect of Lisp, TXR Lisp, to demonstrate/test the foreign function capabilities, I took a C code sample from MSDN (a minimal Win32 program to create a Window) and translated it, almost expression for expression: (deffi-cb wndproc-fn LRESULT (HWND UINT LPARAM WPARAM)) (defun WindowProc (hwnd uMsg wParam lParam) (caseql* uMsg (WM_DESTROY (PostQuitMessage 0) 0) (WM_PAINT (let* ((ps (new PAINTSTRUCT)) (hdc (BeginPaint hwnd ps))) (FillRect hdc ps.rcPaint (cptr-int (succ COLOR_WINDOW) 'HBRUSH)) (EndPaint hwnd ps) 0)) (t (DefWindowProc hwnd uMsg wParam lParam)))) (let* ((hInstance (GetModuleHandle nil)) (wc (new WNDCLASS lpfnWndProc [wndproc-fn WindowProc] hInstance hInstance lpszClassName "Sample Window Class"))) (RegisterClass wc) (let ((hwnd (CreateWindowEx 0 wc.lpszClassName "Learn to Program Windows" WS_OVERLAPPEDWINDOW CW_USEDEFAULT CW_USEDEFAULT CW_USEDEFAULT CW_USEDEFAULT NULL NULL hInstance NULL))) (unless (equal hwnd NULL) (ShowWindow hwnd SW_SHOWDEFAULT) (let ((msg (new MSG))) (while (GetMessage msg NULL 0 0) (TranslateMessage msg) (DispatchMessage msg)))))) TXR Lisp benefits from featuring the referencing dot operator a.b.c.d for accessing structure fields (which is a syntactic sugar for a (qref a b c d) form). This is much more important than having infix math, because the notation supports the expressivity of object-oriented programming, which strikes at the heart of larger scale program organization. The full program with all the boilerplate to define Win32 types and bind to the foreign functions is here: http://nongnu.org/txr/rosetta-solutions-main.html#Window%20creation http://nongnu.org/txr/rosetta-solutions-main.html#Window%20c... The original C is here: https://docs.microsoft.com/en-us/windows/desktop/learnwin32/your-first-windows-program https://docs.microsoft.com/en-us/windows/desktop/learnwin32/...