• Re: Translating circular Haskell code to lisp

    From B. Pym@21:1/5 to Pascal Costanza on Tue Jun 18 05:44:10 2024
    Pascal Costanza wrote:

    I don't need a general purpose transformation, just some guidelines to follow when I see code like this.

    General guideline: Look for a solution that uses LOOP. ;)

    (defun diff3 (list)
    (let ((avg (loop for element in list
    sum element into sum
    count t into length
    finally (return (/ sum length)))))
    (loop for element in list
    collect (- element avg))))

    Gauche Scheme

    (define (diff3 lst :optional (seen '()) (sum 0) (cnt 0))
    (if (null? lst)
    (let1 avg (/ sum cnt)
    (map (cut - <> avg) (reverse seen)))
    (let1 x (car lst)
    (diff3 (cdr lst) (cons x seen) (+ sum x) (+ cnt 1)))))

    (diff3 '(1 2 3 4 5))
    ===>
    (-2 -1 0 1 2)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Pascal Costanza on Fri Aug 30 17:09:35 2024
    XPost: comp.lang.scheme

    Pascal Costanza wrote:

    I don't need a general purpose transformation, just some guidelines to follow when I see code like this.

    General guideline: Look for a solution that uses LOOP. ;)

    (defun diff3 (list)
    (let ((avg (loop for element in list
    sum element into sum
    count t into length
    finally (return (/ sum length)))))
    (loop for element in list
    collect (- element avg))))

    Gauche Scheme

    (define (diff3 lst)
    (let ((len 0) (sum 0))
    (dolist (x lst) (inc! len) (inc! sum x))
    (map (cute - <> (/ sum len)) lst)))

    (diff3 '(1 2 3 4 5))
    ===>
    (-2 -1 0 1 2)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to B. Pym on Fri Aug 30 17:32:35 2024
    XPost: comp.lang.scheme

    On 2024-08-30, B. Pym <Nobody447095@here-nor-there.org> wrote:
    Pascal Costanza wrote:

    I don't need a general purpose transformation, just some guidelines to
    follow when I see code like this.

    General guideline: Look for a solution that uses LOOP. ;)

    (defun diff3 (list)
    (let ((avg (loop for element in list
    sum element into sum
    count t into length
    finally (return (/ sum length)))))
    (loop for element in list
    collect (- element avg))))

    Gauche Scheme

    (define (diff3 lst)
    (let ((len 0) (sum 0))
    (dolist (x lst) (inc! len) (inc! sum x))
    (map (cute - <> (/ sum len)) lst)))

    The loop algorithm contains no explicitly visible side effects
    whereas yours has ugly variable stepping.

    Point-free one-liner:

    This is the TXR Lisp interactive listener of TXR 296.
    Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
    TXR kind of supports IEEE 754.00000000000003 floating-point.
    [[callf mapcar [chain [callf / sum len] (do op - @@1)] identity] '(1
    2 3 4 5)]
    (2.0 1.0 0.0 -1.0 -2.0)

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Sat Aug 31 20:32:15 2024
    XPost: comp.lang.scheme

    B. Pym wrote:

    Pascal Costanza wrote:

    I don't need a general purpose transformation, just some guidelines to follow when I see code like this.

    General guideline: Look for a solution that uses LOOP. ;)

    (defun diff3 (list)
    (let ((avg (loop for element in list
    sum element into sum
    count t into length
    finally (return (/ sum length)))))
    (loop for element in list
    collect (- element avg))))

    Gauche Scheme

    (define (diff3 lst)
    (let ((len 0) (sum 0))
    (dolist (x lst) (inc! len) (inc! sum x))
    (map (cute - <> (/ sum len)) lst)))

    (diff3 '(1 2 3 4 5))
    ===>
    (-2 -1 0 1 2)

    Since it's "cute" instead of "cut", (/ sum len) is done only once.

    "Cute is a variation of cut that evaluates expr-or-slots before
    creating the procedure."

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