lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Scheme coding] turning a list into a markup/string


From: David Kastrup
Subject: Re: [Scheme coding] turning a list into a markup/string
Date: Tue, 21 Jan 2020 21:42:10 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Kieren MacMillan <address@hidden> writes:

> Hi all,
>
> Here’s a perfect example of why I keep stumbling (and stopping) when trying 
> to learn Scheme+Lilypond…  =\
>
> With Mike S’s help — read: he did it all, actually! (though I fully 
> understand every part of the code) — I have the following:
>
> %%%  SNIPPET BEGINS
> \version "2.19.83"
>
> some-music = { a'4 bes' b' aes' g' cis' d' ees' fis' f' e' c' }
>
> #(define (zip . xss) (apply map list xss))
>
> #(display
>  (let* ((res (map
>               (lambda (foo) (ly:pitch-semitones (ly:music-property foo 
> 'pitch)))
>               (ly:music-property  some-music 'elements)))
>         (top (reverse (cdr (reverse res))))
>         (bottom (cdr res)))
>    (map (lambda (h) (- (cadr h) (car h))) (zip top bottom)))
>  )
> %%%  SNIPPET ENDS
>
> Works great. Just what I want at this stage in the (multi-stage) procedure 
> I’m trying to code.
>
> As the next step, I want to turn this into a function and display the
> result in a markup. Result: I spend several hours searching
> documentation, trying different functions, and getting one (or more!)
> errors per attempt, but no success. I literally cannot figure out how
> to turn this into a string, save hand-coding a recursion/map that
> takes each element of the list and appends it onto a string… and if
> that’s actually the "correct"/"best" way to do it, then I deeply
> question my desire to code in Scheme+Lilypond at all.  ;)

You are letting yourself getting infected by Mike's coding style.  Mike
is a genius that will reinvent three wheels in the time it takes to
learn about one.

Try the following:

%%%  SNIPPET BEGINS
\version "2.19.83"

some-music = { a'4 bes' b' aes' g' cis' d' ees' fis' f' e' c' }

#(display (let ((res (music-pitches some-music)))
           (map ly:pitch-semitones (map ly:pitch-diff (cdr res) res))))

buzz =
#(define-scheme-function (mus) (ly:music?)
  (map number->string
   (let ((res (music-pitches mus)))
    (map ly:pitch-semitones (map ly:pitch-diff (cdr res) res)))))

\markup \right-column \with-color #red { \buzz \some-music }

%%%  SNIPPET ENDS
> So someone please tell me what simple thing I’m missing here. In the
> worst case scenario, you just give me a fish and I can eat for a
> little while longer (read: not give up); best case senario, you teach
> me (and anyone else reading this list, now or in the future in the
> future) how to fish.

For teaching how to fish from scratch, Mike's demonstration may be
better-suited.  My solution is more a demonstration of what you can do
when you know your way around the fishing grounds.  I find it great that
you understand everything that Mike does here: that's really involved
stuff, and understanding it means that you are well-prepared to figure
out what I am doing, and what may be hidden away in some of the
internals of the read-made tools I use.

At my advanced age, I tend to run into "eyes glaze over" territory
pretty fast, so it is seminal for me to tackle problems with the
simplest means I can manage.

-- 
David Kastrup

reply via email to

[Prev in Thread] Current Thread [Next in Thread]