I need some one can explain for me to how to do this please,
divisible-by-7?
Define your own Racket function that takes a single integer as an argument and returns a Boolean that indicates whether the number is evenly divisible by 7. You do not have to perform error checking on the input.
Input: An integer.
Output: A Boolean.
Example:
> (divisible-by-7? 14)
#t
> (divisible-by-7? 31)
#f
> (divisible-by-7? 56)
#t
this what I have but I keep receiving error
(define (divisible-by-7)
(divisible 7))
It seems that divisible is not a library procedure in racket.
Try
(define (divide-by-7? x)
(= (remainder x 7) 0))
Alternatively, if you would like to implement divisible? in racket:
(define (divisible k)
(lambda (x)
(= (remainder x k) 0)))
and to use it:
(define divide-by-7?
(divisible 7))
Related
CL-USER> (a-sum 0 3)
->> 6
I wrote this program :
(defun a-sum (x y)
(if (and (> x -1) (> y -1))
(do ((i 0 (1+ i))
(sum 0)
(num x))
((equal i (+ (- y x) 1)))
(setq sum (+ sum num))
(setq num (+ num 1))
sum)
(print " NOPE")))
put if I run it in the terminal it returns nil and not the answer stated above;
can someone help with the problem so it returns the value then Boolean.
DO,DO* Syntax
The entry for DO,DO* says that the syntax is as follows:
do ({var | (var [init-form [step-form]])}*)
(end-test-form result-form*)
declaration*
{tag | statement}*
The body is used as a list of statements and no intermediate value in this body is used as the result form of the do form. Instead, the do form evaluates as the last expression in result-form*, which defaults to nil.
(do ((i 0 (1+ i))
(sum 0)
(num x))
((equal i (+ (- y x) 1))
;;; RESULT FORMS HERE
)
(setq sum (+ sum num)) ;; (*)
(setq num (+ num 1)) ;; (*)
sum ;; (*)
)
All the expressions marked commented (*) above are used for side-effects only: the result of their evaluation is unused and discarded.
Problem statement
It is not clear to me what Σpi=ni means, and your code does not seem to compute something that could be expressed as that mathematical expression.
One red flag for example is that if (+ (- y x) 1) is negative (i.e. if y < x-1, for example y=1,x=3), then your loop never terminates because i, which is positive or null, will never be equal to the other term which is negative.
I would try to rewrite the problem statement more clearly, and maybe try first a recursive version of your algorithm (whichever is easier to express).
Remarks
Please indent/format your code.
Instead of adding setq statements in the body, try to see if you can define them in the iteration clauses of the loop (since I'm not sure what you are trying to achieve, the following example is only a rewrite of your code):
(do ((i 0 (1+ i))
(sum 0 (+ sum num)
(num x (1+ num))
(... sum))
Consider what value(s) a function returns. It's the value of the last form evaluated. In your case, that appears to be a do or maybe a setq or print (It's difficult to read as it's formatted now, and I don't have question edit privileges).
In short, the form that's returning the value for the function looks to be one evaluated for side-effects instead of returning a value.
I am trying to define a procedure that takes an integer and returns its representation in Church numerals. Could any one please help me figure out the mistake I am making? The following code it's what I have been able to do so far.
(define succ
(lambda (cn)
(lambda (f)
(lambda (x)
(f ((cn f) x))))))
(define (n->cn n)
(if (= n 0)
zero
(succ (n->cn (lambda (x) (- x 1))))))
When I run the test:
(test (num->cn 3) three)
I am getting the following error:
exception (num->cn 3) at line 107
expected: <no-expected-value>
=: contract violation
expected: number?
given: #<procedure:...ad/racket-file.rkt:99:21>
argument position: 1st
other arguments...:
0
It seems it's expecting a number? but a procedure is given. Which I think matches the intention of the procedure? Thanks for your help and comments for a newbie.
The argument to n->ch should be a number, not a procedure:
(define (n->cn n)
(if (= n 0)
zero
(succ (n->cn (- n 1)))))
I want to define functions in Racket which uses a parameter not given as its argument but will be defined in the scope. Is this possible?
For instance,
(define (fun1 x) (get-value x data))
(define (another-function args . data)
... (map (eval '((fun1 . x) (fun2 . y)))) ...
)
fun1 needs to refer a data structure but it will be given as an argument in the functions from which fun1 will be called
I am not experienced in Racket, and couldn't find a solution for my problem in the web. Sorry for any inconvenience, and thank you for the responses.
Edit:
I should give a more precise explanation.
The point is, I don't use a data structure like struct. Instead, I give a list containing data, apply some changes to it, and return the updated data.
The problematic part is, since my data is given as an argument, when I define a function somewhere else I can't use this data and thus I got an error for unbound identifier
A code snippet:
(define (var x) (get-value x data)) ;Should return value of x from data
;Error due to previous part: "unbound identifier in module in: data"
(define (myapply func expr data) (eval (cons(func (map (eval expr))))))
> (myapply '+ '((var x) (var y)) '((x 3) (y 4)))
7
We have standard lexical closures. Eg. we can make a curry procedure:
(define (curry proc arg)
(lambda args
(apply proc arg args)))
(define add-10 (curry + 10))
(add-10 5) ; ==> 15
If you want a value to be changed dynamically. eg. that you can change behavior of one procedure by setting a variable while you call it it's caled dynamic variables. Dynamic variables are available in #!racket using parameters. Here is an example:
#!racket
(define p (make-parameter 10))
(define (list-3-p)
(let ((p-val (p)))
(list p-val p-val p-val)))
(define (override-p-parameter new-val thunk)
(parameterize ([p new-val]) ; we override p momentarily to a new value
(thunk))) ; but it restores to it's initial value after
(list-3-p) ; (10 10 10)
(override-p-parameter 20 list-3-p) ; (20 20 20)
(list-3-p) ; (10 10 10)
If p were a normal lexical variable this would not work:
#!racket
(define p 10)
(define (list-3-p)
(let ((p-val p))
(list p-val p-val p-val)))
(define (override-p-parameter new-val thunk)
(let ([p new-val]) ; we override p momentarily to a new value
(thunk))) ; but it won't change `p` in thunk because of lexical scoping.
(list-3-p) ; (10 10 10)
(override-p-parameter 20 list-3-p) ; (10 10 10)
(list-3-p) ; (10 10 10)
I'm trying to write a function in Common Lisp to convert a base 10 number into a base 8 number, represented as a list, recursively.
Here's what I have so far:
(defun base8(n)
(cond
((zerop (truncate n 8)) (cons n nil))
((t) (cons (mod n 8) (base8 (truncate n 8))))))
This function works fine when I input numbers < 8 and > -8, but the recursive case is giving me a lot of trouble. When I try 8 as an argument (which should return (1 0)), I get an error Undefined operator T in form (T).
Thanks in advance.
Just for fun, here's a solution without recursion, using built-in functionality:
(defun base8 (n)
(reverse (coerce (format nil "~8R" n) 'list)))
It seems you have forgotten to (defun t ...) or perhaps it's not the function t you meant to have in the cond? Perhaps it's t the truth value?
The dual namespace nature of Common Lisp makes it possible for t to both be a function and the truth value. the difference is which context you use it and you clearly are trying to apply t as a function/macro.
Here is the code edited for the truth value instead of the t function:
(defun base8(n)
(cond
((zerop (truncate n 8)) (cons n nil))
(t (cons (mod n 8) (base8 (truncate n 8))))))
(base8 8) ; ==> (0 1)
I want to calculate the sum of digits of a number in Scheme. It should work like this:
>(sum-of-digits 123)
6
My idea is to transform the number 123 to string "123" and then transform it to a list '(1 2 3) and then use (apply + '(1 2 3)) to get 6.
but it's unfortunately not working like I imagined.
>(string->list(number->string 123))
'(#\1 #\2 #\3)
Apparently '(#\1 #\2 #\3) is not same as '(1 2 3)... because I'm using language racket under DrRacket, so I can not use the function like char->digit.
Can anyone help me fix this?
An alternative method would be to loop over the digits by using modulo. I'm not as used to scheme syntax, but thanks to #bearzk translating my Lisp here's a function that works for non-negative integers (and with a little work could encompass decimals and negative values):
(define (sum-of-digits x)
(if (= x 0) 0
(+ (modulo x 10)
(sum-of-digits (/ (- x (modulo x 10)) 10)))))
Something like this can do your digits thing arithmetically rather than string style:
(define (digits n)
(if (zero? n)
'()
(cons (remainder n 10) (digits2 (quotient n 10))))
Anyway, idk if its what you're doing but this question makes me think Project Euler. And if so, you're going to appreciate both of these functions in future problems.
Above is the hard part, this is the rest:
(foldr + (digits 12345) 0)
OR
(apply + (digits 1234))
EDIT - I got rid of intLength above, but in case you still want it.
(define (intLength x)
(define (intLengthP x c)
(if (zero? x)
c
(intLengthP (quotient x 10) (+ c 1))
)
)
(intLengthP x 0))
Those #\1, #\2 things are characters. I hate to RTFM you, but the Racket docs are really good here. If you highlight string->list in DrRacket and hit F1, you should get a browser window with a bunch of useful information.
So as not to keep you in the dark; I think I'd probably use the "string" function as the missing step in your solution:
(map string (list #\a #\b))
... produces
(list "a" "b")
A better idea would be to actually find the digits and sum them. 34%10 gives 4 and 3%10 gives 3. Sum is 3+4.
Here's an algorithm in F# (I'm sorry, I don't know Scheme):
let rec sumOfDigits n =
if n<10 then n
else (n%10) + sumOfDigits (n/10)
This works, it builds on your initial string->list solution, just does a conversion on the list of characters
(apply + (map (lambda (d) (- (char->integer d) (char->integer #\0)))
(string->list (number->string 123))))
The conversion function could factored out to make it a little more clear:
(define (digit->integer d)
(- (char->integer d) (char->integer #\0)))
(apply + (map digit->integer (string->list (number->string 123))))
(define (sum-of-digits num)
(if (< num 10)
num
(+ (remainder num 10) (sum-of-digits (/ (- num (remainder num 10)) 10)))))
recursive process.. terminates at n < 10 where sum-of-digits returns the input num itself.