About “with-input-from-file” command on Scheme












-2














How do I call the content of a file in an interpreter of Scheme working on the terminal of Ubuntu?



I am trying to make use of some data included into a text file and "with-input-from-file" is not working, even spelling the exact direction of that file. I would like to call that content without having to execute the file first.










share|improve this question
























  • The code does not make sense. /home/.../file is a variable since it is not quoted. eg it is assumed to be defined by you at an earlier point and since you do cdr of it it should be a pair like (cons 4 5). Then the expression would work and become 5. with-input-from-file will replace stdin with the contents of the file name in the first argument for the duration og the function in the second. thus (read-char) will read the char from the file instead of stdin. You are probably looking for (load "path/to/file")?
    – Sylwester
    Nov 12 '18 at 11:37












  • Your question is not very clear, but maybe you are looking for load? ie (load "/home/.../file.scm"). It will read and evaluate (call) content of a file.
    – rsm
    Nov 12 '18 at 11:38












  • What format is the data in the text file? Is it Scheme code, S-expressions, Comma-separated values, Tab-separated values, Json? Or do you just want one string for the whole value?
    – Alex Knauth
    Nov 12 '18 at 15:48










  • Also, which variant of scheme are you using? Is it R6RS-compatible, R5RS-compatible, or?
    – Alex Knauth
    Nov 12 '18 at 15:50










  • I do not understand, I'm sorry. I have been asked to make use of "with-input-from-file" to use that data as an input for a simple procedure. Is it possible to do it on the ubuntu terminal? How? Alex Knauth, I am using Racket 7.0 and make use of a R5RS manual.
    – gibarian
    Nov 12 '18 at 15:51


















-2














How do I call the content of a file in an interpreter of Scheme working on the terminal of Ubuntu?



I am trying to make use of some data included into a text file and "with-input-from-file" is not working, even spelling the exact direction of that file. I would like to call that content without having to execute the file first.










share|improve this question
























  • The code does not make sense. /home/.../file is a variable since it is not quoted. eg it is assumed to be defined by you at an earlier point and since you do cdr of it it should be a pair like (cons 4 5). Then the expression would work and become 5. with-input-from-file will replace stdin with the contents of the file name in the first argument for the duration og the function in the second. thus (read-char) will read the char from the file instead of stdin. You are probably looking for (load "path/to/file")?
    – Sylwester
    Nov 12 '18 at 11:37












  • Your question is not very clear, but maybe you are looking for load? ie (load "/home/.../file.scm"). It will read and evaluate (call) content of a file.
    – rsm
    Nov 12 '18 at 11:38












  • What format is the data in the text file? Is it Scheme code, S-expressions, Comma-separated values, Tab-separated values, Json? Or do you just want one string for the whole value?
    – Alex Knauth
    Nov 12 '18 at 15:48










  • Also, which variant of scheme are you using? Is it R6RS-compatible, R5RS-compatible, or?
    – Alex Knauth
    Nov 12 '18 at 15:50










  • I do not understand, I'm sorry. I have been asked to make use of "with-input-from-file" to use that data as an input for a simple procedure. Is it possible to do it on the ubuntu terminal? How? Alex Knauth, I am using Racket 7.0 and make use of a R5RS manual.
    – gibarian
    Nov 12 '18 at 15:51
















-2












-2








-2







How do I call the content of a file in an interpreter of Scheme working on the terminal of Ubuntu?



I am trying to make use of some data included into a text file and "with-input-from-file" is not working, even spelling the exact direction of that file. I would like to call that content without having to execute the file first.










share|improve this question















How do I call the content of a file in an interpreter of Scheme working on the terminal of Ubuntu?



I am trying to make use of some data included into a text file and "with-input-from-file" is not working, even spelling the exact direction of that file. I would like to call that content without having to execute the file first.







scheme






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 11:28

























asked Nov 12 '18 at 9:50









gibarian

365




