**In racket true is already a keyword and the question asks me to multiply if the value is true **
Image
In Racket the literals #t and #f will evaluate to the true and the false value respectively. Note that constructs such as if use the convention that any non-false value will count as a true value. Only the false value will trigger the else-branch.
Read more in the Guide: http://docs.racket-lang.org/guide/booleans.html
For source files in DrRacket that are written in #lang racket, thus the definition has that as the first line, we have the following documentation from the reference:
A #true, #t, #T followed by a delimiter is the input syntax for the
boolean constant “true,” and #false, #f, or #F followed by a delimiter
is the complete input syntax for the boolean constant “false.”
Since you can write in other languages in racket (that #lang racket) the other languages might have other literals to represent true and false booleans. eg. in Advanced student, a complete different language than #lang racket, you can use the variables true and false that when evaluated displays as #true and #false.
Related
I understand that both suppress evaluation of a symbol or expression. But the backtick is used for macro definitions while the apostrophe is used for symbols (among other things). What is the difference, semantically speaking, between these two notations?
Backticks allow for ,foo and ,#foo to interpolate dynamic parts into the quoted expression.
' straight up quotes everything literally.
If there are no comma parts in the expression, ` and ' can be used interchangeably.
A standard quote is a true constant literal and similar lists and list that end with the same structure can share values:
'(a b c d) ; ==> (a b c d)
A backquoted structure might not be a literal. It is evaluated as every unquote needs to be evaluated and inserted into place. This means that something like `(a ,#b ,c d) actually gets expanded to something similar to (cons 'a (append b (cons c '(d)))).
The standard is very flexible on how the implementations solves this so if you try to macroexpand the expression you get many different solutions and sometimes internal functions. The result though is well explained in the standard.
NB: Even though two separate evaluation produces different values the implementation is still free to share structure and thus in my example '(d) has the potential to be shared and if one would use mutating concatenation of the result might end up with an infinite structure.
A parallel to this is that in some algol languages you have two types of strings. One that interpolates variables and one that don't. Eg. in PHP
"Hello $var"; // ==> 'Hello Shoblade'
'Hello $var'; // ==> 'Hello $var'
I'm very very new in elisp and just started learning it. I have seen the following expressions in the document:
(1+ (buffer-size))
(+ 1 (buffer-size))
What do they mean? As I know elisp use prefix notation, so the second one should be correct one. But both of them can be executed without any errors. The first one is from the documentation of point-max function.
Thanks.
The token 1+ is an identifier which denotes a symbol. This symbol has a binding as a function, and so (1+ arg) means "call the 1+ function, with the value of arg as its argument). The 1+ function returns 1 plus the value of its argument.
The syntax (+ 1 arg) is a different way to achieve that effect. Here the function is named by the symbol +. The + function receives two arguments which it adds together.
In many mainstream programming languages popular today, the tokenization rules are such that there is no difference between 1+ and 1 +: both of these denote a numeric constant followed by a + token. Lisp tokenization is different. Languages in the Lisp family usually support tokens that can contain can contain digits and non-alpha-numeric characters. I'm looking at the Emacs Lisp reference manual and do not see a section about the logic which the read function uses to convert printed representations to objects. Typically, "Lispy" tokenizing behavior is something like this: token is scanned first without regard for what kind of token it is based on accumulating characters which are valid token constituents, stopping at a character which is not a token constituent. For instance when the input is abcde(f, the token that will be extracted is abcde. The ( character terminates the token (and stays in the input stream). Then, the resulting clump of characters abcde is re-examined and classified, converted to an object based on what it looks like, according to the rules of the given Lisp dialect. Across Lisp dialects, we can broadly depend on a token of all alphabetic characters to denote a symbol, and a token of all digits (possibly with a leading sign) to denote an integer. 1+ has a trailing + though, which is different!
Lets say i want to get the documentation for a function, I'd say
(documentation 'foo 'function)
but what if I only had foo and function as strings? E.g. "foo" and "function".
What would I have to do to them to make them usable as parameters to the documentation call?
[Side note: I'm using clisp, but I doubt that matters.]
Use FIND-SYMBOL, not INTERN. If you want to find documentation for an existing function, finding a symbol is enough. INTERN also creates symbols.
CL-USER > (find-symbol "SIN" "COMMON-LISP")
SIN
:EXTERNAL
Note that Common Lisp symbols are uppercase internally be default. Thus you need to use an uppercase string to find the corresponding symbol in the corresponding package.
Also note that there actually isn't something like a 'quoted variable'. You want to convert a string to a symbol.
Use INTERN to convert a string to a symbol. Make sure you uppercase the strings because, unlike with symbols, the reader will not do it for you:
(tested in SBCL):
* (documentation 'mapcar 'function)
"Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
return values."
* (documentation (intern "MAPCAR") (intern "FUNCTION"))
"Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
return values."
I am trying to compare the objects of two triples in a knowledgebase. I have connected the knowledgebase through ACL client (Allegro CL Enterprise edition 9.0) and written the following code:
(select (?s ?threshold ?NameMatch)
(q- ?s !mynamespace:hasNameMatch ?NameMatch)
(q- !mynamespace:hasNameMatch !mynamespace:hasThresholdValue ?threshold)
(setq return (if (>= (?NameMatch ?threshold "true" "false")))
(pprint ret))
My Knowledgebase includes the following triples
Subject Predicate Object
ns:hasAddressMatch ns:hasThresholdValue "60"
<../729277> :hasNameMatch "70"^^xsd:double
I am trying to compare the ?threshold with value "60" and ?NameMatch with value "70"^^xsd:double, and I expect the result "true". How can I do this?
In RDF data, things that aren't blank nodes or IRIs are literals. Literals may be a plain literal (a string with no additional information), a string with a language tag, or a lexical form (string part) with a datatype (an IRI).
In your data, the literal with lexical form "70" has a datatype which you've censored, but I assume is supposed to be xsd:double. I.e., the value is "70"^^xsd:double, which is the double precision floating point number 70. Your other literal value "60" is just a plain literal. It doesn't really make sense to compare those two values, since one is a number and one is a string.
You have two options though:
You could do a string comparison with the plain literal "60" and the lexical form of "70"^^xsd:double, in which case you'd use string< or string-lessp, depending on whether you want case sensitivity or not (since these are strings of digit characters, it probably doesn't matter).
You could assume that the plain literal "60" has a lexical form that's legal for an xsd:double, do the appropriate conversion, and then compare "60"^^xsd:double and "70"^^xsd:double as numbers with <.
I'd suggest that, if possible, you clean up your data (but this might not be an option if it's coming from somewhere else) so that you can do a numeric comparison with < as in (2), but without needing to do the conversion at comparison time.
In addition to those RDF concerns, your Lisp code also has some issues. if takes three arguments (the third is optional, though). The first is a test condition, and the second and third are forms that get evaluated depending on whether the first evalutes to true or not. In your code, if is only getting one argument:
(if (>= (?NameMatch ?threshold "true" "false")))
-------------------------------------------
This is trying to call >= with one argument, and that's the
(?NameMatch ?threshold "true" "false")
which would be a function call to the function named ?NameMatch with three arguments (?threshold, "true" and "false"). What you probably want here is:
(if (>= ?NameMatch ?threshold) "true" "false")
(Of course, you may still need to modify the test as described above.)
I read in the book "Land of Lisp", the author mentions syntax expression. Does that mean the ability to express syntax as a form of data? Is this the same as S-expression (symbolic expression)?
A symbolic expression is data which is serialized as known from Lisp. It uses symbols, strings, numbers, lists and more. Lists are written in the form of ( expression* ).
Thee author of Land of Lisp talks about syntax expressions and Lisp syntax expressions. Seems like this is something he invented (discovered?). ;-) He probably means an expression in Lisp syntax, where something like (walk right) is such an expression with the first element of the list being a verb.
In Common Lisp a valid expression of the programming language is called a Lisp form. So an s-expression can express all kinds of data, but not all s-expressions are valid Lisp programs. For example (defun) is not a valid Common Lisp program, since it lacks a function name and a parameter list - plus the optional declarations, documentation and implementation body: (defun foo ()).