3 ms·
This actually sounds far more imperative than I imagined Lisp to be. I thought it was mostly a functional language.
by mcv 8y ago
This 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/...
- Jtsummers 8y agoCommon Lisp is a very pragmatic language, complete with goto, if you want it. This is for the purposes of efficiency rather than because you should use it yourself. Suppose you wanted a really efficient numerical library written in Common Lisp, you could do it. But you wouldn't want to use map/reduce/remove/etc which are all linear time on the sequences they operate on (which can be lists or vectors). So clean looking code turns out to have poor performance because you have multiple O(n) operations, introducing large constants or if they're nested turns them into quadratic or worse operations. For instance, if we wanted to compute dot product we could, in CL, do: (defun dot-product (v1 v2) (reduce #'+ (mapcar #'* v1 v2))) That's O(n) which is the "best" we can do for dot product. But it actually contains two iterations. In C, you'd have: double dotproduct = 0.0; for(int i = 0; i < vector_length; i++) { dotproduct += v1[i]*v2[i]; } While for this small example the difference in performance is minor, in the case of a library making many calls to these functions that double loop would add up. And for more complex operations you're introducing higher constants (or worse) by using the clean looking Common Lisp code. Enter something like Series Series: https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node347.html https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node347.html See here for a demo of how it tranforms functional code to imperative: https://malisper.me/loops-in-lisp-part-4-series/ https://malisper.me/loops-in-lisp-part-4-series/ So you still (as the user) get the illusion of working in a functional language, but under-the-hood it happily transforms itself to an imperative form. And if you wanted to make something like Series, you could present that functional form to your users and hide the imperative, optimized form. Main takeaway: Common Lisp has high-level functional aspects, OO aspects, but also low-level systems-oriented and imperative aspects. You get to pick and choose which level you want to operate on based on your domain. And with the macro language you can present clean, functional interfaces to the end-user while using the lower-level features to optimize things for them.