• Re: Why don't people like lisp?

    From B. Pym@21:1/5 to Pascal Costanza on Sun Jul 7 11:59:24 2024
    Pascal Costanza wrote:

    Indentation in Lisp is not clear enough. Let's look at an example from http://www-users.cs.umn.edu/~gini/aiprog/graham/onlisp.lisp:

    (defun mostn (fn lst)
    (if (null lst)
    (values nil nil)
    (let ((result (list (car lst)))
    (max (funcall fn (car lst))))
    (dolist (obj (cdr lst))
    (let ((score (funcall fn obj)))
    (cond ((> score max)
    (setq max score
    result (list obj)))
    ((= score max)
    (push obj result)))))
    (values (nreverse result) max))))

    Note that only one pair of adjacent lines is indented by the same amount. Other alignments are in the middle of lines.

    Here is a straightforward translation into my dream language; note that there aren't a lot of parens despite insignificant indentation and despite using braces (like C) instead of bracketing keywords (like Pascal):

    def mostn Fun [] = [], null;
    def mostn Fun (First\List) {
    var Result = [First];
    var Max = Fun First;
    each List ?Obj {
    let Score = Fun Obj;
    if Score > Max {Max = Score; Result = [Obj]}
    if Score == Max {Result = Obj\Result}
    };
    reversed Result, Max
    };

    Apparently, Paul Graham doesn't like CLOS nor the LOOP macro. Here is
    another verson in Common Lisp (and this is not a dream language ;):

    (defmethod mostn (fn (list (eql nil)))
    (declare (ignore fn list))
    (values nil nil))

    (defmethod mostn (fn list)
    (loop with result = (list (car list))
    with max = (funcall fn (car list))
    for object in (cdr list)
    for score = (funcall fn object)
    when (> score max) do (setq max score
    result (list object))
    when (= score max) do (push object result)
    finally return (values (nreverse result) max)))

    Gauche Scheme

    (use gauche.collection) ;; fold2

    (define (max-by fn lst)
    (if (null? lst)
    (values '() #f)
    (fold2
    (lambda (x best worth)
    (let ((score (fn x)))
    (cond ((> score worth) (values (list x) score))
    ((= score worth) (values (cons x best) worth))
    (#t (values best worth)))))
    (take lst 1) (fn (car lst))
    (cdr lst))))

    (max-by (lambda(x) (modulo x 5)) '(22 23 24 25 26 27 28 29))

    ===>
    (29 24)
    4

    In Forth?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to All on Wed Jul 10 10:06:25 2024
    Since you feel free to use external collections at will,
    let me allow to use a library as well:

    \ ma.mf
    0 VALUE M
    : M5 swap f>s 5 mod max swap ;
    : F5 f>s 5 mod m = ;
    : MAX-BY mdup 0 ['] m5 mareduce to m ['] f5 vfilter ;

    MinForth 3.6 (32 bit) (fp matrix)
    # fload ma.mf ok
    # m[ 22 23 24 25 26 27 28 29 ] max-by m. mdrop m .
    [ 24 29 ]
    4 ok

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)