How to provide full path in require in racket? - scheme

Trying to have access to functions from another file. Placed (provide (all-defined-out)) inside the other file. Now trying to actually refer to it using full path:
(require “C:\Users\functions.rkt”)
returns this error: #%require: bad require spec in: “C:Usersfunctions.rkt”
(require "C:\Users\functions.rkt")
returns this error: read-syntax: no hex digit following \U

There are two syntax errors in this snippet:
(require “C:\Users\functions.rkt”)
For starters, the double quote characters are incorrect. And you must escape the backslashes, and as Ryan points using file is mandatory. Try this:
(require (file "C:\\Users\\functions.rkt"))

Related

(Scheme) Unbound Variable when copy-pasting code

I'm copying the following Scheme code into a buffer file on emacs from a pdf:
(define (plural wd)
(if (equal? (last wd) ’y)
(word (bl wd) ’ies)
(word wd ’s)))
The initial formatting is as a long string, and I manually edit it to the format seen above. The file loads, but when I use the function I get the error:
*** Error:
unbound variable: |’y|
Current eval stack:
__________________
0 (equal? (last wd) |’y|)
1 (if (equal? (last wd) |’y|) (word (bl wd) |’ies|) (word wd |’s|))
When I manually type this code and load the file, however, the function runs no problem.
In what way is the pasting/editing of the code messing with the formatting of the code?
Is there a proper way to copy-paste code into a file? I tried formatting the code in a text editor before pasting into the buffer, but that didn't work either.
Thank you for your time and help.
It was already answered in the comments by Barmar, but this should enable you to complete your question, and help anybody else with the same problem in the future.
When you copy/pasted the code from the PDF, you did not copy a simple ASCII quote character '. Instead, you copied a "right single quotation mark" (unicode U+2019) ’. As this is not a reserved character in Scheme, it can be used as an identifier, and so what you expected to be the quoted symbol 'y was in fact the identifier ’y. The error was caused by there being no binding for the variable ’y.
A simple way of fixing this that does not required manually copying the code or fixing every quotation mark by hand is to find-and-replace ’ for ' (as long as you don't expect any ’ characters in your strings).

about racket : #lang not enabled

I've got to learn how-to-design-program for a while.But once I started to use my Emacs to learn htdp, I met some problem.
THE PROBLEM IS THAT:
I typed #lang racket , but it just show:
> stdin::7: read: #lang not enabled in the current context
context...:
/usr/share/racket/collects/racket/private/misc.rkt:87:7
> racket: undefined;
cannot reference undefined identifier
context...:
/usr/share/racket/collects/racket/private/misc.rkt:87:7
And I use 'require' to load path.
stdin::30: cannot open module file
module path: #<path:/Desktop/htdp/convert.rkt>
path: /Desktop/htdp/convert.rkt
system error: No such file or directory; errno=2
context...:
standard-module-name-resolver
/usr/share/racket/collects/racket/private/misc.rkt:87:7
Also it can not work.
Can you help me to solve it?
P.S my system is Fedora20.
When you're running a racket script from the console, you shouldn't need to define the language on the first line. This flag
racket -I <language>
can be used to specify a language when running from the command line. #lang racket should be the default, so just remove the line and run your script from the command line using the racket command.
from the link https://docs.racket-lang.org/guide/Module_Syntax.html#%28part._hash-lang%29
it says:
The #lang at the start of a module file begins a shorthand for a module form, much like ' is a shorthand for a quote form. Unlike ', the #lang shorthand does not work well in a REPL, in part because it must be terminated by an end-of-file, but also because the longhand expansion of #lang depends on the name of the enclosing file.

Parsing a string containing "\" in Racket

I am trying to extract tokens from a Windows path string that contains "\".
I have tried string-split and string->list, but one of the path elements starts with a "d", so I get a "read: unknown escape sequence \d in string" error.
Edit: The paths have the form: "\\aaaa\bbbb\cccc\dddd\eeee....". I need to extract, for example, the "eeee" part. The "string->" functions choke on the "dddd" element, with the above error. On Windows 7, Racket 5.3.3. For example : (string-split path), (string->list path)
Backslash is used as escape in strings. If you do (read-line) and enter one backslash the result is "\\".
For hardcoded strings to test your programs you need to write like this "\\\\aaaa\\bbbb\\..."

How to insert white space in a file path in .emacs file?

I'm trying use mit-scheme in emacs but I can't get passed that problem...
The problem is I don't know how to add white spaces in a file path in .emacs file
So far I've tried
(setq scheme-program-name
"/Applications/MIT:GNU\ Scheme.app/Contents/Resources/mit-scheme")
(require 'xscheme)
and
(setq scheme-program-name
"/Applications/MIT:GNU Scheme.app/Contents/Resources/mit-scheme")
(require 'xscheme)
but the outputs I get is
Can't exec program: /Applications/MIT:GNUScheme.app/Contents/Resources/mit-scheme
Can't exec program: /Applications/MIT:GNU
because there is a white space missing in the path..
I think this is not possible using the existing functions, due to the way scheme gets called. The function run-scheme contains an explicit call to the function split-string-and-unquote on the scheme program name. As a consequence, the path to the scheme program will always be split at the first space. This means it is impossible to use a path with a space in it.
This is a bug that should be reported to the maintainers I think.

Pattern matching error in Scheme

I have written a function match-rewriter that is just match-lambda except that it returns its argument if no match is found. match-rewriter is part of a larger function. Here is a portion of the code:
((match-rewriter
(`(PARAMS: (,<arg>))
`(Success))
(`(,<func> . ,<args>)
`(Failure))
)ls)
This function call:
(annotate '(PARAMS: (y))
returns Failure
In another post someone pointed out that this works:
#lang racket
(match `(PARAMS: (y))
[`(PARAMS: (,var)) 'yep]
[otherise 'nope])
returning yep
I verified that it works but I can't figure out why the same pattern isn't being matched in match-rewriter.
Strangely, if I just run this code manually substituting '(PARAMS: (y)) for "ls" it works. Which really confuses me.
Any advice is appreciated.

Resources