Expects 2 arguments but found only 1 - racket

I am doing a practice question where it asks to define a function that is supposed to produce all elements in a list, loi, that can be divided by 3 or a natural number, mult, but not both.
Below is my code:
(define (keep-multiples-of-three-or loi mult)
(cond
[(empty? loi) empty]
[else (cond [(empty? loi) empty]
[(and (not (equal? 3 mult))
(or (equal? (remainder (first loi) 3) 0)
(equal? (remainder (first loi) mult) 0)))
(first loi)]
[else (keep-multiples-of-three-or (rest-loi)
mult)]))]))
for (keep-multiples-of-three-or (cons 9 (cons 3 empty)) 3)
the error message says: cons expects 2 arguments but found only 1.
I don’t know what is wrong here. Can somebody help me?

You have a couple of errors:
There's no need to nest the cond expression
The [(empty? loi) empty] case is repeated two times
It's not (rest-loi), it's (rest loi)
In the second case, you need to cons the result and call the recursion again
The (not (equal? 3 mult)) condition is making your example return an empty list, think about it: you're saying that mult is 3, but then you check it mult is not 3
This should fix the issues:
(define (keep-multiples-of-three-or loi mult)
(cond [(empty? loi) empty]
[(or (equal? (remainder (first loi) 3) 0)
(equal? (remainder (first loi) mult) 0))
(cons (first loi)
(keep-multiples-of-three-or (rest loi) mult))]
[else (keep-multiples-of-three-or (rest loi) mult)]))
For example:
(keep-multiples-of-three-or (list 9 3) 3)
=> '(9 3)
(keep-multiples-of-three-or (list 1 3 5 9) 5)
=> '(3 5 9)

Related

Filter function for nested lists in dr racket