365












  • The code does not make sense. /home/.../file is a variable since it is not quoted. eg it is assumed to be defined by you at an earlier point and since you do cdr of it it should be a pair like (cons 4 5). Then the expression would work and become 5. with-input-from-file will replace stdin with the contents of the file name in the first argument for the duration og the function in the second. thus (read-char) will read the char from the file instead of stdin. You are probably looking for (load "path/to/file")?
    – Sylwester
    Nov 12 '18 at 11:37












  • Your question is not very clear, but maybe you are looking for load? ie (load "/home/.../file.scm"). It will read and evaluate (call) content of a file.
    – rsm
    Nov 12 '18 at 11:38












  • What format is the data in the text file? Is it Scheme code, S-expressions, Comma-separated values, Tab-separated values, Json? Or do you just want one string for the whole value?
    – Alex Knauth
    Nov 12 '18 at 15:48










  • Also, which variant of scheme are you using? Is it R6RS-compatible, R5RS-compatible, or?
    – Alex Knauth
    Nov 12 '18 at 15:50










  • I do not understand, I'm sorry. I have been asked to make use of "with-input-from-file" to use that data as an input for a simple procedure. Is it possible to do it on the ubuntu terminal? How? Alex Knauth, I am using Racket 7.0 and make use of a R5RS manual.
    – gibarian
    Nov 12 '18 at 15:51




















  • The code does not make sense. /home/.../file is a variable since it is not quoted. eg it is assumed to be defined by you at an earlier point and since you do cdr of it it should be a pair like (cons 4 5). Then the expression would work and become 5. with-input-from-file will replace stdin with the contents of the file name in the first argument for the duration og the function in the second. thus (read-char) will read the char from the file instead of stdin. You are probably looking for (load "path/to/file")?
    – Sylwester
    Nov 12 '18 at 11:37












  • Your question is not very clear, but maybe you are looking for load? ie (load "/home/.../file.scm"). It will read and evaluate (call) content of a file.
    – rsm
    Nov 12 '18 at 11:38












  • What format is the data in the text file? Is it Scheme code, S-expressions, Comma-separated values, Tab-separated values, Json? Or do you just want one string for the whole value?
    – Alex Knauth
    Nov 12 '18 at 15:48










  • Also, which variant of scheme are you using? Is it R6RS-compatible, R5RS-compatible, or?
    – Alex Knauth
    Nov 12 '18 at 15:50










  • I do not understand, I'm sorry. I have been asked to make use of "with-input-from-file" to use that data as an input for a simple procedure. Is it possible to do it on the ubuntu terminal? How? Alex Knauth, I am using Racket 7.0 and make use of a R5RS manual.
    – gibarian
    Nov 12 '18 at 15:51


















The code does not make sense. /home/.../file is a variable since it is not quoted. eg it is assumed to be defined by you at an earlier point and since you do cdr of it it should be a pair like (cons 4 5). Then the expression would work and become 5. with-input-from-file will replace stdin with the contents of the file name in the first argument for the duration og the function in the second. thus (read-char) will read the char from the file instead of stdin. You are probably looking for (load "path/to/file")?
– Sylwester
Nov 12 '18 at 11:37






The code does not make sense. /home/.../file is a variable since it is not quoted. eg it is assumed to be defined by you at an earlier point and since you do cdr of it it should be a pair like (cons 4 5). Then the expression would work and become 5. with-input-from-file will replace stdin with the contents of the file name in the first argument for the duration og the function in the second. thus (read-char) will read the char from the file instead of stdin. You are probably looking for (load "path/to/file")?
– Sylwester
Nov 12 '18 at 11:37














Your question is not very clear, but maybe you are looking for load? ie (load "/home/.../file.scm"). It will read and evaluate (call) content of a file.
– rsm
Nov 12 '18 at 11:38






Your question is not very clear, but maybe you are looking for load? ie (load "/home/.../file.scm"). It will read and evaluate (call) content of a file.
– rsm
Nov 12 '18 at 11:38














What format is the data in the text file? Is it Scheme code, S-expressions, Comma-separated values, Tab-separated values, Json? Or do you just want one string for the whole value?
– Alex Knauth
Nov 12 '18 at 15:48




What format is the data in the text file? Is it Scheme code, S-expressions, Comma-separated values, Tab-separated values, Json? Or do you just want one string for the whole value?
– Alex Knauth
Nov 12 '18 at 15:48












Also, which variant of scheme are you using? Is it R6RS-compatible, R5RS-compatible, or?
– Alex Knauth
Nov 12 '18 at 15:50




Also, which variant of scheme are you using? Is it R6RS-compatible, R5RS-compatible, or?
– Alex Knauth
Nov 12 '18 at 15:50












