I am a beginner in CLIPS. I need to interact with the user using read function. My problem is if the user says Yes is different to YES and to yes.
I was looking a long time in the documentation but I couldn't find any to normalize my input. I try with things like upper or normalize but don't exist in CLIPS.
Look in the CLIPS 6.4 Basic Programming Guide (http://www.clipsrules.net/Documentation.html) under section 12.3, String Functions. Section 12.3.7, Converting a String to Uppercase, describes the upcase function, and section 12.3.8, Converting a String to Lowercase, describes the lowcase function.
CLIPS (6.4 2/9/21)
CLIPS> (lowcase (read))
Yes
yes
CLIPS> (upcase (read))
no
NO
CLIPS>
Related
How would I do something like the following in scheme?
printf("I went to the park at %d with %s", 4, "Bob");
The closest I have right now is:
(define TIME 4)
(define NAME "Bob")
(display "I went to the park at ") (display TIME) (display " with ") (display NAME) (display ".")
You can't do this in standard Scheme. Many Scheme implementations have a format procedure that is modeled on the Common Lisp format. For example, Chez Scheme has a pretty complete implementation of format, and also printf which is just a wrapper around format. I am so used to using format that I never think to use printf in lisps:
> (format #t "I went to the park at ~A with ~A~%" 4 "Bob")
I went to the park at 4 with Bob
> (printf "I went to the park at ~A with ~A~%" 4 "Bob")
I went to the park at 4 with Bob
Here format sends the output to the current output port when the first argument is #t; printf automatically sends output to the current output port. Common Lisp style format directives are prefixed with a tilde (~). The ~A, or aesthetic, directive prints objects in human-readable form, and is what you want most of the time. There are other directives for formatting numbers; I added the ~% directive, which emits a newline. Your original example did not include a newline, and printf, at least in C, does not add a newline at the end of output (ordinarily this is desirable). The format procedure should allow much more control over results than the mother of all printfs, namely C's fprintf.
The specific facilities for printing formatted output will depend on the implementation, but Chez Scheme, MIT Scheme, Gauche Scheme, and Guile all implement format. Chicken Scheme implements format, and also implements printf, fprintf, and sprintf which all use the same format directives as format. Racket has a host of formatted output procedures, including format, printf, and fprintf; all of these use Common Lisp style format directives, too.
You will have to consult the documentation of a specific implementation to understand which format directives are supported and how they work; the Chez Scheme documentation contains some information, but suggests consultation of the Common Lisp HyperSpec for complete documentation.
There are also SRFI-28 (Basic Format Strings) and SRFI-48 (Intermediate Format Strings) which provide some of this functionality to implementations that support them.
It really depends on what Scheme interpreter you're using. For example, in Racket you can use printf for a similar effect:
(printf "I went to the park at ~a with ~a" 4 "Bob")
=> I went to the park at 4 with Bob
Check the documentation for more formatting modifiers.
This is the Clips code that i am trying to arrange. I have some vector in de BH and I want to search among them, those which ?P is common between them. Moreover in the second vector to search the restriction is that ?E can only be that types. Please help me.
(defrule padre
(es-padre ?P ?H)
(?E & :(tigre | leopardo | jirafa | cebra | avestruz | pinguino | albatros) ?P)
=>
(assert (?E ?H))
)
Is there any solution implementing a Switch case, or the unique solution is making more rules?
You have multiple syntax errors, so it's hard to tell what you're trying to do. To start, the first field of a pattern or fact you're asserting can't be a variable. If you include examples in your question (such as the facts you're asserting and what should happen when your rule executes) it will make it easier to answer your question.
I am new to CLIPS development and I need to retrieve a rule body and store it in string in order to parse it. I tried to redirect the defrule stream but without success.
Is there any way to do it like that or does it exist a special command that I would have forgotten.
(defrule one
(fact a)
=>
(assert (fact b)))
(bind ?str (ppdefrule one))
one rule displaying but ?str is empty
Thank you for your time and consideration.
There's not a clean way to do this out of the box, but there's a C API for retrieving the text, so you could extend CLIPS with a user-defined function to allow you to do this. The alternative would be to use dribble-on/dribble-off to capture the output in a file, but this would also display the output to the screen each time you retrieved the rule text.
I noticed that there is no
format directive which would
call force-output/finish-output.
Why?
It does seem to be useful in user interaction, cf.
Lisp format and force-output.
E.g., ~= could translate to finish-output, and ~:= to force-output.
I don't think clear-output makes much sense in this context, but we
might map ~#= to it for completeness.
PS. Cf. CLISP RFE.
Summary from comp.lang.lisp:
An explanation from Steven Haflich
The language defines no portable way to extend the set of format
directives (other then ~/.../) but that's not really the issue here.
The real problem is that it is not well defined to call finish-output or similar functions at arbitrary places during printing.
If pretty-printing is in progress, the stream received by a
pprint-dispatch or print-object method may be an encapsulating stream --
one that delays output temporarily until it can make decisions about
white space and line breaks. (There are also potential problems if
finish-output were called inside a ~< justification, but that directive
is a hairball!) What would one expect finish-output to do if called
inside a pretty print operation? I don't think it's well defined.
The problem isn't particular to format, of course, but a directive for
finish-output from format would just add another sharp edge to the
language. finish-output etc. are only safe to call when completely
outside an actual or implied call to cl:write. Call it as a function
at an appropriate point in your code (where you know execution isn't
inside a nested write) so the intention is clear and you don't mess up
printer internals.
A suggestion from Rob Warnock
Actually, no changes to format are needed. Just add this function somewhere in the COMMON-LISP-USER package:
(defun fo (stream arg colon-p atsign-p &rest params)
(declare (ignore arg params))
(cond
(colon-p (force-output stream))
(atsign-p (clear-output stream))
(t (finish-output stream))))
Then:
(progn
(format t "enter var: ~/fo/" nil)
(read))
enter var: 456
456
The problems with this (portable!) approach are
verbosity (~/fo/ instead of ~=)
need to consume a format argument (nil in the example above)
So, I'm trying to make a "golf club recommendation system" for a 18 hole course. Now, after having defined the basic templates for the golfcourse, golf club and the golf player, I'm stuck due to the large search space this particular problem presents. So currently I have:
(defrule teeoff
?g <- (golfer (position "tee"))
=>
(retract ?g)
(assert (golfer (position "fairway") (Current_club "driver") (Yardage 650))
After this, the ball is on fairway and can have a combination of factors say, it can be on sand, it can be on rough or it could be on a normal green. My question is instead of making a rule for every possibility can I have one or two rules like:
(defrule makemove
?m <- (golfer (position ?x))
?go <- (golfcourse (obstacles ?$y)
=>
(assert (golfer (Current_club ?c)))
If not, then what alternatives do I have?
I suggest to you to design an object or attribute to manage the ground_material (sand, green, ...) and include it inside the rule.
You can write rule for each ground or use IF-THEN condition inside a single rule.
Hope this helps you.
bye
Nic