[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: scheme set list function
From: |
Thomas Morley |
Subject: |
Re: scheme set list function |
Date: |
Sat, 6 Apr 2019 18:50:29 +0200 |
Am Sa., 6. Apr. 2019 um 17:21 Uhr schrieb Gianmaria Lari
<address@hidden>:
>
> On Sat, 6 Apr 2019 at 16:30, Thomas Morley <address@hidden> wrote:
>> One possibility is to use a macro:
>>
>> #(define-macro (reset-list l) `(set! ,l '()))
>
>
> Thank you, it works.
>
> I tried to make the same with the second of these functions:
>
> #(define (allButLast l) (reverse (cdr (reverse l))))
> #(define (numberInc l) (append (allButLast l) (list (1+ (last l)))))
>
> (it simply increments the last integer element of a list). Here it is a code
> that use it:
>
> \version "2.21.0"
> #(define (allButLast l) (reverse (cdr (reverse l))))
> #(define (numberInc l) (append (allButLast l) (list (1+ (last l)))))
>
> #(define mylist '(1 2 3))
> \markup #(object->string mylist)
> #(set! mylist (numberInc mylist))
> \markup #(object->string mylist)
>
>
> And here my try with macro:
>
> #(define-macro (numberInc l) `(set! ,l (append (allButLast l) (list (1+ (last
> l))))))
>
>
> and this is a complete code that does not work:
>
> \version "2.21.0"
> #(define (allButLast l) (reverse (cdr (reverse l))))
> #(define-macro (numberInc l) `(set! ,l (append (allButLast l) (list (1+ (last
> l))))))
>
>
> #(define mylist '(1 2 3))
> \markup #(object->string mylist)
> #(numberInc mylist)
> \markup #(object->string mylist)
Well, you need to always unquote the macro's arg(s), here `l´.
Leading to:
#(define-macro (numberInc l)
`(set! ,l (append (allButLast ,l) (list (1+ (last ,l))))))
Though, I'd think you should rethink whatever you plan, I'm very
skeptic about always resetting a toplevel defined list.
It's too destructive.
Why not let the toplevel list untouched, only changing it locally, like:
#(define (1+last l)
(if (number? (last l))
(append (drop-right l 1) (list (1+ (last l))))
l))
#(define lst '(1.1 2.2 3.3 4.4))
#(display (1+last lst))
For my own projects I need to define such macros averagely every other
5 years ...
Cheers,
Harm
Re: scheme set list function, Andrew Bernard, 2019/04/07
Re: scheme set list function, Andrew Bernard, 2019/04/08
Re: scheme set list function, Urs Liska, 2019/04/08