I do not understand, I'm sorry. I have been asked to make use of "with-input-from-file" to use that data as an input for a simple procedure. Is it possible to do it on the ubuntu terminal? How? Alex Knauth, I am using Racket 7.0 and make use of a R5RS manual.
– gibarian
Nov 12 '18 at 15:51






I do not understand, I'm sorry. I have been asked to make use of "with-input-from-file" to use that data as an input for a simple procedure. Is it possible to do it on the ubuntu terminal? How? Alex Knauth, I am using Racket 7.0 and make use of a R5RS manual.
– gibarian
Nov 12 '18 at 15:51














1 Answer
1






active

oldest

votes


















1














If the Scheme interpreter you're using is R6RS-compatible, you can use with-input-from-file from the io simple library like this:



#!r6rs
(import (rnrs base)
(rnrs io simple))

(with-input-from-file "path/to/file.txt"
(lambda ()
;; do the reading using the current input port
....))


Notice how there are two arguments:




  • the path to the file

  • a lambda () with no arguments that must do the reading


Alternatively, you can use call-with-input-file, also from the io simple library:



#!r6rs
(import (rnrs base)
(rnrs io simple))

(call-with-input-file "path/to/file.txt"
(lambda (in-port)
;; do the reading using `in-port` explicitly
....))


Notice how there are two arguments:




  • the path to the file

  • a lambda (in-port) with one argument that must do the reading, using in-port as an input port


To actually do the reading once you have the input port, you can use functions like read, read-char, get-string-n, get-string-all, get-line, or get-datum. Which one you should use depends on the format of the data in the text file. The simplest one that reads the whole file is get-string-all from the io-ports library:



#!r6rs
(import (rnrs base)
(rnrs io ports)
(rnrs io simple))

(call-with-input-file "path/to/file.txt"
(lambda (in-port)
;; do the reading using `in-port` explicitly
(get-string-all in-port)))


And it is worth noting that if this is your final program it can be simplified to:



#!r6rs
(import (rnrs base)
(rnrs io ports)
(rnrs io simple))

(call-with-input-file "path/to/file.txt" get-string-all)





share|improve this answer























  • I do not see the point yet. Can I call the file as an input? I mean: (function (with-input-from-file "path/to/file.txt")). It is asking two arguments rather than one.
    – gibarian
    Nov 12 '18 at 16:40












  • In both of my examples I am passing two arguments. The first argument is the "path/to/file.txt", and the second argument is a lambda.
    – Alex Knauth
    Nov 12 '18 at 17:07










  • Thank you. This is what I have: "; with-input-from-file: contract violation ; expected: (-> any)"
    – gibarian
    Nov 12 '18 at 17:13










  • Can you update your question with a Minimal Complete example of what you've tried and what's going wrong with it?
    – Alex Knauth
    Nov 12 '18 at 17:18











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259546%2fabout-with-input-from-file-command-on-scheme%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If the Scheme interpreter you're using is R6RS-compatible, you can use with-input-from-file from the io simple library like this:



#!r6rs
(import (rnrs base)
(rnrs io simple))

(with-input-from-file "path/to/file.txt"
(lambda ()
;; do the reading using the current input port
....))


Notice how there are two arguments:




  • the path to the file

  • a lambda () with no arguments that must do the reading


Alternatively, you can use call-with-input-file, also from the io simple library:



#!r6rs
(import (rnrs base)
(rnrs io simple))

(call-with-input-file "path/to/file.txt"
(lambda (in-port)
;; do the reading using `in-port` explicitly
....))


Notice how there are two arguments:




  • the path to the file

  • a lambda (in-port) with one argument that must do the reading, using in-port as an input port


To actually do the reading once you have the input port, you can use functions like read, read-char, get-string-n, get-string-all, get-line, or get-datum. Which one you should use depends on the format of the data in the text file. The simplest one that reads the whole file is get-string-all from the io-ports library:



#!r6rs
(import (rnrs base)
(rnrs io ports)
(rnrs io simple))

(call-with-input-file "path/to/file.txt"
(lambda (in-port)
;; do the reading using `in-port` explicitly
(get-string-all in-port)))


And it is worth noting that if this is your final program it can be simplified to:



#!r6rs
(import (rnrs base)
(rnrs io ports)
(rnrs io simple))

(call-with-input-file "path/to/file.txt" get-string-all)





