why is this racket code giving me a function call error? - function

i am working through this online book //htdp.org/2021-11-15/Book/index.html to learn the racket.as i progressed through the book i came across this code - this isnt my code and all it had asked of me was to change the ... with a string which i did - check below - first one is from the site - second one is the one i modified with strings as as specifed
(define current-color ...)
(define next-color
(if (string=? "green" current-color) "yellow" ...))
(define current-color "red")
(define next-color
(if (string=? "green" current-color) "yellow" "amber"))
and when i run it i get this error
(next-color "green")
function call: expected a function after the open parenthesis, but found a variable
am i missing something please help me out

next-color, defined as:
(define next-color
(if (string=? "green" current-color) "yellow" "amber"))
is variable, not function, so you can't call it like this: (next-color "green"). If you want to get its value, just type into REPL:
> next-color

Related

Script-Fu Problem with a simple function that renames the selected layer

I am writing a simple script to rename the selected layer.
Here is the code:
(script-fu-register
"script-fu-renaming" ;code name
"Renaming Function" ;name
"This is for a question for Stack Overflow" ;description
"Me" ;author
"copyright 2020, Me" ;copyright
"Wednesday 8/Jul/2020" ;date
"" ;?
)
(define (script-fu-renaming)
(gimp-item-set-name (gimp-image-get-active-layer 1) "屈")
)
But when I execute it on the Script-Fu console, through this "(script-fu-renaming 0)", I got the following error: "Error: ( : 32595) Invalid type for argument 1 to gimp-item-set-name".
So my question would be what is the code to do what I explained above without getting errors?
Like most GIMP functions gimp-image-get-active-layer returns a list, so you need to extract the first element using car :
(gimp-item-set-name (car (gimp-image-get-active-layer 1)) "?")

Clojure - How to put a list and a function inside a defn function

I am a newbie and making some exercises. How can I put a def with a list of sentences and a randomizer function inside a defn function? How does that work?
(def list["test1", "test2", "test3"]) - works fine
(rand-nth list) - works fine
How do I put it inside a function defn?
Thanks for help.
IIUC you just want to reimplement rand-nth, no?
(defn wrapped-rand-nth [a-list]
(rand-nth a-list))
If you want the list to be static (non-changing)
(defn randomize []
(rand-nth ["test1" "test2" "test3"]))
works, but it creates the vector upon each call, a better way is to
(let [the-list ["test1" "test2" "test3"]]
(defn randomize []
(rand-nth the-list)))

json get prolog predicate

I'm tryung to create this predicate in prolog:
The predicate json_get/3 can be defined as: json_get(JSON_obj, Fields, Result). which is true when Result is recoverable by following
the chain of fields in Fields (a list) starting from JSON_obj. A field
represented by N (with N a major number o equal to 0) corresponds to
an index of a JSON array.
Please help me to understand to follow the chain of fields.
Thanks
edit1:
Of course, so json object looks like this '{"name" : "Aretha", "surname" : "Franklin"}'.
if i call json_parse predicate to this object prolog show me this
json_obj([(”name”, ”Aretha”), (”surname”, ”Franklin”)]), we call this obj O.
with json_get i need to extract from O the name in this way, json_get(O, ["name"], R)
edit2:
with someone's help this is the predicate now:
json_get(json_obj(JSON_obj), Field, Result) :-
memberchk((Field,Result), JSON_obj).
json_get(JSON_obj, Fields, Result) :-
maplist(json_get(JSON_obj), Fields, Result).
so now the problem is nested list.
For example with this input
json_parse('{"nome" : "Zaphod",
"heads" : ["Head1", "Head2"]}', Z),
json_get(Z, ["heads", 1], R).
the output will should be R = "Head2" but the predicate doesn't extract the field and fail.
edit3:
this is the output of json_parse
json_obj([("nome", "Zaphod"), ("heads", json_array(["Head1", "Head2"]))]).
How about this
json_get(json_obj(Obj),[F|Fs],Res) :-
member((F,R),Obj),
json_get(R,Fs,Res).
json_get(json_array(Is),[N|Fs],Res) :-
nth1(N,Is,R),
json_get(R,Fs,Res).
json_get(Res,[],Res).
This produces Head1 not Head2 in your 2nd example. Please explain how that is supposed to work, if you did not just make a typo. (If it is zero-based you can just change nth1/3 to nth0/3.)

Common Lisp: How to use macro to generate S-expressions for CL-WHO?

