Why am I taking "object not applicable" error in scheme? [duplicate] - object

This question already has an answer here:
My code signals the error "application: not a procedure" or "call to non procedure"
(1 answer)
Closed 4 years ago.
(define subset? (lambda (st1 st2)
(cond
((not (set? st1))
(error "Your first argument is not a set!"))
((not (set? st2))
(error "Your second argument is not a set!"))
((null? (st1)) #t)
((in? ((car st1) st2)) (subset? ((cdr st1) st2)))
(else #f)
)))
I have written this code to check whether the first list appears in the second, i.e. second contains the first. Everything seems fine to me but it says object bla bla (first list is shown) is not applicable.
Any help will be appreciated. Should be easy one, but couldn't see it.

You have several parenthesis issues, possibly because you think that the argument list should be wrapped in parentheses.
You should just write the arguments sequentially after the function's name.
The form
(define subset? (lambda (st1 st2) ...
can easily lead to this mistake, but the equivalent form
(define (subset? st1 st2) ...
looks like function application looks.
So, in
(in? ((car st1) st2))
you try to apply (car st1) to st2 and pass the result to in?; in
(subset? ((cdr st1) st2))
you try to apply (cdr st1) to st2 and pass the result to subset?; and in
(null? (st1))
you're trying to call st1 without any arguments and pass the result to null?.
The correct syntax is
(in? (car st1) st2)
(subset? (cdr st1) st2)
(null? st1)

Related

Case command in Scheme language

I am trying to understand how to use case command with variables in Scheme
(define CONSTANT 5)
(define x 5)
(case x ((CONSTANT) "equal") (else "not equal"))
The above example results in "not equal". Why?
Note that the following example works:
(define CONSTANT 5)
(define x 5)
(case x ((5) "equal") (else "not equal"))
If you look at the documentation, it's stated that:
The selected clause is the first one with a datum whose quoted form is equal? to the result of val-expr.
The key word here is "quoted". The expression in the clause is not evaluated, is taken literally as it was written. So in fact you're trying to match against the symbol 'CONSTANT. We can verify this:
(define x 'CONSTANT)
(case x
((CONSTANT) "equal")
(else "not equal"))
=> "equal"
UPDATE: From the comments, it appears that you need to match an element against a list of constants. A cond + member will work better:
(define CONSTANT1 1)
(define CONSTANT2 2)
(define CONSTANT3 3)
(define x 3)
(define constants (list CONSTANT1 CONSTANT2 CONSTANT3))
(cond ((member x constants) "equal")
(else "not equal"))
=> "equal"

In DrRacket how do i alter my program that uses if statements to rather use a conditional statement

Here is the question I am trying to solve
Write a function (last-substring-len s n).
It consumes a Str and a Nat, and returns the substring of s with
length n that comes last alphabetically.
Remember to consider if your function Requires anything of its
arguments!
For example:
(last-substring-len "foobar" 4) => "ooba"
(last-substring-len "thequickbrownfoxjumpsoverthelazydogs" 7) => "xjumpso"
I have actually nearly solved my question but I want to be able to instead alter my code so that the if statements for the function get-maximium are replaced with cond statements.
This is my original code:
(define (substrings-w-len s n)
(cond
[(> n (string-length s)) '()]
[ else (cons (substring s 0 n)
(substrings-w-len (substring s 1 (string-length s)) n))]))
(define (get-maximium string-list)
(if(null? string-list) '()
(if(= 1( length string-list)) string-list
(if (equal? #true (string>=? (first string-list) (first (rest string-list))))
(get-maximium (cons (first string-list) (rest (rest string-list))))
(get-maximium (rest string-list))))))
(define (last-substring-w-len s n)
( get-maximium (substrings-w-len s n)))
(check-expect (last-substring-w-len "foobar" 4) "ooba")
This yields (list "ooba") when it should only yield "ooba" so I need to convert the list into a string which I should be able to solve myself but i'd appreciate if someone could help me with this.
The main issue now though is i want to replace the if statements in get-maximum with a conditional statement
This is me altering the get-maximium function to try to replace the if functions with a cond statement but it doesn't work yet.
(define (get-maximium string-list)
(cond
[(null? string-list) '()]
[(= 1( length string-list)) string-list]
[ else (equal? #true (string>=? (first string-list) (first (rest string list))))
(get-maximium (cons (first string-list) (rest (rest string-list))))
(get-maximium (rest string-list))]))
There's an issue with the else part. It should look like this:
(define (get-maximium string-list)
(cond [(null? string-list) '()]
[(= 1 (length string-list)) string-list]
[(string>=? (first string-list) (first (rest string list)))
(get-maximium (cons (first string-list) (rest (rest string-list))))]
[else (get-maximium (rest string-list))]))
Also, there's no need to ask (equal? #t <condition>), simply ask <condition>.
Try to retrieve the first element of the answer like so:
(define (last-substring-w-len s n)
(first ( get-maximium (substrings-w-len s n))))
(last-substring-w-len "foobar" 4) => "ooba"
(last-substring-w-len "thequickbrownfoxjumpsoverthelazydogs" 7) => "xjumpso"
For a thorough explanation see my answer here

Checking if element is present in list in racket

How to check if an element is present in a list, both taken as input from the function call, without using the lambda? I was trying member? but could not get it.
(define (find-string (lst lst str ua)
(cond ((member? ua lst) #t)
(else #f))
The use of member would work it's just that you are adding extra "?" in front of the function none is required
(member 2 (list 1 2 3 4)) [1]
would return true
another way around is writing ones own recursive function
(define (is-in-list list value)
(cond
[(empty? list) false]
[(= (first list) value) true]
[else (is-in-list (rest list) value)]))
[1]https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._member%29%29
First, the way with lambda and ormap (for testing):
; ismember? :: String List-of-Strings -> Bool
(define (ismember1? str strs) (ormap [lambda (s) (string=? s str)] strs) )
Second way, with for/or, without lambda:
(define (ismember2? str strs)
(for/or ([s (in-list strs)])
(string=? s str) ) )
Third way, with member, without lambda:
(define (ismember3? str strs) (if [member str strs] #t #f) )
Refer to the official Racket documentation for member.
Notice that the last version is actually the worst in terms of performance.

Scheme list of strings

my function in scheme looks like this
(define (func1 input)
(let kloop ((x 6))
(let ((act (string-copy (func2 input2))))
(if (eq? act "") (display "null") (display act))
(if (> x 0) (kloop (- x 1)))))))
func2 return some string which is stored in act. Now I have to create a list of all strings returned by this function. Here above, I am just displaying those strings. I tried different approaches, but nothing is working out. I tried using append and cons.
Please suggest.
Your last if is missing the else case, which is where one would expect the return value of the function to be.
You don't mention how you've tried to use append and cons, but a common pattern is to pass an accumulating parameter around in the loop:
(define (five input)
(let loop ((x 5) (outputs '()))
(if (> x 0)
(loop (- x 1) (cons input outputs))
outputs)))
> (five "yes")
'("yes" "yes" "yes" "yes" "yes")
You are calling func2 on input six times. Does it return a different value each time? If not, this works:
(define (func1 input)
(make-list 6 (func2 input)))
The question is a bit confusing, you should provide a sample of the expected output for a given input. And why the empty string is treated differently in your code? apparently the recursion should advance on the value of x, not the value of the string returned by func2. Also, why are you copying the string? seems unnecessary.
Assuming that the named let is used just for keeping track of the number of iterations, this solution seems aligned with your intent, as this will return a 6-element list of all strings returned by func2
(define (func1 input)
(let kloop ((x 6))
(if (zero? x)
'()
(cons (func2 input)
(kloop (- x 1))))))
But we can be smarter and use the named let to give a tail-recursive solution, which is more efficient:
(define (func1 input)
(let kloop ((x 6)
(acc '()))
(if (zero? x)
acc
(kloop (- x 1)
(cons (func2 input)
acc)))))

Scheme - list functions with filter

I am currently working on a homework assignment with MIT scheme, and have come across a few problems that are supposedly very short, though I'm a bit confused as to how to implement some of them.
One problem asks me to write a function that returns a list with all the integers removed. I did manage to solve that,
(define (f2a lst) (map (lambda(x) (remove number? x)) lst))
though I'm confused as to how I can rewrite it to not use remove, but rather use a filter.
*note: (f2a '(("a" 1 "b") (2 "c") (-1 "d") (-2))) returns '(("a" "b") ("c") ("d"))
The other two problems are ones to which I haven't found any solutions.
They ask me to write a function that returns a list with all positive odd and negative even integers removed. For example,
(f2b '(("a" 1 "b") (2 "c") (-1 "d") (-2)))
returns
(("a" "b") (2 "c") (-1 "d"))
I have some code down that is incorrect, but I feel shows how I have tried to approach solving this one:
(define (f2b lst)
(lambda(x)
(cond ((and (positive? x) (odd? x)) (filter x lst))
((and (negative? x) (even? x)) (filter x lst))
(else "this should never print"))))
The last problem simply asks for a function that returns a string consisting of all strings appended together in a list. (f2c '(("a" 1 "b") (2 "c") (-1 "d") (-2))) returns "abcd".
I almost managed to figure this one out, but got stuck when it kept returning strange values. This is the code I have:
(define (f2c lst)
(lambda(x)
(map (lambda (x) (filter string? x)) lst)
(list x))
(string-append (car lst) (cdr lst)))
In terms of higher-order syntax, I'm limited to map, filter, accumulate and sum. I am not asking for a direct answer, but rather some help for me to figure out what I need to do. What am I doing wrong with my code? Any assistance given with this is very much appreciated. Thank you.
The structure of the input and the desired output is identical in the first two problems; the only thing that differs is the predicate on when/when-not to remove an element. For the second case it would be:
(define (f2b lst)
(map (lambda (sublst)
(remove (lambda (x)
(and (number? x)
(or (and (positive? x) (odd? x))
(and (negative? x) (even? x)))))
sublst))
lst))
Since only the predicate differs you can generalize this as:
(define (f2x predicate)
(lambda (lst)
(map (lambda (sublst) (remove predicate sublst)) lst)))
(define f2a (f2x number?))
(define f2b (f2x (lambda (x)
(and (number? x)
(or (and (positive? x) (odd? x))
(and (negative? x) (even? x))))))
For your last problem, you can use the result of the first problem as:
(define (f2c lst)
(apply string-append (apply append (f2a list))))
Also, note that your syntax for f2b and f2a is incorrect. You are using
(define (func arg)
(lambda (x) ...))
which means that (func arg) returns a function which isn't what you want.

Resources