racket

Racket Programming. Where am I going wrong?

扶醉桌前 提交于 2019-11-28 14:29:16
The question i'm trying to answer: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? Where am I going wrong? my prime? test seems to be the issue, but it works fine on relatively small numbers. However the prime? test gives a wrong answer with larger numbers. Is there an easier way to go about this? (define b 3) (define z 0) (define divides? (lambda (a b) (= (remainder a b) 0))) (define (prime? n) (cond ((or (= n 1) (= n 0)) false) ((even? n) false) ((= n 2) true) ((= n b) true) ((divides? n b) false) (else (and (set! b (+ b 1))

How do you return the description of a procedure in Scheme?

99封情书 提交于 2019-11-28 14:06:27
Suppose I have something like this: (define pair (cons 1 (lambda (x) (* x x)) If I want to return the front object of the pair I do this: (car pair) And it returns 1. However when the object is a procedure I don't get the exact description of it. In other words: (cdr pair) returns #<procedure> and not (lambda (x) (*x x)) . How do I fix this? Although there's no way to do this generally, you can rig up something to do it for procedures that you define. Racket struct s can define a prop:procedure that allows the struct to be applied (called) as a procedure. The same struct can hold a copy of

Fibonacci in Scheme

萝らか妹 提交于 2019-11-28 13:47:49
I am trying to understand recursion in scheme and i have a hard time doing the dry run for it, for example a simple fibonacci number problem can someone breakdown the steps in which the additions take place ? (define (fib n) (if (<= n 2) 1 (+ (fib (- n 1)) (fib (- n 2))))) If you're using Racket, as your tags indicate, then you have a built-in stepper. Enter the program into DrRacket, and click Step in the top-right menu: First step http://f.cl.ly/items/341k1X2c44422e220T3I/Screen%20Shot%202013-02-01%20at%208.00.27%20PM.png Then a stepper window will open up. Click Step over and over, and you

arbitrary precision addition using lists of digits

主宰稳场 提交于 2019-11-28 12:46:52
问题 What I'm trying to do is take two lists and add them together like each list is a whole number. (define (reverse lst) (if (null? lst) '() (append (reverse (cdr lst)) (list (car lst))))) (define (apa-add l1 l2) (define (apa-add-help l1 l2) (cond ((and (null? l1) (null? l2)) '()) ((null? l1) (list (+ (apa-add-help '() (cdr l2))))) ((null? l2) (list (+ (apa-add-help (cdr l1) '())))) ((>= (+ (car l1) (car l2)) 10) (append (apa-add-help (cdr l1) (cdr l2)) (list (quotient (+ (car l1) (car l2)) 10))

Finding path between 2 points in Racket

五迷三道 提交于 2019-11-28 11:38:05
问题 I have following list of connections: (define routelist (list (list'a 'b) (list'a 'c) (list'b 'e) (list'b 'f) (list'b 'c) (list'a 'd) (list'e 'f) (list'f 'g))) Routes between 'a and 'g are to be found. This page shows a solution in Prolog: http://www.anselm.edu/homepage/mmalita/culpro/graf1.html I could manage following solution, though it is iterative: (define (mainpath routelist start end (outl '())) (if (equal? start end) (println "Same start and end points.") (for ((item routelist)) (when

Transpose a matrix in racket (list of lists

半城伤御伤魂 提交于 2019-11-28 11:31:24
I got a list of lists in racket and have to transpose them. (: transpose ((list-of(list-of %a)) -> (list-of (list-of %a)))) (check-expect (transpose (list (list 1 2 3) (list 4 5 6))) (list (list 1 4) (list 2 5) (list 3 6))) (define transpose (lambda (xs) (cond ((empty? xs)empty) ((pair? xs)(make-pair (make-pair (first(first xs)) (make-pair (first(first(rest xs)))empty)) (transpose (rest(rest xs)))))))) That's my code at the moment. I think the problem is in the recursive call (correct me if I'm wrong please). The actual outcome is (list (list 1 4)) . The rest seems kinda ignored. It would

How to compute the number of times pattern in one list appears in other list in Scheme

人走茶凉 提交于 2019-11-28 10:59:42
问题 I am stuck up in a Scheme program for about 5 hours. The program that I am working on should take two lists as input and then compute the number of times the pattern within the first list appears on the second list. For example : > (patt '(b c) '(a b c d e b c)) ==> answer = 2 (patt '(a b c) '(a b c a b c d e a b c c c)) ==> answer = 3 (patt '((a b) c) '(a b (a b) c d e b c)) ==> answer = 1 Below is the code that I have till now. (define (patt lis1 lis2) (cond ((null? lis1) 0) ((null? lis2) 0

Why is one-armed “if” missing from Racket?

a 夏天 提交于 2019-11-28 09:38:11
In standard Scheme it is possible to write (if (> x 2) (set! x (- x 1))) but this is not possible in Racket -- Racket's if always requires two arms. Why? Rationale The one-armed variant of if was removed from Racket to prevent bugs. In functional code one always uses the two-armed variant of if . (if test expr-on-true expr-on-false) Forgetting the second arm expr-on-false would not lead to a syntax-error, but to a runtime error (the expression would return #<void> ). To prevent these often occurring bugs in functional code, it was decided to introduce the form when for the one-armed variant of

While Loop Macro in DrRacket

感情迁移 提交于 2019-11-28 09:20:35
问题 I am trying to create a macro for while loop in DrRacket. Here is what I wrote: (require mzlib/defmacro) (define-macro my-while (lambda (condition body) (list 'local (list (list 'define (list 'while-loop) (list 'if condition (list body (list 'while-loop)) '(void)))) '(while-loop)))) (define x 0) (my-while (< x 10) (begin (display x) (newline) (set! x (+ x 1)))) The output of this program is: 0 1 2 3 4 5 6 7 8 9 error: procedure application: expected procedure, given: #<void>; arguments were:

Behavour of nested quotes in Scheme and Racket

人盡茶涼 提交于 2019-11-28 07:53:33
问题 While writing a function in Racket I accidently put two single quotes in front of a symbol instead of one. i.e. I accidently wrote ''a and discovered some behaviour of nested quotes that seems strange. I'm using DrRacket and tested this with both the Racket lang and the R5RS lang. (write (pair? (quote (quote a)))) prints: #t . (write (car (quote (quote a)))) prints: quote But (write (quote (quote a))) and (write '(quote a))) Both print: 'a Can someone tell me why in Scheme (and Racket) the