Pattern matching error in Scheme - syntax

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.

Related

How to provide full path in require in racket?

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"))

Regex not matching string in scheme but works on other platform

I am running string-match using the pattern [ \[\]a-zA-Z0-9_:.,/-]+ to match a sample text Text [a,b]. Although the pattern works on regex101, when I run it on scheme it returns #f. Here is the regex101 link.
This is the function I am running
(string-match "[ \\[\\]a-zA-Z0-9_:.,/-]+" "Text [a,b]")
Why isn't it working on scheme but works eleswhere? Am I missing something?
After discussing the issue on the guile gnu mailing list, I found out that Guile's (ice-9 regex) library uses POSIX extended regular expressions. And this flavor of regular expression doesn't support escaping in character classes [..], hence that's why it wasn't matching the strings.
However, I used the following function as a workaround and it works:
(string-match "[][a-zA-Z]+" "Text[ab]")
I don't see anything wrong with your regular expression syntax as it is quoted correctly so I assume there must be a bug in Guile, or the regexp library it uses, where \] just isn't interpreted the correct way inside brackets. I found a workaround by using the octal code point values instead:
(string-match "[A-Za-z\\[\\0135]+" "Text [a,b]")
; ==> #("Text [a,b]" (0 . 4))
Your regular expression isn't very good. It matches any combination of those chars so "]/Te,3.xt[2" also matches. If you are expecting a string like "Something [something, something]" I would rather have made /[A-Z][a-z0-9]+ [[a-z0-9]+,[a-z0-9]+]/ instead. eg.
(define pattern "[A-Z][a-z0-9]+ \\[[a-z0-9]+,[a-z0-9]+\\]")
(string-match pattern "Test [q,w]") ; ==> #("Test [q,w]" (0 . 10))
(string-match pattern "Be100 [sub,45]") ; ==> #("Be100 [sub,45]" (0 . 14))

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.

Scheme pattern matching with match-lambda

I am writing a function called annotate that uses match-lambda, often with recursive calls to annotate. Here is one of the pattern matches:
(`(lambda (,<param1> . ,<params>) ,<stmts>)
`(CLOSURE ENV (,<param1> . ,<params>) `(lambda (ENV) ,(map annotate ,(list-append `(,<param1> . ,<params>) `(,<stmts>))))))
list-append just makes new lists out of its two arguments. The problem is that when this pattern matches it returns something like:
'(CLOSURE
ENV
(x)
`(lambda (ENV)
,(map
annotate
(<results of list-append>))))
Specifically, ",(map annotate" prints literally rather than being evaluated -- even though it is being unquoted. Other patterns within the function appear to use the exact same syntax without this issue. Also, the unquoted function list-append executes with no problems.
Any advice is appreciated.
You have nested backquotes: you have one in front of CLOSURE and then a second one in front of the second lambda without a comma in between: notice the literal backquote in the middle of your output. I think removing the backquote before the second lambda will fix the problem.

What would the following display in scheme?

I have tried this expression on a few (online) scheme interpreters/parsers and sometimes get different answers. For the following expression:
(display "okkk") \; "ok;" ;"ok"
What would it display and/or return? Why? For example:
Are \ acceptable outside of an s-expression, and do they escape the next character?
How is a string interpreted outside an expression, or is that invalid?
That probably depends on what Scheme syntax your implementation may support.
One might for example expect:
(display "okkk") -> displays okkk
\; -> error: unbound variable
"ok;" -> displays nothing, but returns the string
;"ok" -> end of line comment

Resources