racket

Matrix multiplication in scheme, List of lists

折月煮酒 提交于 2019-11-29 16:37:51
I started to study Scheme and I do not understand some of it. I'm using DrRacket. I wrote the following code: (define mult_mat (λ (A B) (Trans_Mat (map (λ (x) (mul_Mat_vec A x)) (Trans_Mat B))))) That uses this functions: (define Trans_Mat (λ (A) (apply map (cons list A)))) (define mul_Mat_vec (λ (A v) (map (λ (x) (apply + (map * x v))) A))) In mult_mat , I multiply the matrix A in each vector of the transpose matrix B. It works fine. I found a code on the web that makes the multiplication in a way that I don't understand: (define (matrix-multiply matrix1 matrix2) (map (λ (row) (apply map (λ

Scheme: Changing the definition of complex numbers to accept vectors

孤街浪徒 提交于 2019-11-29 16:29:00
Basically what I am trying to do is change the definition of complex numbers so I can represent vectors in Scheme. I want to be able to write something like "i+j+k" without the quotes and not have the program go entirely crazy. I know complex numbers can be represented by something like "1+2i" so I was hoping a simple overwrite could accomplish this. I thought overwriting the complex? definition might work, but it seems to have no effect. I am unsure of where the code I need to affect even is. Any help would be amazing. Thanks. Are you aware that the Racket reader already supports complex

Racket/Scheme Flatten Explanations

这一生的挚爱 提交于 2019-11-29 14:38:17
Can someone help me to break down exactly the order of execution for the following versions of flatten? I'm using Racket. version 1, is from racket itself, while version two is a more common? implementation. (define (flatten1 list) (let loop ([l list] [acc null]) (printf "l = ~a acc = ~a\n" l acc) (cond [(null? l) acc] [(pair? l) (loop (car l) (loop (cdr l) acc))] [else (cons l acc)]))) (define (flatten2 l) (printf "l = ~a\n" l) (cond [(null? l) null] [(atom? l) (list l)] [else (append (flatten2 (car l)) (flatten2 (cdr l)))])) Now, running the first example with '(1 2 3) produces: l = (1 2 3)

Behavour of nested quotes in Scheme and Racket

偶尔善良 提交于 2019-11-29 14:14:41
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 function pair? interprets (quote (quote a))) as a pair of two elements quote and a , but the function

Improving performance for converting numbers to lists, and base10 to base2

时光毁灭记忆、已成空白 提交于 2019-11-29 12:12:39
Many Project Euler problems require manipulating integers and their digits, both in base10 and base2. While I have no problem with converting integers in lists of digits, or converting base10 into base2 (or lists of their digits), I often find that performance is poor when doing such conversions repeatedly. Here's an example: First, here are my typical conversions: #lang racket (define (10->bin num) (define (10->bin-help num count) (define sq (expt 2 count)) (cond [(zero? count) (list num)] [else (cons (quotient num sq) (10->bin-help (remainder num sq) (sub1 count)))] ) ) (member 1 (10->bin

Functional variant of 'oneof' function in Racket

让人想犯罪 __ 提交于 2019-11-29 12:11:40
I have written following function to find if one and only one of 5 variables is true: (define (oneof v w x y z) (or (and v (not w) (not x) (not y) (not z)) (and w (not v) (not x) (not y) (not z)) (and x (not v) (not w) (not y) (not z)) (and y (not v) (not w) (not x) (not z)) (and z (not v) (not w) (not x) (not y)) )) (xor takes only 2 arguments) However, it is very imperative and not functional. Moreover, I want to write a function (oneof N) which will be generic rather than specific for 5 variables. How can this be done? Thanks. Edit: As pointed out in the comments, the code is 'repetitive'

Building accumulator for lazy lists in Racket

拟墨画扇 提交于 2019-11-29 11:32:14
I defined a simple lazy list of all integers from zero: (define integers-from (lambda (n) (cons n (lambda () (integers-from (+ 1 n)))))) (define lz (integers-from 0)) I also coded an accumaltor that gets a lazy list as a parameter (define lz-lst-accumulate (lambda (op initial lz) (if (null? lz) initial (cons (op (head lz) initial) (lambda () (lz-lst-accumulate op (op initial (head lz)) (tail lz))))))) Does this accumaltor answer the format of lazy lists? Here is a simple test of the accumulator: (define acc (lz-lst-accumulate * 1 lz)) (take acc 4) => '(1 2 6 24) take is a helper function that

Loop in PLT Scheme

非 Y 不嫁゛ 提交于 2019-11-29 09:57:07
问题 How can I implement loop in plt-scheme like in java- for(int i=0;i<10;){ for(int j=0;j<3;){ System.out.println(""+j); j++; } System.out.println(""+i); i++; } 回答1: Your example in Java doesn't directly map onto the Scheme language by just learning a few new keywords as there aren't explicit constructs for implementing a for loop in Scheme (unless you write a construct yourself!). The cookbook way to do this in Scheme is to define a recursive function that loops over a list. Here's an example

Writing a formal language parser with Lisp

醉酒当歌 提交于 2019-11-29 09:42:28
问题 My company is designing a new domain specific scripting language; I have to implement a parser that translates our brand new programming language into a common scripting language so as to be able to enact it. The usual way I do this is by means of Bison and Flex tools that generate the C/C++ code of the translator. I found other tools, for most of the mainstream programming languages, but none for Lisp . Hasn't Lisp ever been used for that? What is the usual way to write a parser with Lisp ?

How to write a scheme function that takes two lists and returns four lists

时光怂恿深爱的人放手 提交于 2019-11-29 08:15:33
I have 2 lists of elements '(a b c) '(d b f) and want to find differences, union, and intersection in one result. Is that possible? How? I wrote a member function that checks if there is a car of the first list in the second list, but I can't throw a member to the new list. (define (checkResult lis1 lis2) (cond........... )) (checkresult '( a b c) '(d b f)) My result should be (( a c) (d f) (a b c d f) (b)) . Like others have said, all you need to do is create separate functions to compute the intersection, union, and subtraction of the two sets, and call them from checkresult: (define