• .Re: A simple lisp problem.

    From Robert L.@21:1/5 to All on Sat Mar 19 23:50:53 2022
    I am Lisp beginner and I am doing a simple execise on lisp. I was asked
    to write a lisp program , by using either recursion or do, to return
    true if the difference between each successive pair of the them is 1.

    ie:

    (difference '(2 1))
    T
    (difference'(3 4 5 6 5 4))
    T
    (differnce '(3 4 5 3))
    NIL
    ....

    (defun difference (param)
    (if (> (length param) 1)
    (if (<= (abs (- (first param) (first (rest param)))) 1 )
    (difference (rest param))
    nil
    )
    T
    )
    )

    Gauche Scheme

    Using "every":

    (define (diff1 xs)
    (or (null? xs)
    (every (compose ($ = 1 $) abs -) xs (cdr xs))))

    Using recursion:

    (define (diff1 xs)
    (if (or (null? xs) (null? (cdr xs)))
    #t
    (if (= 1 (abs (- (car xs) (cadr xs))))
    (diff1 (cdr xs))
    #f)))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Frank Schwieterman on Sat Jun 15 03:45:39 2024
    Frank Schwieterman wrote:

    I am Lisp beginner and I am doing a simple execise on lisp. I was asked
    to write a lisp program , by using either recursion or do, to return
    true if the difference between each successive pair of the them is 1.

    ie:

    (difference '(2 1))
    T
    (difference'(3 4 5 6 5 4))
    T
    (differnce '(3 4 5 3))
    NIL
    ....

    (defun difference (param)
    (if (> (length param) 1)
    (if (<= (abs (- (first param) (first (rest param)))) 1 )
    (difference (rest param))
    nil
    )
    T
    )
    )

    Gauche Scheme

    (define (diff1? xs)
    (every
    (lambda (a b) (= 1 (abs (- a b))))
    xs
    (cdr xs)))

    (diff1? '(2 3 4 3 4 5 6 7 8 9 8))
    ===>
    #t
    (diff1? '(2 3 4 3 4 5 6 7 8 9 8 0))
    ===>
    #f

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From HenHanna@21:1/5 to B. Pym on Sat Jun 15 13:08:40 2024
    On 6/14/2024 8:45 PM, B. Pym wrote:
    Frank Schwieterman wrote:

    I am Lisp beginner and I am doing a simple execise on lisp. I was asked
    to write a lisp program , by using either recursion or do, to return
    true if the difference between each successive pair of the them is 1.

    ie:

    (difference '(2 1))
    T
    (difference'(3 4 5 6 5 4))
    T
    (differnce '(3 4 5 3))
    NIL
    ....

    (defun difference (param)
    (if (> (length param) 1)
    (if (<= (abs (- (first param) (first (rest param)))) 1 )
    (difference (rest param))
    nil
    )
    T
    )
    )


    (define (diff1p? x)
    (or (null? x)
    (null? (cdr x))
    (and (= 1 (abs (- (car x) (cadr x)) ))
    (diff1p? (cdr x)) )))



    Gauche Scheme

    (define (diff1? xs)
    (every
    (lambda (a b) (= 1 (abs (- a b))))
    xs
    (cdr xs)))

    (diff1? '(2 3 4 3 4 5 6 7 8 9 8)) ===> #t

    (diff1? '(2 3 4 3 4 5 6 7 8 9 8 0)) ===> #f



    __________________________
    find
    find-tail
    any
    every
    count


    every pred clist1 clist2 . . . [Function]

    [R7RS list] Applies pred across each element of clists, and returns
    #f as soon as pred returns #f. If all application of pred
    return a non-false value, [every] returns the last result of the
    applications.


    it sounds like it expands to a big (OR ........ )



    ______________________________ Not

    (define (every? predicate lst) (and (map predicate lst)))

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