mailing-list for TeXmacs Users

Text archives Help


Re: [TeXmacs] style sheet language value typing


Chronological Thread 
  • From: Kyle Andrews <address@hidden>
  • To: address@hidden
  • Subject: Re: [TeXmacs] style sheet language value typing
  • Date: Fri, 10 Aug 2018 18:02:42 -0400

Despite my code rendering correctly in the Scheme session, I can't figure out how to actually embed it in a macro. I've placed the following code into my-init-buffer.scm:

----------
;; Return the substring containing the last k characters from string s.
(define (string-last s k)
  (let ((n (string-length s)))
    (string-tail s (- n (min n k)))))

;; Return the TeXmacs tree version of the suffix
(define (sup y p) `(concat ,y (rsup ,p)))

;; Return the properly suffixed version of an integer.
(define (integer-suffix n suff)
  (let* ((m  (number->string n))
         (t1 (string-last m 1))
         (t2 (string-last m 2)))
    (cond ((member t2 (list "11" "12" "13"))
       (suff m "th"))
          ((equal? "1" t1) (suff m "st"))
          ((equal? "2" t1) (suff m "nd"))
          ((equal? "3" t1) (suff m "rd"))
          (else            (suff m "th")))))
------------

Then I tried to make an extern using the following forms:

<extern|`(lambda (x) (integer-suffix ,x sup))|4>>
<extern|(lambda (x) (integer-suffix x sup))|4>>

But both of these give me an error. How is this supposed to work?

Regards,

Kyle



On Mon, 6 Aug 2018 at 00:57 Kyle Andrews <address@hidden> wrote:
Thanks very much for your time, Takama.

I've inferred from the code you provided and reading more style-sheet code that I should really do this kind of computation in scheme.

So, here is my implementation for future reference to others. Maybe there is a more concise way, but this appears to work.

(define (last-character x)
  (char->string
   (last
    (string->list
     (if (integer? x)
       (number->string x) x)))))

(define (sup y p) `(concat ,y (rsup ,p)))

(define (integer-suffix n suff)
  (let ((d (last-character n))
        (m (number->string n)))
    (cond ((member m (list "11" "12" "13")) (suff m "th"))
          ((equal? "1" d) (suff m "st"))
          ((equal? "2" d) (suff m "nd"))
          ((equal? "3" d) (suff m "rd"))
          (else           (suff m "th")))))

(stree->tree (integer-suffix 42 sup))

Regards,

Kyle


On Sun, 5 Aug 2018 at 09:56 Takama M. <address@hidden> wrote:
I don't understand things completely, but you can do as follows.

In the scheme session, define a function

     (define (bottom-elm ls)
       (if (pair? ls)
           (bottom-elm (list-ref ls (1- (length ls))))
           ls))

then, for example
     (bottom-elm '(1 2 3 (4 5) (6 7 (8 9 10)))) gives 10.

Using this function, the _expression_
     <minus|5|<extern|(compose bottom-elm tree->stree)|<last-character|42>>>
should work with the result (i.e. 3).

So, to do all things in macro expressions,

     <assign|cval|<macro|expr|<extern|(lambda (expr) (letrec ((belm (lambda
     (xs) (if (pair? xs) (belm (list-ref xs (1- (length xs)))) xs)) )) (belm
     (tree->stree expr))))|<arg|expr>>>>

then
     <minus|5|<cval|<last-character|42>>> gives the answer.

You can also define the 'nth' macro as

    <assign|nth|<macro|n|<with|d|<cval|<last-character|<arg|n>>>|

<case|<equal|<arg|d>|1>|<arg|n><rsup|st>|<equal|<arg|d>|2>|<arg|n><rsup|nd>|<equal|<value|d>|3>|
       <arg|n><rsup|rd>|<arg|n><rsup|th>>>>>

--
Takama M.

On 2018年08月04日 13:40, Kyle Andrews wrote:
> Dear TeXmacs users,
>
> I am trying to learn the style sheet language by implementing a simple
> macro called nth. The goal of this macro is to transform 1 and 1st, 2 into
> 2nd, 3 into 3rd, and 4 into 4th, and so on. The code is shown below.
>
> <assign|last-character|<macro|expr|<with|n|<length|<arg|expr>>|<range|<arg|expr>|<minus|<value|n>|1>|<value|n>>>>>
>
> <assign|nth|<macro|n|<with|d|<last-character|<arg|n>>|<case|<equal|<arg|d>|1>|<arg|n><rsup|st>|<equal|<arg|d>|2>|<arg|n><rsup|nd>|<equal|<value|d>|3>|<arg|n><rsup|rd>|<arg|n><rsup|th>>>>>
>
> It seems like it should work, but I'm missing something fundamental in my
> understanding about how the style sheet macro language does value typing. I
> tried taking <minus|5|<last-character|42>>, and got <error|bad plus/minus>.
> When I tried to compare equality, I got false. Clearly these things are
> being interpreted by the parser in a way I don't understand.
>
> I tried searching for some documentation about this kind of thing in the
> manual, but so far haven't had any luck. I would appreciate any pointers
> from the community.
>
> Best Regards,
>
> Kyle
>
>
> P.S.
>
> Here is a nicer scheme-like representation fo the macro code:
>
> (document
>   (assign "last-character"
>       (macro "expr"
>          (with "n" (length (arg "expr"))
>                (range (arg "expr")
>                   (minus (value "n") "1")
>                   (value "n")))))
>
>   (assign "nth"
>       (macro "n"
>          (with "d" (last-character (arg "n"))
>                (case (equal (arg "d") "1") (concat (arg "n") (rsup "st"))
>                  (equal (arg "d") "2") (concat (arg "n") (rsup "nd"))
>                  (equal (value "d") "3") (concat (arg "n") (rsup "rd"))
>                  (concat (arg "n") (rsup "th")))))))
>


--
===============================================
高間 道秋
Tel   : 075-703-4142
E-mail: address@hidden
          address@hidden
===============================================



Archive powered by MHonArc 2.6.19.

Top of page