Ah OK, I understand that additional newline. Since the code matching the
regex is run before that newline gets in, the newline will be on the
input port.

In DrRacket I observe the following:

(I remembered that there are negative numbers :D and my regex got a bit
more complicated.)

~~~
(regexp-try-match #rx"\\s*^(-?[1-9]+[0-9]*)$|\\s*^0$" (current-input-port))
~~~

When I run it the first time, I get the "input box" where I can click
the "EOF" button on the right and the regex seems to work. When I try
again immediately after, the first time, there is still an #<eof> on the
port, which apparently was not consumed, so it will immediately return
#f. I can get rid of that #<eof> by running (read-line) once. On REPL
line I would have a newline and in DrRacket I have an #<eof>.

On REPL it does not stop expecting input, because it never receives an
#<eof>. I read somewhere on Stackoverflow that I input #<eof> by
pressing Ctrl+d and that may be true, but it does not work in REPL,
because that shuts down the REPL. So basically it seems impossible to
test such regular expressions on REPL.

I think I will instead use:

~~~
(let ([something (read-line (current-input-port) 'any)])
  (... do regex things on finished string here ...))
~~~

to work around this problem in the future. This will actually finish the
line at any marker of a new line and then let me work with the regex
more comfortably. Also instead of running it in the REPL, I will
probably run a small program instead with racket <racket file>.

Thanks for clearing up my confusion!

I tried to write this in Typed Racket and the following is what I got.
This works:

~~~
(: get-input-number (-> Integer))
(define (get-input-number)
  (let loop : Integer ()
    (let ([something (read-line (current-input-port) 'any)])
      (cond [(string? something)
             (let ([number-representation (string->number something)])
               (cond [(exact-integer? number-representation) 
number-representation]
                     [else (printf "nope~n")
                           (loop)]))]
            [else (printf "nope~n")
                  (loop)]))))

(get-input-number)
~~~

While the following does not work:

~~~
(define (get-input-number-2)
  (let loop : Integer ()
    (let* ([something (read-line (current-input-port) 'any)]
           [match-res (regexp-match #rx"(^-?[1-9]+[0-9]*$)|(^0$)" something)])
      (cond [(string? something)
             (let ([number-representation (string->number something)])
               (cond [(exact-integer? number-representation) 
number-representation]
                     [else (printf "nope~n")
                           (loop)]))]
            [else (printf "nope~n")
                  (loop)]))))

(get-input-number-2)
~~~

The opening paren of regexp-match is underlined and when I run this code
I get the lengthy error message:

~~~
main.rkt:34:22: Type Checker: No function domains matched in function
application:
Domains: (U Byte-Regexp Bytes Regexp String) (U Bytes Input-Port)
Integer (U False Integer) (U False Output-Port) Bytes #f *
         (U Byte-Regexp Bytes Regexp String) (U Bytes Input-Port)
Integer (U False Integer) (U False Output-Port) #f *
         (U Byte-Regexp Bytes Regexp String) (U Bytes Input-Port)
Integer (U False Integer) #f *
         (U Byte-Regexp Bytes Regexp String) (U Bytes Input-Port)
Integer #f *
         (U Byte-Regexp Bytes Regexp String) (U Bytes Input-Port) #f *
         (U Byte-Regexp Bytes) (U Bytes Input-Port Path-String) Integer
(U False Integer) (U False Output-Port) Bytes #f *
         (U Byte-Regexp Bytes) (U Bytes Input-Port Path-String) Integer
(U False Integer) (U False Output-Port) #f *
         (U Byte-Regexp Bytes) (U Bytes Input-Port Path-String) Integer
(U False Integer) #f *
         (U Byte-Regexp Bytes) (U Bytes Input-Port Path-String) Integer #f *
         (U Byte-Regexp Bytes) (U Bytes Input-Port Path-String) #f *
         (U Regexp String) Path-String Integer (U False Integer) (U
False Output-Port) Bytes #f *
         (U Regexp String) Path-String Integer (U False Integer)
  context...:
  
/usr/share/racket/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:345:0:
type-check
  
/usr/share/racket/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:588:0:
tc-module
  
/usr/share/racket/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:94:0:
tc-module/full
  
/usr/share/racket/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:23:4
   standard-module-name-resolver
~~~

(Racket 6.10.1)

I am aware, that I do not really need the match-res, but this is only
some trying out of stuff. I am not really sure what it expects me to do
about the regexp-match. It seems it must be used differently in TR.
Should I put this in a new question ("How to use regexp-match in TR?")
instead?


On 22.04.2018 15:15, Matthew Flatt wrote:
> At Sun, 22 Apr 2018 14:54:26 +0200, Zelphir Kaltstahl wrote:
>> I am sorry, I think I am still misunderstanding it.
>>
>> When I try:
>>
>> (regexp-try-match #rx"^[1-9]+[0-9]*$" (current-input-port))
>>
>> It also results immediately in:
>>
>>  #f
> Using stdin both for reading an expresion and getting input can be
> confusing. At Racket's ">" prompt, when you run
>
>  >  (regexp-try-match #rx"^[1-9]+[0-9]*$" (current-input-port))
>
> by hitting Return, then a newline is included is in the input stream
> just after the `(regexp-try-match ...)` expression. That newline is
> still there when you try to match against the regexp, so the match
> fails.
>
> DrRacket separates the input stream for expressions and for
> `current-input-port`. So, if you try that expression in DrRacket's
> interactions area, and if you hit the "EOF" button (to the right of the
> input box) instead of Return after a number, then you should get a
> match. Note that if you hit Return after the number, then it won't
> match even in DrRacket, because the input port will have a newline
> after the number.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to