I am trying to retract all facts of form:
(<something> task-error)
To do this I wrote the rule:
(defrule retract-task-error "retract task error"
(declare (salience -1000))
?f <- (?n task-error)
=>
(retract ?f)
)
But it does not work with error:
[PRNTUTIL2] Syntax Error: Check appropriate syntax for the first field of a pattern.
Is it ever possible to accomplish this task with CLIPS or do I need a code rearrangement to avoid matching first fields of facts?
The first field of a pattern must be a symbol. The simplest solution would probably be adding a common symbol (such as task) to the beginning of all facts and patterns which can contain task-error:
(defrule retract-task-error "retract task error"
(declare (salience -1000))
?f <- (task ?n task-error)
=>
(retract ?f)
)
Related
There is a list of "спсВыбора":
("ФайлыКаталоги" "Клиент Проверка Существования Каталога" "Клиент Проверка Существование Файла" "СтандартныеСтруктурыМодуля" "стндОбрОтв" "элКлючаОтветУспехОбрОтв" "элКлючаОтветОшибкаОбрОтв" "элКлючаОтветПроцедура" "элКлючаОтветМодуль" "стндОтчОтв")
To sort, use the command:
(setq спсВыбора (sort спсВыбора (lambda (a b) (string> a b))))
As a result, the list of "спсВыбора":
("элКлючаОтветУспехОбрОтв" "элКлючаОтветПроцедура" "элКлючаОтветОшибкаОбрОтв" "элКлючаОтветМодуль" "стндОтчОтв" "стндОбрОтв" "ФайлыКаталоги" "СтандартныеСтруктурыМодуля" "Клиент Проверка Существования Каталога" "Клиент Проверка Существование Файла")
Sorting takes into account the separate order of lower and upper case letters. Tell me how to sort the list by removing the case order. Example:
"caB" => "aBc"
Use string-collate-lessp as the predicate:
string-collate-lessp is a built-in function in ‘src/fns.c’.
(string-collate-lessp S1 S2 &optional LOCALE IGNORE-CASE)
Return t if first arg string is less than second in collation order.
Symbols are also allowed; their print names are used instead.
This function obeys the conventions for collation order in your
locale settings. For example, punctuation and whitespace characters
might be considered less significant for sorting:
(sort '("11" "12" "1 1" "1 2" "1.1" "1.2") 'string-collate-lessp)
=> ("11" "1 1" "1.1" "12" "1 2" "1.2")
The optional argument LOCALE, a string, overrides the setting of your
current locale identifier for collation. The value is system
dependent; a LOCALE "en_US.UTF-8" is applicable on POSIX systems,
while it would be, e.g., "enu_USA.1252" on MS-Windows systems.
If IGNORE-CASE is non-nil, characters are converted to lower-case
before comparing them.
To emulate Unicode-compliant collation on MS-Windows systems,
bind ‘w32-collate-ignore-punctuation’ to a non-nil value, since
the codeset part of the locale cannot be "UTF-8" on MS-Windows.
If your system does not support a locale environment, this function
behaves like ‘string-lessp’.
As you noticed, string< is case-sensitive. My suggestion in your case to obtain case-insensitive sorting would be to upcase/downcase operands of that comparator, so that it is effectively case-insensitive:
(setq спсВыбора (sort спсВыбора (lambda (a b) (string> (downcase a) (downcase b)))))
Note that that is reverse-alphabetical per your example.
I need to format a string, but even if I copy a seemingly correct code, the CLIPS interpreter signals me an error.
(format nil "Integer: |% ld|" 12)
“I expect the output of "Integer: |12|" but the CLIPS interpreter signals me an error.
There should be no space between the % character and the format flag. Also the character 'l' is not a valid format flag.
CLIPS> (format nil "Integer: |%d|" 12)
"Integer: |12|"
CLIPS>
Is there a way to do something like the following?
(format t "~{~va~}" '("aa" "bb" "cc") 4)
I need to iterate through a list. Each element of that list should be padded with a variable number of spaces (specified at runtime, so I cannot use "~4a").
Or more generally, is there a way to refer to a specific argument in the argument list of FORMAT?
By nesting format function, you can do what you want.
(format t (format nil "~~{~~~Aa~~}" 4) '("aa" "bb" "cc"))
;; returns: aa bb cc
Here the inner format directive:
The nil as first argument, format returns a string.
(format nil "~~{~~~Aa~~}" 4)
;; returns: "~{~4a~}" - and this is exactly what you want to give
;; to the outer `format` as second argument!
You can of course write a function for this:
(defun format-by-padding-over (lst padding)
(format t (format nil "~~{~~~Aa~~}" padding) lst))
And then:
(format-by-padding-over '("aa" "bb" "cc") 4)
;; aa bb cc
;; NIL
I learned this trick here from #Sylwester (many thanks!).
You could also interleave the list with repetitions of the padding:
(format t "~{~va~}"
(mapcan (lambda (element)
(list 4 element))
list))
You can build the format control string using nested format functions, but then you have to take care about escaping tildes. When working with regular expressions (using CL-PPCRE), one can define regular expressions using trees, like (:alternation #\\ #\*), which helps preventing bugs and headaches related to escaping special characters. The same can be done with format strings, using format-string-builder, available in Quicklisp:
(lambda (v)
(make-format-string `((:map () (:str ,v)))))
Returns a closure, which can be used to build format strings:
(funcall * 10)
=> "~{~10a~}"
I am writing a function that takes stringA and stringB as parameters and compares the first character of stringB with the last character of StringA. If they are equal, then the function returns true, else false is returned.
I have nearly the whole function ready, however I can't find a way to take the last character of stringA because its length is unknown. I checked the documentation and I found nothing. Any suggestions?
(cond
[(string=? (substring stringA ???) (substring stringB 0 2))"True"]
[else "False"])
You can get the last character position of a string using string-length (or rather one less than):
(string-ref str (sub1 (string-length str)))
Note that a character is different from a string of length 1. Thus the correct way to extract a character is with string-ref or the like, rather than substring.
It seems Chris answered your question. Just a reminder, to use the string-ref, which returns a character, you should use the comparison function char=? (or equal?).
I'd like to add another solution which I find more elaborate, but requires to download a collection from the planet racket (after installing package collections). Using the collections package, you can use the same function with any collection rather then just strings, using the (last ..) and (first ..) functions of the module.
(require data/collection)
(let ([stringA "abcd"]
[stringB "dcba"])
(cond
[(equal? (last stringA)
(first stringB)) "True"]
[else "False"]))
You could also use the SRFI-13 function string-take-right, which returns the last n characters of the argument string as a string.
every language has a length function for a string. in Racket I found this :
https://docs.racket-lang.org/reference/strings.html#%28def.%28%28quote.~23~25kernel%29._string-length%29%29
there is this : string-length str
so just run that it will give you the length and then you can extract the last character
I'm currently looking into Scheme, and the way I have understood it, procedures can take an arbitrary number of arguments.
I have been trying to play around with this, but I'm struggling to grasp the concept.
For instance, say I want to write a welcome message, based on information provided by the user.
If user provides a first and last name, the program shout write:
Welcome, <FIRST> <LAST>!
;; <FIRST> = "Julius", <LAST>= "Caesar"
Welcome, Julius Caesar!
Otherwise, the program should refer to a default value, specified as:
Welcome, Anonymous Person!
I have the following outline for my code, but struggling with how to finalise this.
(define (welcome . args)
(let (('first <user_first>/"Anonymous")
('last <user_last>/"Person"))
(display (string-append "Welcome, " first " " last "!"))))
Example usage:
(welcome) ;;no arguments
--> Welcome, Anonymous Person!
(welcome 'first "John") ;;one argument
--> Welcome, John Person!
(welcome 'first "John" 'last "Doe") ;;two arguments
--> Welcome, John Doe!
Any help is highly appreciated!
In Racket, the way to do this would be using keyword arguments. You can define a function with keyword arguments my writing #:keyword argument-id when declaring the arguments:
(define (welcome #:first first-name #:last last-name)
(display (string-append "Welcome, " first-name " " last-name "!")))
Which you can call like this:
> (welcome #:first "John" #:last "Doe")
Welcome, John Doe!
However, what you want is to make them optional. To do that, you can write #:keyword [argument-id default-value] in the argument declaration.
(define (welcome #:first [first-name "Anonymous"] #:last [last-name "Person"])
(display (string-append "Welcome, " first-name " " last-name "!")))
So that if you don't use that keyword in a certain function call, it is filled with the default value.
> (welcome)
Welcome, Anonymous Person!
> (welcome #:first "John")
Welcome, John Person!
> (welcome #:first "John" #:last "Doe")
Welcome, John Doe!
> (welcome #:last "Doe" #:first "John")
Welcome, John Doe!
#Alex Knauth's answer is great. That's something I didn't know about.
Here's an alternative, though it's not quite as flexible
(define (welcome (first "Anonymous") (last "Person"))
(displayln (string-append "Welcome, " first " " last "!")))
This works pretty nicely with your basic requirements
> (welcome)
Welcome, Anonymous Person!
> (welcome "John")
Welcome, John Person!
> (welcome "John" "Doe")
Welcome, John Doe!
However, Alex's solution has two distinct advantages.
The arguments can be called in either order
The last name can be specified without the first name