Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove allowing string argument in (read) #327

Closed
jcubic opened this issue Mar 5, 2024 · 5 comments
Closed

Remove allowing string argument in (read) #327

jcubic opened this issue Mar 5, 2024 · 5 comments
Milestone

Comments

@jcubic
Copy link
Collaborator

jcubic commented Mar 5, 2024

This will be real braking change, but the code doesn't work correctly anyway:

(let ((x "1 2 3"))
  (write (read x))
  (newline)
  (write (read x))
  (newline))
;; ==> 1
;; ==> 1

You will still be able to use lips.parse, to get the same behavior:

(let ((x "1 2 3"))
  (write (. (lips.parse x) 0))
  (newline)
  (write (. (lips.parse x) 0))
  (newline))

With this the read can be patched to get the old behavior:

(define read (let ((read read))
               (lambda (arg)
                 (if (string? arg)
                     (. (lips.parse arg) 0)
                     (read arg)))))
@jcubic jcubic added this to the 1.0 milestone Mar 5, 2024
jcubic added a commit that referenced this issue Mar 5, 2024
jcubic added a commit that referenced this issue Mar 5, 2024
@jcubic jcubic closed this as completed Mar 5, 2024
@jcubic
Copy link
Collaborator Author

jcubic commented Mar 14, 2024

with idiomatic Scheme you can use string ports from SRFI-6.

(let* ((str "(1 2 3)")
       (port (open-input-string str)))
   (with-input-from-port port read))
;; ==> (1 2 3)
(define read (let ((read read))
               (lambda (arg)
                 (if (string? arg)
                     (let* ((port (open-input-string arg)))
                        (with-input-from-port port read))
                     (read arg)))))

@lassik
Copy link

lassik commented Mar 15, 2024

Traditional:

(read-from-string "1 2 3") ; => 1

(with-input-from-string "1 2 3" read) ; => 1

(with-input-from-string "1 2 3" read-all) ; => (1 2 3)

I recommend adding with-input-from-string and read-all. They are generally useful. Gambit has both.

@jcubic
Copy link
Collaborator Author

jcubic commented Mar 15, 2024

Thanks, for the tip.

@jcubic
Copy link
Collaborator Author

jcubic commented Mar 15, 2024

It seems that with-input-from-string is even in WikiBook about Scheme. BTW: it's worth linking to this book from the website.

@lassik
Copy link

lassik commented Mar 15, 2024

Also in Common Lisp.

I'll add a link to the wikibook, thanks!

jcubic added a commit that referenced this issue Mar 15, 2024
jcubic added a commit that referenced this issue Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants