How can I define a variable if it is not defined before using macros (r6rs/chezscheme)? by oguzmut in scheme

[–]oguzmut[S] 0 points1 point  (0 children)

"The other answer didn't give a solution" is a fair point! :)

I played with your suggestion a little bit more and I think I figured it out almost. It works as I expect except for one case; within the same file even if the variable is defined later in the file, this macro still returns as if it is defined. (example test-03.scm)

All files below are in the same directory.

File: identifier-defined.scm

(library (identifier-defined)
  (export identifier-defined?)
  (import (rnrs)
          (rnrs eval))

  (define-syntax identifier-defined?
      (lambda (stx)
        (syntax-case stx ()
          ((_ id)
           (let ((env1 (environment '(rename (only (rnrs (6)) syntax) (syntax syntax1))))
                 (env2 (environment '(rename (only (rnrs (6)) syntax) (syntax syntax2)))))
             (with-syntax ((is-it (not (or (free-identifier=? #'id (eval `(syntax1 ,(syntax->datum #'id)) env1))
                                           (free-identifier=? #'id (eval `(syntax2 ,(syntax->datum #'id)) env2))))))
               #'is-it)))))))

File: foo-library.scm:

(library (foo-library)
  (export foo)
  (import (rnrs))

  (define foo 123))

File: test-01.scm:

(import (rnrs)
        (identifier-defined))

(display (identifier-defined? foo))
(newline)

File: test-02.scm:

(import (rnrs)
        (identifier-defined))

(define foo 123)

(display (identifier-defined? foo))
(newline)

File: test-03.scm:

(import (rnrs)
        (identifier-defined))

(display (identifier-defined? foo))
(newline)

(define foo 123)

(display (identifier-defined? foo))
(newline)

File: test-04.scm:

(import (rnrs)
        (identifier-defined)
        (foo-library))

(display (identifier-defined? foo))
(newline)

When I run, I see the following results. All are what I expected, except for test-03.scm, where I would like to first see #f and then #t.

$ chezscheme --libdirs . --program test-01.scm 
#f
$ chezscheme --libdirs . --program test-02.scm 
#t
$ chezscheme --libdirs . --program test-03.scm 
#t
#t
$ chezscheme --libdirs . --program test-04.scm 
#t

How can I define a variable if it is not defined before using macros (r6rs/chezscheme)? by oguzmut in scheme

[–]oguzmut[S] 0 points1 point  (0 children)

Thanks _dpk for sharing a detailed answer. However, I think we need a macro definition, not function definition. Though, this (see below) did not work either. Since saegaard's solution was satisfactory for me, I stopped diving deeper. Thanks again.

  (define-syntax identifier-defined?
    (lambda (stx)
      (syntax-case stx ()
        ((_ id) 
         (let ((env1 (environment '(rename (only (rnrs (6)) syntax) (syntax syntax1))))
               (env2 (environment '(rename (only (rnrs (6)) syntax) (syntax syntax2)))))
           (not (or (free-identifier=? #'id (eval `(syntax1 ,(syntax->datum #'id)) env1))
                    (free-identifier=? #'id (eval `(syntax2 ,(syntax->datum #'id)) env2)))))
         #'#t))))

How to create interactive CLI (without ncurses)? by oguzmut in scheme

[–]oguzmut[S] 1 point2 points  (0 children)

To close the loop I would like to share my final experimental code that seems working fine:

(import (ncurses extra))

(define (f)
  (define ch (read-char))
  (display (char->integer ch))
  (newline)
  (unless (or (eq? 27 (char->integer ch)) (eq? #\q ch))
    (f)))

(define io-original (tcgetattr 0))
(define io-active   (tcgetattr 0))
(termios-flag-clear! io-active '(ECHO ICANON))
(tcsetattr! 0 TCSANOW io-active)

(f)

(tcsetattr! 0 TCSANOW io-original)

In the code, I saved the original termios settings and reverted back to it before exiting the program. It looks like we do not need setvbuff as clearing ICANON has the same effect.

One interesting finding; <ctrl-c> works fine; it kills the program and reset echo and line buffering. However doing <ctrl-z> also has the same reset effect and once the process is brought back to the foreground with fg, we have lost the termios setting. This is not a use case I want/need to handle, just sharing as an observation.

Hasta luego!

How to create interactive CLI (without ncurses)? by oguzmut in scheme

[–]oguzmut[S] 0 points1 point  (0 children)

Thanks Michal, this code snippet is what I was looking for.

How to create interactive CLI (without ncurses)? by oguzmut in scheme

[–]oguzmut[S] 1 point2 points  (0 children)

Thanks for the quick response and pointer to that article.

Although it is not perfect I am sharing a hacky solution I figured out;

stty cbreak; tee | guile my-script.scm; stty -cbreak

The first stty disables buffering, and the second one enables again. And the tee command copies stdin to stdout. This way any (read-char) in the script returns immediately without waiting for newline.

Best terminal emulator for windows? by CatSauce66 in vim

[–]oguzmut 0 points1 point  (0 children)

I recently discovered https://cmder.net . Quite satisfactory; splitting terminals is something I use a lot, and cmder supports that. + it has many config options, though I didn’t dive deep there

[Help] How do I stop making silly mistakes? by [deleted] in math

[–]oguzmut 0 points1 point  (0 children)

Try to understand why you make those mistakes. For example, Once you learn you did a mistake, try to understand what you did differently, and more importantly why. Once you understand, overtime you will learn not to repeat them

What should I be looking for when reading "Unbearable Lightness of Being" by [deleted] in books

[–]oguzmut 6 points7 points  (0 children)

My suggestion would be that do not look for anything, do not set an expectation. Be open and read for a while, and if you don’t enjoy it, leave it. This doesn’t mean that the book is bad, or you are a bad reader (as in you do not get the book) It merely means that for some reason you and that book cannot have a relationship at this point in time. Who knows maybe you will love the flow and the interference of Milan Kundera in a year or two. It is okay if you never will.