lambda symbol in code file

34 views
Skip to first unread message

Doug Telford

unread,
May 21, 2021, 7:34:11 AM5/21/21
to Shen
vim and emacs offer a way to include the lambda symbol in a file.  To me it is so much more pleasing than /.  .  If I try something like

(define λ    \\ in vim: ctrl-k l*
  Arg Body -> (lambda Arg Body))  \\ does not work

I get a read error.  How difficult would it be ot allow the lambda symbol in shen?

nha...@gmail.com

unread,
May 22, 2021, 2:18:35 PM5/22/21
to Shen
The reader needs to be modified and you need to implement utf8 decoding over byte streams. Below is a basic utf8 decoder but it uses some bit twiddling features of Chez scheme which I couldn't be bothered to write.

An easier solution is just to have emacs display /. differently like this http://ergoemacs.org/emacs/emacs_pretty_lambda.html. There are probably other mods as well.


(package utf8 []

 (define gte
   Min Z -> (error "malformed utf8") where (< Z Min)
   Min Z -> Z)

 (define <*>
   L R -> (scm.fxlogand L R))

 (define <+>
   L R -> (scm.fxlogor L R))

 (define <<
   L R -> (scm.fxsll L R))

 (define utf8-rest
   -1 -> (error "partial utf-8")
   X  -> (let HI (<*> X (scm. "#b11000000"))
              LO (<*> X (scm. "#b00111111"))
            (if (= HI (scm. "#b10000000"))
                LO
                (error "malformed utf8"))))


 (define utf8->utf32
   X F -> X
     where (< X (scm. "#b10000000"))

   X F -> (error "malformed utf8")
     where (< X (scm. "#b11000000"))

   X F -> (let Z (<*> X (scm. "#b11111"))
               Z (<+> (<< Z 6) (utf8-rest (F)))
             (gte (scm. "#x80") Z))

     where (< X (scm. "#b11100000"))

   X F -> (let Z (<*> X (scm. "#b1111"))
               Z (<+> (<< Z 6) (utf8-rest (F)))
               Z (<+> (<< Z 6) (utf8-rest (F)))
             (gte (scm. "#x800") Z))

     where (< X (scm. "#b11110000"))

   X F -> (let Z (<*> X (scm. "#b111"))
               Z (<+> (<< Z 6) (utf8-rest (F)))
               Z (<+> (<< Z 6) (utf8-rest (F)))
               Z (<+> (<< Z 6) (utf8-rest (F)))
             (gte (scm. "#x100000") Z))

     where (< X (scm. "#b11111000"))

   _ _ -> (error "malformed utf8"))


 (define string-generator
   S -> (let Ctx (address-> (absvector 1) 0 S)
           (freeze (let S (<-address Ctx 0)
                      (if (= "" S)
                          -1
                          (do
                           (address-> Ctx 0 (tlstr S))
                           (string->n (hdstr S))))))))

 (define list-generator
   XS -> (let Ctx (address-> (absvector 1) 0 XS)
            (freeze (let XS (<-address Ctx 0)
                       (if (cons? XS)
                           (do
                            (address-> Ctx 0 (tl XS))
                            (hd XS))
                           -1)))))


 (define utf8.gen8->list32
   F -> (let X (F)
           (if (= -1 X)
               []
               [(utf8->utf32 X F) | (utf8.gen8->list32 F)])))

 (define utf8.gen8->foreach
   G F -> (let X (F)
             (if (= -1 X)
                 unit
                 (do
                  (G (utf8->utf32 X F))
                  (utf8.gen8->foreach G F)))))


 (define utf8.string->codepoints
   XS -> (let F (string-generator XS)
            (utf8.gen8->list32 F)))

 (define utf8.bytestream->codepoints
   File -> (let F (freeze (read-byte File))
              (utf8.gen8->list32 F)))


 (define utf8.read-codepoint
   Stream -> (let F (freeze (read-byte Stream))
                (utf8->utf32 (F) F)))

 (define utf8.unrank
   N -> (scm.string (scm.integer->char N)))

 (define utf8.rank
   XS -> (let F (string-generator XS)
            (utf8->utf32 (F) F)))
 )

Reply all
Reply to author
Forward
0 new messages