Not sure what this line does - lisp

(or (parse-integer (prompt-read "Rating") :junk-allowed t) 0)
This line confuses me quite a bit. the full program is here if you need it to follow: http://paste.lisp.org/display/124929
'Parse-integer' will turn a string into and integer right? if possible. And ':junk-allowed t' makes it accept junk strings somehow right?
Not sure what the 'or' and the 0 at the end are though.
Thanks.

or goes through the forms passed to it, evaluates them in order until it finds one that does not evaluate to nil, and returns that result. So this will return the result of parse-integer if that call succeeds in parsing an integer, or 0 if not.

Related

Is there an alternative of and() in Matlab that does not check syntax and returns a false as soon as the first false is found?

Is there a convenient alternative of and() in Matlab that does not check existence, number of input or output arugments, and returns a false as soon as the first false is found without evaluating the expressions in subsequent inputs?
For example, I would like
and(0,a),
and(0,error()),
to both return false as opposed to returning error messages. Once an earliest input argument returns false, I have no use of subsequent input arguments and I am happy to ignore syntax errors. But Matlab isn't.
(The more likely scenario for me is that the false case of preceding inputs cover any syntax errors in later inputs.)
Is there a way around this? If I write an alternative of and() with a (Matlab) loop on varargin, will the alternative be slower?
Using the && operator solves your problem,
0 && a
0 && error()
will return
ans =
logical
0
Of course, even when a is undefined.
Caveat: and() can take (syntax checked) array arguments while && cannot. The different answers and comments in this question explain in more details.
The MATLAB interpreter (just like any other interpreter I’ve come across) parses all the input arguments to a function before calling the function. The function is passed the results of evaluating the arguments. Therefore, it is not possible to have a function control which of its input arguments are parsed.
There is no functional equivalent to &&, the short-circuit logical AND. The function and is equivalent to & and does not short-circuit.

How do I write a perl6 macro to enquote text?

