Can anyone help me with a recursive function in racket? - racket

I am writing a recursive function. But the question requires you not to use the exponential function. Can anyone show me how to get larger powers by multiplying smaller powers by a?
Input a=2 n=4. Then get[2, 4, 8, 16]
Input a=3 n=4. Then get[3 9 27 81].
I was trying to multiply a by a each time, so when I input 2 and 4. I get [2 4 16 256]. So what should I do?
Here is what I have written:
(define (input a n)
(if (= n 0)
'()
(append (cdr (list [* a a] a))
(let ((a (* a a)))
(input a (- n 1))))))

You are approaching the problem wrong, you really need two recursive functions (one to build the list and one to build each element). I am assuming you are allowed to use local, but if you aren't you could move that into a helper function.
(define (build-sqr-list a n)
(local [(define (sqr-recurse a n)
(if (= n 0)
1
(* a (sqr-recurse a (sub1 n)))))]
(if (= n 0)
'()
(cons (sqr-recurse a n) (build-sqr-list a (sub1 n))))))

Related

using basic build-in or standard function(s) to solve problems in racket?

I'm working with lists in Racket, and I was doing a function to sum the elements of a list.
for example
(define (mysum L)
(if (empty? L) 0
(+ (first L) (mysum (rest L))))
)
then the result will be
> (mysum'(5 4))
9
>
But I would like to know what to do to multiply the numbers inside the list, rather than add them. when I put the * sign instead of + then it gives me an output of zero.
(define (myproduct L)
(if (empty? L) 0
(* (first L) (myproduct (rest L))))
)
and the result is
> (myproduct'(3 5))
0
> enter code here
I'm trying to understand how to multiply numbers in a list.
The myproduct should use 1 instead of 0 in the base case, because the identity element of multiplication is 1.
(define (myproduct L)
(if (empty? L) 1
(* (first L) (myproduct (rest L)))))
> (myproduct'(3 5))
15
A nice way to think about this is that * and + are the two operations of the field of numbers. And in any field, the two operations have identities, which are objects such that (<op> x <identity-of-op>) is x. But they have different identities. So let's define a function which tells us what the identity of the two operations is:
(define (identity-of op)
(cond
[(eqv? op +)
0]
[(eqv? op *)
1]
[else
(error "there are only two operations for the field of numbers")]))
And now we can define a general function which, given an operator, will return a function which combines a bunch of numbers using it (this function will be correct, since both * and + are associative):
(define (make-combiner op)
(define id (identity-of op))
(define (combiner numbers)
(if (null? numbers)
id
(op (first numbers) (combiner (rest numbers)))))
combiner)
And now:
(define sum-numbers (make-combiner +))
(define multiply-numbers (make-combiner *))

Trying to get this code to work, can't understand where to put the argument in and keep getting errors

Define the function iota1(n, m) that takes positive integers n, m with n < m as input, and outputs the list (n,n+1,n+2,...,m)
I've tried switching the code around multiple times but cannot seem to get it to function and display a list the right way
(define (iota1 n m)
(if (eq? n 0)
'()
(append (iota1 (< n m) (+ n 1)) (list n))))
There's a few oddities to the code you provided, which I've formatted for readability:
(define (iota1 n m)
(if (eq? n 0)
'()
(append (iota (< n m) (+ n 1))
(list n))))
The first is that the expression (< n m) evaluates to a boolean value, depending on whether n is less than m or not. When you apply iota to (< n m) in the expression (iota (< n m) (+ n 1)), you are giving iota a boolean value for its first argument instead of a positive integer.
Secondly, the use of append is strange here. When constructing a list in Racket, it's much more common to use the function cons, which takes as arguments a value, and a list, and returns a new list with the value added to the front. For example,
(append '(3) (append '(4) (append '(5) '()))) ==> '(3 4 5)
(cons 3 (cons 4 (cons 5 '()))) ==> '(3 4 5)
It's a good idea to opt for using cons instead of append because it's simpler, and because it is faster, since cons does not traverse the entire list like append does.
Since this sounds a bit like a homework problem, I'll leave you with a "code template" to help you find the answer:
; n : integer
; m : integer
; return value : list of integers
(define (iota1 n m)
(if (> n m) ; base case; no need to do work when n is greater than m
... ; value that goes at the end of the list
(cons ... ; the value we want to add to the front of the list
(iota1 ... ...)))) ; the call to iota, generating the rest of the list
Welcome to the racket world, my version is here:
#lang racket
(define (iota1 n m)
(let loop ([loop_n n]
[result_list '()])
(if (<= loop_n m)
(loop
(add1 loop_n)
(cons loop_n result_list))
(reverse result_list))))

SICP - Multiplication through addition

I am using the book SICP and attempting to solve this exercise:
1.2.4 Exponentiation
Exercise 1.18. Using the results of exercises 1.16 and 1.17, devise
a procedure that generates an iterative process for multiplying two
integers in terms of adding, doubling, and halving and uses a
logarithmic number of steps
I am trying to solve this with the following code:
(define (double x)
(+ x x))
(define (halve x)
(floor (/ x 2)))
(define (* a b)
(define (iter count accumulate)
(cond ((= count 1) accumulate)
((even? a) (iter (halve count) (+ accumulate (double b))))
(else empty)))
(iter a 0))
As you might see, I am trying to deal with even numbers first.
I am using the SICP wiki as my solutions-guide. They suggest some tests to see if the code works:
(* 2 4)
(* 4 0)
What I do not get is that my code passes on these two first tests, dealing only with even numbers.
However, when I try some big numbers which are multiples of two, the code fails. I checked the result using Python. For instance,
(IN PYTHON)
2**100
>> 1267650600228229401496703205376
2**98
>> 316912650057057350374175801344
a = 2**100
b = 2**98
a*b
>> 401734511064747568885490523085290650630550748445698208825344
When I use my function inside Dr. Racket with these values I get a different result:
(* 1267650600228229401496703205376 316912650057057350374175801344)
My result is: 63382530011411470074835160268800, which is wrong, as Python built-in functions suggest.
Why this is happening?
The recursive step seems wrong, and what's that empty doing there? also, what happens if b is negative? this solution should work:
(define (mul a b)
(define (iter a b acc)
(cond ((zero? b) acc)
((even? b) (iter (double a) (halve b) acc))
(else (iter a (- b 1) (+ a acc)))))
(if (< b 0)
(- (iter a (- b) 0))
(iter a b 0)))
For example:
(mul 1267650600228229401496703205376 316912650057057350374175801344)
=> 401734511064747568885490523085290650630550748445698208825344

Scheme Calculator Negative numbers

Could you help me with an example function code take an array of integers and gives the output of the array for negative numbers
thanks in advance
the user will enter this
(sumNeg '(3 -5 -2 b -5 1 b))
and will get the solution -12
The idiomatic solution in Racket would be to use built-in higher-order procedures - notice that first you have to filter the negative numbers from the list (it's not an array!) and then add them, and this is precisely expressed by the following procedure:
(define (sumNeg lst)
(apply +
(filter (λ (x) (and (number? x) (negative? x))) lst)))
To avoid a double iteration, we can use foldl:
(define (sumNeg lst)
(foldl (λ (x sum)
(if (and (number? x) (negative? x))
(+ x sum)
sum))
0
lst))
Or even more idiomatic, we can use Racket's Iterations and Comprehensions:
(define (sumNeg lst)
(for/sum ([x lst]
#:when (and (number? x)
(negative? x)))
x))
Anyway, it works as expected:
(sumNeg '(3 -5 -2 b -5 1 b))
=> -12

"application: not a procedure" while computing binomial

I am defining a function binomial(n k) (aka Pascal's triangle) but am getting an error:
application: not a procedure;
expected a procedure that can be applied to arguments
given: 1
arguments...:
2
I don't understand the error because I thought this defined my function:
(define (binomial n k)
(cond ((or (= n 0) (= n k)) 1)
(else (+ (binomial(n) (- k 1))(binomial(- n 1) (- k 1))))))
In Scheme (and Lisps in general), parentheses are placed before a procedure application and after the final argument to the procedure. You've done this correctly in, e.g.,
(= n 0)
(= n k)
(- k 1)
(binomial(- n 1) (- k 1))
However, you've got an error in one of your arguments to one of your calls to binomial:
(define (binomial n k)
(cond ((or (= n 0) (= n k)) 1)
(else (+ (binomial(n) (- k 1))(binomial(- n 1) (- k 1))))))
***
Based on the syntax described above (n) is an application where n should evaluate to a procedure, and that procedure will be called with no arguments. Of course, n here actually evaluates to an integer, which is not a procedure, and can't be called (hence “application: not a procedure”). You probably want to remove the parentheses around n:
(binomial n (- k 1))
It's also worth pointing out that Dr. Racket should have highlighted the same portion of code that I did above. When I load your code and evaluate (binomial 2 1), I get the following results in which (n) is highlighted:
Your error is here:
binomial(n)
n is an integer, not a function. If you put parentheses around it like that, scheme tries to invoke an integer as a function, which naturally produces an error.
This is the correct code:
(define (binomial n k)
(cond ((or (= n 0) (= n k)) 1)
(else (+ (binomial n (- k 1))(binomial(- n 1) (- k 1))))))
Problem is at here:
(binomial (n) (- k 1))