Racket Scheme: Include externel rkt file with dynamic file name - include

I want to load external rkt file in racket scheme from a parameter of a function.
E.G.,
(define (test fileName)
(include fileName)
)
However, I am getting error indicating that fileName is not a pathname string, file' form, orlib' form.
Is there a way to fix this or is there another better way to include a file from dynamic filename?

The best way to do this is to make the external file a module, and use dynamic-require.

Related

How to get the path of a elisp program file from inside of the file? [duplicate]

This question already has an answer here:
How to refer to the file currently being loaded in Emacs Lisp?
(1 answer)
Closed 3 years ago.
When I edit a file, say current-file-path.el
I can have the following code:
(message (format "Here is the path of the current file %s" (buffer-file-name)))
when execting the statement in the buffer of the file when the file is open in a buffer, I got the correct message
of the path of the file:
Here is the path of the current file /home/yubrshen/tmp/current-file-path.el
However, if I just load the file, then the message becomes:
Here is the path of the current file nil
What would be the proper way to find out the path of the file where my code is?
Actually, I'm interested in knowing the directory of the file of my program so that I can load the other files at the same directory through the program.
You seem to be looking for symbol-file.
Of course, not every symbol is defined in Lisp code loaded from a named file; some are defined in the C source code for Emacs, and some are defined interactively by yourself.
There is also no guarantee that your data files will be packaged in the same location as your source code, so what you are describing should probably be implemented with a package variable instead.
(defvar foo-directory (file-name-directory load-file-name)
"*Directory for data files belonging to package \`foo'.")
This should probably use defcustom actually, but I'd have to guess too many things about your code to create a meaningful example.

How can I read file loaded in a variable in guile?

I am new to to guile and scheme and what I am trying to do right now is take a scheme file (file.scm) and load it up into a variable so I will be able to parse it, and I am having trouble finding how to do this anywhere.
What I have right now is
(define var (load "file.scm")) ; loads file scheme
but I am unsure how to start reading the lines.
load parses and runs the scheme code in a file. If you just want to read a file, use open-input-file.
(define file (open-input-file "file address here"))
(display (read-line file))
If you just want to read an entire file as a string, there's a function for that in the textual-ports module. You'd use it something like:
(define contents (call-with-input-file "file.txt" get-string-all))
(You can use call-with-input-file and with-input-from-file to avoid having to manually open and close a file port, which is handy)

Validate for File naming scheme

I have a file that drops into a folder location. I'm looking for a file named like this.
ID _Data_Tape_CCYYMMDD_hhmmss.txt
How do i accomplish this?
How about using a regular expression? in Racket, this will match the given file name:
(define name "42_Data_Tape_11220331_234532.txt")
(regexp-match #px"\\d+_Data_Tape_\\d{8}_\\d{6}.txt" name)
=> '("42_Data_Tape_11220331_234532.txt")
(regexp-match #px"\\d+_Data_Tape_\\d{8}_\\d{6}.txt" "something-else")
=> #f
To list all the files in a directory use directory-list or an equivalent procedure in your interpreter, then match their names against the above regular expression - and there, you found the files with the expected name.

How to load file from dir using Racket?

I'm trying to set a dir to load files from using racket. I want to set the dir and then use command (load "extract.rktl") to load the file.
I'm on windows environment.
Command I'm trying is :
(add-to-list 'load-path ("c:/Users/racket/")
I receive error :
add-to-list: undefined;
cannot reference undefined identifier
context...:
the dir c:\Users\racket exists. Are commands correct ?
Update : this helped : How do I include files in DrScheme?
In Racket, path is a type and strings are not paths. So convert the name of a path with string->path.
(define default-dir
(string->path "c:\\user\\racket"))
Notes:
The Windows separator character '\' must be escaped as '\'.
Many Racket functions that act on paths will implicitly convert strings to paths without an explicit call to string->path.
However string operations cannot be permed on path objects.
>(string-split default-dir "\\")
string-split: contract violation
expected: string?
given: #<path:c:\user\racket>
Alternatively, a GUI can be used:
> (require racket/gui)
> (define my-file (get-file))
> my-file
#<path:/home/ben/Documents/racket/my-module.rkt>

How to append to a file using Scheme?

I am using TinyScheme (actually Script-Fu in GIMP), and cannot find a good way to open a file and append a line of text to it.
I am trying to log some info to the file for debugging, and transcript-on doesn't seem to be implemented...
Right now I am scraping along by reading the entire file (one character at a time!), concatenating that into a string, concatenating my text to the end of that, then writing it all out to the file again.
There must be a better way!
It's going to be something like
(open-file "myfile" (file-options append))
You want to look up the file-options function.
Update
Here's the guile version:
(open-file "myfilename.dat" "a")
Just had the same exact problem in GIMP and decided to use open-input-output-file. My solution is something like this:
(let* ((out (open-input-output-file "file.txt") ))
(display "hello world" out)
(newline out)
(close-output-port out))
Went through the TinyScheme source and checked that this function actually calls fopen with "a+". The file is open for reading and writing. For our purposes we only want to write, so just ignore reading.
I am writing a Script-Fu to write the values of gimp-selection-bounds to a file.

Resources