Say I have defined a macro for CL-WHO:
(defmacro test-html (&body body)
`(with-html-output-to-string (*standard-output* nil :PROLOGUE t :indent t)
(:html
(:body
,#body))))
Then:
(test-html (:h1 "hallo"))
Gives (first line removed):
"<html>
<body>
<h1>
hallo
</h1>
</body>
</html>"
As expected. Now I have defined a function to generate the s-expression to be used by CL-WHO:
(defun test-header (txt)
`(:h1 ,txt))
When called with "hallo" returns
(:h1 "hallo")
BUT now when I call
(test-html (test-header "hallo"))
It returns:
"<html>
<body>
</body>
</html>"
What went wrong and why?
The way I tend to solve this problem is by defining a shortcut macro like
(defmacro html-to-stout (&body body)
"Outputs HTML to standard out."
`(with-html-output (*standard-output* nil :indent t) ,#body))
or the string-equivalent. The key here is that it doesn't output a :prologue, so it can output an HTML chunklet rather than a full page. Once you've got that, you can do things like
(defun test-header (text)
(html-to-stout
(:h1 (str text))))
(test-html (test-header "Hello Hello"))
I had the same problem. As far as I could google out was, that it is not possible in the official version of cl-who: http://lisp-univ-etc.blogspot.com/2009/03/cl-who-macros.html
I used this version instead, which supports macros: https://github.com/vseloved/cl-who

How to organize big R functions?

I'm writing an R function, that is becoming quite big. It admit multiple choice, and I'm organizing it like so:
myfun <- function(y, type=c("aa", "bb", "cc", "dd" ... "zz")){
if (type == "aa") {
do something
- a lot of code here -
....
}
if (type == "bb") {
do something
- a lot of code here -
....
}
....
}
I have two questions:
Is there a better way, in order to not use the 'if' statement, for every choice of the parameter type?
Could it be more functional to write a sub-function for every "type" choice?
If I write subfunction, it would look like this:
myfun <- function(y, type=c("aa", "bb", "cc", "dd" ... "zz")){
if (type == "aa") result <- sub_fun_aa(y)
if (type == "bb") result <- sub_fun_bb(y)
if (type == "cc") result <- sub_fun_cc(y)
if (type == "dd") result <- sub_fun_dd(y)
....
}
Subfunction are of course defined elsewhere (in the top of myfun, or in another way).
I hope I was clear with my question. Thanks in Advance.
- Additional info -
I'm writing a function that applies some different filters to an image (different filter = different "type" parameter). Some filters share some code (for example, "aa" and "bb" are two gaussian filters, which differs only for one line code), while others are completely different.
So I'm forced to use a lot of if statement, i.e.
if(type == "aa" | type == "bb"){
- do something common to aa and bb -
if(type == "aa"){
- do something aa-related -
}
if(type == "bb"){
- do something bb-related -
}
}
if(type == "cc" | type == "dd"){
- do something common to cc and dd -
if(type == "cc"){
- do something cc-related -
}
if(type == "dd"){
- do something dd-related -
}
}
if(type == "zz"){
- do something zz-related -
}
And so on.
Furthermore, there are some if statement in the code "do something".
I'm looking for the best way to organize my code.
Option 1
One option is to use switch instead of multiple if statements:
myfun <- function(y, type=c("aa", "bb", "cc", "dd" ... "zz")){
switch(type,
"aa" = sub_fun_aa(y),
"bb" = sub_fun_bb(y),
"bb" = sub_fun_cc(y),
"dd" = sub_fun_dd(y)
)
}
Option 2
In your edited question you gave far more specific information. Here is a general design pattern that you might want to consider. The key element in this pattern is that there is not a single if in sight. I replace it with match.function, where the key idea is that the type in your function is itself a function (yes, since R supports functional programming, this is allowed).:
sharpening <- function(x){
paste(x, "General sharpening", sep=" - ")
}
unsharpMask <- function(x){
y <- sharpening(x)
#... Some specific stuff here...
paste(y, "Unsharp mask", sep=" - ")
}
hiPass <- function(x) {
y <- sharpening(x)
#... Some specific stuff here...
paste(y, "Hipass filter", sep=" - ")
}
generalMethod <- function(x, type=c(hiPass, unsharpMask, ...)){
match.fun(type)(x)
}
And call it like this:
> generalMethod("stuff", "unsharpMask")
[1] "stuff - General sharpening - Unsharp mask"
> hiPass("mystuff")
[1] "mystuff - General sharpening - Hipass filter"
There is hardly ever a reason not to refactor your code into smaller functions. In this case, besides the reorganisation, there is an extra advantage: the educated user of your function(s) can immediately call the subfunction if she knows where she's at.
If these functions have lots of parameters, a solution (to ease maintenance) could be to group them in a list of class "myFunctionParameters", but depends on your situation.
If code is shared between the different sub_fun_xxs, just plug that into another function that you use from within each of the sub_fun_xxs, or (if that's viable) calculate the stuff up front and pass it directly into each sub_fun_xx.
This is a much more general question about program design. There's no definitive answer, but there's almost certainly a better route than what you're currently doing.
Writing functions that handle the different types is a good route to go down. How effective it will be depends on several things - for example, how many different types are there? Are they at all related, e.g. could some of them be handled by the same function, with slightly different behavior depending on the input?
You should try to think about your code in a modular way. You have one big task to do overall. Can you break it down into a sequence of smaller tasks, and write functions that perform the smaller tasks? Can you generalize any of those tasks in a way that doesn't make the functions (much) more difficult to write, but does give them wider applicability?
If you give some more detail about what your program is supposed to be achieving, we will be able to help you more.
This is more of a general programming question than an R question. As such, you can follow basic guidelines of code quality. There are tools that can generate code quality reports from reading your code and give you guidelines on how to improve. One such example is Gendarme for .NET code. Here is a typical guideline that would appear in a report with too long methods:
AvoidLongMethodsRule