I'm looking to create a macro in P6 which converts its argument to a string.
Here's my macro:
macro tfilter($expr) {
quasi {
my $str = Q ({{{$expr}}});
filter-sub $str;
};
}
And here is how I call it:
my #some = tfilter(age < 50);
However, when I run the program, I obtain the error:
Unable to parse expression in quote words; couldn't find final '>'
How do I fix this?
Your use case, converting some code to a string via a macro, is very reasonable. There isn't an established API for this yet (even in my head), although I have come across and thought about the same use case. It would be nice in cases such as:
assert a ** 2 + b ** 2 == c ** 2;
This assert statement macro could evaluate its expression, and if it fails, it could print it out. Printing it out requires stringifying it. (In fact, in this case, having file-and-line information would be a nice touch also.)
(Edit: 007 is a language laboratory to flesh out macros in Perl 6.)
Right now in 007 if you stringify a Q object (an AST), you get a condensed object representation of the AST itself, not the code it represents:
$ bin/007 -e='say(~quasi { 2 + 2 })'
Q::Infix::Addition {
identifier: Q::Identifier "infix:+",
lhs: Q::Literal::Int 2,
rhs: Q::Literal::Int 2
}
This is potentially more meaningful and immediate than outputting source code. Consider also the fact that it's possible to build ASTs that were never source code in the first place. (And people are expected to do this. And to mix such "synthetic Qtrees" with natural ones from programs.)
So maybe what we're looking at is a property on Q nodes called .source or something. Then we'd be able to do this:
$ bin/007 -e='say((quasi { 2 + 2 }).source)'
2 + 2
(Note: doesn't work yet.)
It's an interesting question what .source ought to output for synthetic Qtrees. Should it throw an exception? Or just output <black box source>? Or do a best-effort attempt to turn itself into stringified source?
Coming back to your original code, this line fascinates me:
my $str = Q ({{{$expr}}});
It's actually a really cogent attempt to express what you want to do (turn an AST into its string representation). But I doubt it'll ever work as-is. In the end, it's still kind of based on a source-code-as-strings kind of thinking à la C. The fundamental issue with it is that the place where you put your {{{$expr}}} (inside of a string quote environment) is not a place where an expression AST is able to go. From an AST node type perspective, it doesn't typecheck because expressions are not a subtype of quote environments.
Hope that helps!
(PS: Taking a step back, I think you're doing yourself a disservice by making filter-sub accept a string argument. What will you do with the string inside of this function? Parse it for information? In that case you'd be better off analyzing the AST, not the string.)
(PPS: Moritz++ on #perl6 points out that there's an unrelated syntax error in age < 50 that needs to be addressed. Perl 6 is picky about things being defined before they are used; macros do not change this equation much. Therefore, the Perl 6 parser is going to assume that age is a function you haven't declared yet. Then it's going to consider the < an opening quote character. Eventually it'll be disappointed that there's no >. Again, macros don't rescue you from needing to declare your variables up-front. (Though see #159 for further discussion.))

Checking for an empty ScriptParameter containing text

I am working on FileMaker 14 and am using ScriptParameters. In my parameters I use text, not numbers. I am looking for a way to calculate whether a parameter is empty or not, but the code below returns a 0 value (false) if there is text in ScriptParameter or not:
If [ isEmpty ( Get ( ScriptParamter ) ) ]
The help docs in FileMaker do say that IsEmpty will return a value of 0 if the argument is text. So obviously I am looking for a different calculation or something. Ideas?
Thanks
GW
FileMaker's IsEmpty() function absolutely returns TRUE (1) if a text argument is truly empty. If you're getting false, there's something in your script parameter.
It looks like you might be passing multiple values in your script parameter (based on your use of the plural "script parameters"). If so, your script parameter will never evaluate to true, because of the presence of one or more carriage returns. If you need to pass multiple values, you'll need to first extract a given value using GetValue( Get(ScriptParameter); ), which gets the nth line of text without a trailing carriage return, then test the extracted value.
If that's not right (and you're only passing a single value), this likely means you're passing invisible characters in your script parameter you're not aware of. To test, you can use Length( Get(ScriptParameter) ) to test how many characters FileMaker "sees" in your script parameter. To quickly get a handle on invisible characters, you might use Code( Get(ScriptParameter) ), which will return the ASCII codes for each character. This can quickly reveal if you have spaces, tabs, returns, etc.
The help docs in FileMaker do say that IsEmpty will return a value of
0 if the argument is text.
No, that's most certainly not true. IsEmpty() will return a value of
0 if the argument is empty. If your parameter is of type text, then IsEmpty() will return 0 if and only if the parameter is a string of zero length.

Command line processing in Racket contains embedded void

What about command-line processing in racket do I not understand?
For example, I just want to process the first argument as foo.html.
From the command-line I run it as:
racket cmd.rkt foo.html
Unfortunately that just returns:
foo.html'#(#<void>)
Here's the code for cmd.rkt:
(for/vector ([i (current-command-line-arguments)])
(display i))
for/vector isn't called that because it iterates over vectors, it's called that because it accumulates the results of its body expression into a vector. So for each commandline argument it evaluates the display call, which prints the argument and returns #<void>, and accumulates the result into a vector of void values.
Use for instead and the problem will go away.
Let's see what the code is doing. With this command...
racket cmd.rkt foo.html
... You're telling the interpreter: run cmd.rkt and pass a single parameter, the string "foo.html".
In the script, this code...
(for/vector ([i (current-command-line-arguments)])
(display i))
...Is iterating over the command line arguments (a single one in the example), displaying each one in turn. Do notice that display returns #<void> as its value, and for/vector creates a vector with all the returned values in the iteration, so naturally this is the output:
foo.html'#(#<void>)
The first part, foo.html is the result of displaying the single command line argument that was passed. The second part, '#(#<void>) is a vector with a single element, #<void>, which as mentioned before, is the result of calling display.
Finally, as has been mentioned in the other answers: if you only intended to print all of the received command line arguments, then avoid using for/vector - you don't want to create a new vector, only traverse and print the arguments and a simple for will suffice. This should work, and includes #GregHendershott's recommended optimization regarding the use of in-vector:
(for ([i (in-vector (current-command-line-arguments))])
(display i))

How can I get a substring of a string in Emacs Lisp?

When I have a string like "Test.m", how can I get just the substring "Test" from that via elisp? I'm trying to use this in my .emacs file.
One way is to use substring (or substring-no-properties):
(substring "Test.m" 0 -2) => "Test"
(substring STRING FROM &optional TO )
Return a new string whose contents are a substring of STRING. The
returned string consists of the characters between index FROM
(inclusive) and index TO (exclusive) of STRING. FROM and TO are
zero-indexed: 0 means the first character of STRING. Negative values
are counted from the end of STRING. If TO is nil, the substring runs
to the end of STRING.
Stefan's answer is idiomatic, when you just need a filename without extension. However, if you manipulate files and filepaths heavily in your code, i recommend installing Johan Andersson's f.el file and directory API, because it provides many functions absent in Emacs with a consistent API. Check out functions f-base and f-no-ext:
(f-base "~/doc/index.org") ; => "index"
(f-no-ext "~/doc/index.org") ; => "~/doc/index"
If, instead, you work with strings often, install Magnar Sveen's s.el for the same reasons. You might be interested in s-chop-suffix:
(s-chop-suffix ".org" "~/doc/index.org") ; => "~/doc/index"
For generic substring retrieval use dkim's answer.
In your particular case, you might like to use file-name-sans-extension.
Probably the most flexible option (although it's not clear if you need flexibility) would be to use replace-regexp-in-string:
See C-hf replace-regexp-in-string RET
e.g.:
(replace-regexp-in-string "\\..*" "" "Test.m")