share|improve this answer























  • I do not see the point yet. Can I call the file as an input? I mean: (function (with-input-from-file "path/to/file.txt")). It is asking two arguments rather than one.
    – gibarian
    Nov 12 '18 at 16:40












  • In both of my examples I am passing two arguments. The first argument is the "path/to/file.txt", and the second argument is a lambda.
    – Alex Knauth
    Nov 12 '18 at 17:07










  • Thank you. This is what I have: "; with-input-from-file: contract violation ; expected: (-> any)"
    – gibarian
    Nov 12 '18 at 17:13










  • Can you update your question with a Minimal Complete example of what you've tried and what's going wrong with it?
    – Alex Knauth
    Nov 12 '18 at 17:18
















1














If the Scheme interpreter you're using is R6RS-compatible, you can use with-input-from-file from the io simple library like this:



#!r6rs
(import (rnrs base)
(rnrs io simple))

(with-input-from-file "path/to/file.txt"
(lambda ()
;; do the reading using the current input port
....))


Notice how there are two arguments:




  • the path to the file

  • a lambda () with no arguments that must do the reading


Alternatively, you can use call-with-input-file, also from the io simple library:



#!r6rs
(import (rnrs base)
(rnrs io simple))

(call-with-input-file "path/to/file.txt"
(lambda (in-port)
;; do the reading using `in-port` explicitly
....))


Notice how there are two arguments:




  • the path to the file

  • a lambda (in-port) with one argument that must do the reading, using in-port as an input port


To actually do the reading once you have the input port, you can use functions like read, read-char, get-string-n, get-string-all, get-line, or get-datum. Which one you should use depends on the format of the data in the text file. The simplest one that reads the whole file is get-string-all from the io-ports library:



#!r6rs
(import (rnrs base)
(rnrs io ports)
(rnrs io simple))

(call-with-input-file "path/to/file.txt"
(lambda (in-port)
;; do the reading using `in-port` explicitly
(get-string-all in-port)))


And it is worth noting that if this is your final program it can be simplified to:



#!r6rs
(import (rnrs base)
(rnrs io ports)
(rnrs io simple))

(call-with-input-file "path/to/file.txt" get-string-all)





share|improve this answer























  • I do not see the point yet. Can I call the file as an input? I mean: (function (with-input-from-file "path/to/file.txt")). It is asking two arguments rather than one.
    – gibarian
    Nov 12 '18 at 16:40












  • In both of my examples I am passing two arguments. The first argument is the "path/to/file.txt", and the second argument is a lambda.
    – Alex Knauth
    Nov 12 '18 at 17:07










  • Thank you. This is what I have: "; with-input-from-file: contract violation ; expected: (-> any)"
    – gibarian
    Nov 12 '18 at 17:13










  • Can you update your question with a Minimal Complete example of what you've tried and what's going wrong with it?
    – Alex Knauth
    Nov 12 '18 at 17:18














1












1








1






If the Scheme interpreter you're using is R6RS-compatible, you can use with-input-from-file from the io simple library like this:



#!r6rs
(import (rnrs base)
(rnrs io simple))

(with-input-from-file "path/to/file.txt"
(lambda ()
;; do the reading using the current input port
....))


Notice how there are two arguments:




  • the path to the file

  • a lambda () with no arguments that must do the reading


Alternatively, you can use call-with-input-file, also from the io simple library:



#!r6rs
(import (rnrs base)
(rnrs io simple))

(call-with-input-file "path/to/file.txt"
(lambda (in-port)
;; do the reading using `in-port` explicitly
....))


Notice how there are two arguments:




  • the path to the file

  • a lambda (in-port) with one argument that must do the reading, using in-port as an input port


To actually do the reading once you have the input port, you can use functions like read, read-char, get-string-n, get-string-all, get-line, or get-datum. Which one you should use depends on the format of the data in the text file. The simplest one that reads the whole file is get-string-all from the io-ports library:



#!r6rs
(import (rnrs base)
(rnrs io ports)
(rnrs io simple))

(call-with-input-file "path/to/file.txt"
(lambda (in-port)
;; do the reading using `in-port` explicitly
(get-string-all in-port)))


And it is worth noting that if this is your final program it can be simplified to:



#!r6rs
(import (rnrs base)
(rnrs io ports)
(rnrs io simple))

(call-with-input-file "path/to/file.txt" get-string-all)





share|improve this answer