I am looking for a function that does what "filter" is used for, but without using filter. On top, it should also filter nested lists.
E. g. list ( 1 2 (list 3 'a 'b) 3 4) should return list (1 2 (list 3) 3 4).
I already have the function to filter. My thought is, that I have to distinguish in the beginning of the function, if first lst is a list itself.
What I got so far is:
(define (my-filter pred lst)
(cond ((null? lst) null) ;;(after this I would try to test if it is a list itself, but I am not sure how)
((pred (first lst))
(cons (first lst) (my-filter pred (rest lst))))
(else (my-filter pred (rest lst)))))
As it has been said in the comments, a possible solution is:
(define (my-filter pred lst)
(cond [(null? lst) null]
[(list? (first lst)) (cons (my-filter pred (first lst))
(my-filter pred (rest lst)))]
[else (if (pred (first lst))
(cons (first lst) (my-filter pred (rest lst)))
(my-filter pred (rest lst)))]))
Example
> (my-filter number? '(1 a 2 (3 (b 4) c) 5 6 d 7))
'(1 2 (3 (4)) 5 6 7)
> (my-filter (lambda (x) (not (number? x))) '(1 a 2 (3 (b 4) c) 5 6 d 7))
'(a ((b) c) d)

trying to write a function that returns every third element in a list in racket language

Trying to write a function that returns every third element in a list
including the first element in racket. All I get now is my code blowing up with a first: contract violation
expected: (and/c list? (not/c empty?))
given: 4
(define l (list 1 2 3 4 5 6 7 8 9))
(define (skipper lst)
(if (null? lst)
'()
(cons (first lst)
(skipper (car (cdr (cdr (cdr lst))))))))
(skipper l)
The problem was just the car around cdddr.
(define l (list 1 2 3 4 5 6 7 8 9))
(define (skipper lst)
(if (null? lst)
'()
(cons (first lst)
(skipper (if (< (length lst) 3)
'()
(cdddr lst))))))
(skipper l) ;; '(1 4 7)
Generalized Solution
(define (my-cdr lst) ;; `cdr` behaving like in common-lisp: (cdr '()) -> '()
(cond ((null? lst) '())
(else (cdr lst))))
(define (multi-cdr lst k) ;; apply `my-cdr` k-times on `lst`
(cond ((zero? k) lst)
(else (multi-cdr (my-cdr lst) (- k 1)))))
(define (skipper lst k)
(if (null? lst)
'()
(cons (first lst)
(skipper (multi-cdr lst k) k))))
Test it:
(skipper l 3) ;; '(1 4 7)
(skipper l 4) ;; '(1 5 9)
(skipper l 2) ;; '(1 3 5 7 9)
(skipper l 1) ;; '(1 2 3 4 5 6 7 8 9)
The issue is that you cannot call (cdr (cdr (cdr lst))) when lst has less than 3 elements.
You tagged this with racket, so I'm going to show you a solution using match
(define (skipper l)
(match l
;; some element and at least 3 more
((list a rest ..3)
(cons a (skipper (cddr rest))))
;; at least one element
((cons a _)
(list a))
;; otherwise
(else
empty)))
(skipper '())
;; '()
(skipper '(0))
;; '(0)
(skipper '(0 1 2 3 4 5 6 7))
;; '(0 3 6)
(skipper '(0 1 2 3 4 5 6 7 8 9))
;; '(0 3 6 9)
This solution doesn't use length which unnecessarily computes the length of the list

Racket - Filter even and odd integers into two separate lists

Function should take a list of integers and return a list containing two sublists -- the first containing the even numbers from the original list, the second containing the odd. My code gets the job done, but if I test it with a negative integer, such as the -5 in the second test, it just gets ignored by my code. Any ideas on how to fix?
(Side note - I know there are functions for even, odd, etc, but for this assignment I am to create them myself.)
(define (segregate lst)
(list(pullEven lst)(pullOdd lst)))
(define (pullEven lst)
(if (empty? lst)
'()
(if (isEven (first lst))
(cons (first lst) (pullEven (rest lst)))
(pullEven (rest lst)))))
(define (pullOdd lst)
(if (empty? lst)
'()
(if (isOdd (first lst))
(cons (first lst) (pullOdd (rest lst)))
(pullOdd (rest lst)))))
(define (isEven x)
(if (equal? (remainder x 2) 0) #t #f)
)
(define (isOdd x)
(if (equal? (remainder x 2) 1) #t #f)
)
;tests
"---------------------------------------------"
"Segregate Tests"
(segregate '(7 2 3 5 8))
(segregate '(3 -5 8 16 99))
(segregate '())
"---------------------------------------------"
Try substituting modulo instead of remainder.
Remainder will preserve the sign of the answer (a remainder of -1 doesn't match the value of 1 that you're checking for).
Modulo returns an answer with the same sign as the denominator.

IN Racket Define a function that takes two arguments

I need some one can explain for me to how to do this please
Define a function that takes two arguments, a list of numbers and a single number (the threshold). It should return a new list that has the same numbers as the input list, but with all elements greater than the threshold number removed. You may not use the built-in filter function as a helper
function. Your implementation must be recursive.
INPUT: A list of numbers and a single atomic number.
OUTPUT: A new list of numbers that contains only the numbers from the original list that are strictly “less than” (<), i.e. below the threshold number.
Example:
> (upper-threshold '(3 6.2 7 2 9 5.3 1) 6)
'(3 2 5.3 1)
> (upper-threshold '(1 2 3 4 5) 4)
'(1 2 3)
> (upper-threshold '(4 8 5 6 7) 6.1)
'(4 5 6)
> (upper-threshold '(8 3 5 7) 2)
'()
This what I have so far but I receve error
(define (upper-threshold pred lst)
(cond [(empty? lst) empty]
[(pred (first lst))
(cons (first lst) (upper-threshold pred (rest lst)))]
[else (upper-threshold pred (rest lst))]))
; (threshold (lambda (x) (> x 5)) '(1 6 7))
Your implementation doesn't have the same arguments as your assignment.
You need something that compares the first element with the second argument so see it its larger or not, then either (cons (car lst) (upper-treshold (cdr lst) streshold)) to include the first element in the result or (upper-treshold (cdr lst) treshold) to not include it.
(define (upper-threshold lst treshold)
(cond [(empty? lst) empty]
[(> (car lst) treshold)
(cons (first lst) (upper-threshold (rest lst) treshold))]
[else (upper-threshold (rest lst) treshold)]))
I don't quite understand your code. However, you might be looking for something like this:
(define (upper-threshold lst theshold)
(cond
((null? lst) '())
((< (car lst) theshold)
(cons (car lst)
(upper-threshold (cdr lst) theshold)))
(else (upper-threshold (cdr lst) theshold))))
If your purpose is to implement the standard function filter, perhaps you should write the code some another way.
It appears that you've taken a filter function and renamed it as upper-threshold. It's true that these two are related. I would suggest trying to build upper-threshold from scratch, using the design recipe:
http://www.ccs.neu.edu/home/matthias/HtDP2e/
When you get confused, refer to existing functions that you have, including the definition of filter that you have here. Your example may be slightly harder to understand because it uses lambda.

racket postfix to prefix

I have a series of expressions to convert from postfix to prefix and I thought that I would try to write a program to do it for me in DrRacket. I am getting stuck with some of the more complex ones such as (10 (1 2 3 +) ^).
I have the very simple case down for (1 2 \*) → (\* 1 2). I have set these expressions up as a list and I know that you have to use cdr/car and recursion to do it but that is where I get stuck.
My inputs will be something along the lines of '(1 2 +).
I have for simple things such as '(1 2 +):
(define ans '())
(define (post-pre lst)
(set! ans (list (last lst) (first lst) (second lst))))
For the more complex stuff I have this (which fails to work correctly):
(define ans '())
(define (post-pre-comp lst)
(cond [(pair? (car lst)) (post-pre-comp (car lst))]
[(pair? (cdr lst)) (post-pre-comp (cdr lst))]
[else (set! ans (list (last lst) (first lst) (second lst)))]))
Obviously I am getting tripped up because (cdr lst) will return a pair most of the time. I'm guessing my structure of the else statement is wrong and I need it to be cons instead of list, but I'm not sure how to get that to work properly in this case.
Were you thinking of something like this?
(define (pp sxp)
(cond
((null? sxp) sxp)
((list? sxp) (let-values (((args op) (split-at-right sxp 1)))
(cons (car op) (map pp args))))
(else sxp)))
then
> (pp '(1 2 *))
'(* 1 2)
> (pp '(10 (1 2 3 +) ^))
'(^ 10 (+ 1 2 3))
Try something like this:
(define (postfix->prefix expr)
(cond
[(and (list? expr) (not (null? expr)))
(define op (last expr))
(define args (drop-right expr 1))
(cons op (map postfix->prefix args))]
[else expr]))
This operates on the structure recursively by using map to call itself on the arguments to each call.