If the Scheme interpreter you're using is R6RS-compatible, you can use with-input-from-file from the io simple library like this:



#!r6rs
(import (rnrs base)
(rnrs io simple))

(with-input-from-file "path/to/file.txt"
(lambda ()
;; do the reading using the current input port
....))


Notice how there are two arguments:




  • the path to the file

  • a lambda () with no arguments that must do the reading


Alternatively, you can use call-with-input-file, also from the io simple library:



#!r6rs
(import (rnrs base)
(rnrs io simple))

(call-with-input-file "path/to/file.txt"
(lambda (in-port)
;; do the reading using `in-port` explicitly
....))


Notice how there are two arguments:




  • the path to the file

  • a lambda (in-port) with one argument that must do the reading, using in-port as an input port


To actually do the reading once you have the input port, you can use functions like read, read-char, get-string-n, get-string-all, get-line, or get-datum. Which one you should use depends on the format of the data in the text file. The simplest one that reads the whole file is get-string-all from the io-ports library:



#!r6rs
(import (rnrs base)
(rnrs io ports)
(rnrs io simple))

(call-with-input-file "path/to/file.txt"
(lambda (in-port)
;; do the reading using `in-port` explicitly
(get-string-all in-port)))


And it is worth noting that if this is your final program it can be simplified to:



#!r6rs
(import (rnrs base)
(rnrs io ports)
(rnrs io simple))

(call-with-input-file "path/to/file.txt" get-string-all)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 17:32

























answered Nov 12 '18 at 16:12









Alex Knauth

4,5221822




4,5221822












  • I do not see the point yet. Can I call the file as an input? I mean: (function (with-input-from-file "path/to/file.txt")). It is asking two arguments rather than one.
    – gibarian
    Nov 12 '18 at 16:40












  • In both of my examples I am passing two arguments. The first argument is the "path/to/file.txt", and the second argument is a lambda.
    – Alex Knauth
    Nov 12 '18 at 17:07










  • Thank you. This is what I have: "; with-input-from-file: contract violation ; expected: (-> any)"
    – gibarian
    Nov 12 '18 at 17:13










  • Can you update your question with a Minimal Complete example of what you've tried and what's going wrong with it?
    – Alex Knauth
    Nov 12 '18 at 17:18


















  • I do not see the point yet. Can I call the file as an input? I mean: (function (with-input-from-file "path/to/file.txt")). It is asking two arguments rather than one.
    – gibarian
    Nov 12 '18 at 16:40












  • In both of my examples I am passing two arguments. The first argument is the "path/to/file.txt", and the second argument is a lambda.
    – Alex Knauth
    Nov 12 '18 at 17:07










  • Thank you. This is what I have: "; with-input-from-file: contract violation ; expected: (-> any)"
    – gibarian
    Nov 12 '18 at 17:13










  • Can you update your question with a Minimal Complete example of what you've tried and what's going wrong with it?
    – Alex Knauth
    Nov 12 '18 at 17:18
















I do not see the point yet. Can I call the file as an input? I mean: (function (with-input-from-file "path/to/file.txt")). It is asking two arguments rather than one.
– gibarian
Nov 12 '18 at 16:40






I do not see the point yet. Can I call the file as an input? I mean: (function (with-input-from-file "path/to/file.txt")). It is asking two arguments rather than one.
– gibarian
Nov 12 '18 at 16:40














In both of my examples I am passing two arguments. The first argument is the "path/to/file.txt", and the second argument is a lambda.
– Alex Knauth
Nov 12 '18 at 17:07




In both of my examples I am passing two arguments. The first argument is the "path/to/file.txt", and the second argument is a lambda.
– Alex Knauth
Nov 12 '18 at 17:07












Thank you. This is what I have: "; with-input-from-file: contract violation ; expected: (-> any)"
– gibarian
Nov 12 '18 at 17:13




Thank you. This is what I have: "; with-input-from-file: contract violation ; expected: (-> any)"
– gibarian
Nov 12 '18 at 17:13












Can you update your question with a Minimal Complete example of what you've tried and what's going wrong with it?
– Alex Knauth
Nov 12 '18 at 17:18




Can you update your question with a Minimal Complete example of what you've tried and what's going wrong with it?
– Alex Knauth
Nov 12 '18 at 17:18


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259546%2fabout-with-input-from-file-command-on-scheme%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()