scheme

How to remove non-duplicate elements from a list in Scheme?

ぐ巨炮叔叔 提交于 2019-12-12 22:48:20
问题 Given a list, (define ll '(a a a b c c c d e e e e)) I want to remove all non-duplicate elements and leave only one copy of the duplicate one, i.e. after removing, the result would be (a c e) My algorithm is: Traverse through the list, comparing current element with next element. If they're equal, then cons the current element with the list of the next recursive call. For example, (a a a b c) Move from left to right, encounter a and a . (cons a (remove-nondup (cddr lst))) Otherwise, skip

What's a recursive way to move the second element of a list to the front? [Racket]

ぐ巨炮叔叔 提交于 2019-12-12 22:34:02
问题 How can I recursively move the middle of a 3-element list to the front of the list? There are nested lists. So, ((not #f) iff (((#f implies #t) and #t) or #f)) Should become (iff (not #f) (or (and (implies #f #t) #t) #f)) 回答1: It's a really good use of match because we can set a condition for the 3-element list and simply ignore the other cases - (define (transform l) (match l ((list a b c) (list (transform b) (transform a) (transform c))) (_ l))) (transform '((not #f) iff (((#f implies #t)

Calling a fortran routine as a Scheme function

大憨熊 提交于 2019-12-12 21:17:13
问题 Is it possible to call a Fortran routine as a Scheme function? I could find nothing by searching the web. 回答1: The answer depends on which implementation you use. Here is an example of writing bindings in Racket. The bindings are for CBLAS and LAPACK. The CBLAS library is C based and LAPACK is Fortran based. Therefore you can see both styles. (Unfinished) Racket bindings for CBLAS and LAPACK 回答2: Is it possible? Technically, yes. Most modern Fortran compilers (e.g. ifort, gfortran) support

Can I define a global from inside a procedure in Scheme?

做~自己de王妃 提交于 2019-12-12 20:19:48
问题 I have a situation where I'd like to do something like... (define (def a b) (store a b) ; store the definition of 'a' somewhere (define-global a b)) ; also define 'a' so that its definition ; is accessible later in the program Is this possible somehow? As far as I know define-global doesn't exist, so define statements inside procedures apply only to the local environment. This is intended for creating a 'def' procedure for an embedded DSL in scheme, so in addition to making the definition I

List passed to procedure converts into list of list inside the procedure

余生长醉 提交于 2019-12-12 18:32:50
问题 I'm debugging this code on DrRacket: #lang racket (define last-element-on-list (lambda l (cond ((null? l) '()) ((null? (cdr l)) (car l)) (else (last-element-on-list (cdr l))) ) ) ) (define lst '( (n 25 f +) (s 25 m +) (ll 20 no -))) (list-ref lst 0) (last-element-on-list (list-ref lst 0)) The code (list-ref lst 0) returns '(n 25 f +) , but when I get into the procedure last-element-on-list the parameter l has the value ((n 25 f +)) . Why l is a list of list in procedure last-element-on-list ?

Convert Byte String to Int in Scheme

孤街浪徒 提交于 2019-12-12 18:18:10
问题 I have code like this to convert hex into byte string (define (word->bin s) (let ((n (string->number s))) (bytes (bitwise-and (arithmetic-shift n -24) #xFF) (bitwise-and (arithmetic-shift n -16) #xFF) (bitwise-and (arithmetic-shift n -8) #xFF) (bitwise-and n #xFF)))) (word->bin "#x10000002") I'm thinking of a similar function to convert binary into integers, then print it. The end result is the binary translated to hex. Some helpful links: http://download.plt-scheme.org/doc/372/html/mzscheme

Pascal's Triangle Row Sequence

好久不见. 提交于 2019-12-12 17:31:44
问题 I'm currently working on finding the row sequences of Pascal's triangle. I wanted to input the row number and output the sequence of numbers in a list up until that row. For example, (Pascal 4) would give the result (1 1 1 1 2 1 1 3 3 1) . I am trying to use an algorithm that I found. Here is the algorithm itself: V c = V c-1 * ((r - c)/c) r and c are supposed to be row and column, and V 0 =1. The algorithm can be specifically found on the wikipedia page in the section titled "Calculating and

Using let variables in a lambda in Scheme

喜你入骨 提交于 2019-12-12 16:42:03
问题 This question is similar in scope to: In R6RS Scheme, is there a way to get the current environment for use with eval? but I'd like to take it a step further and ask how you'd remedy something like this. My issue is further confounded in that in my case '(+ x y) is an arbitrary unevaluated lambda statement. Unevaluated because it may contain calls to variables that are part of the let (and since Scheme doesn't have faith that the procedure will be called in an environment containing those

unfold function in scheme

守給你的承諾、 提交于 2019-12-12 16:17:36
问题 Goal: implement unfold function using only two arguments. The arguments: the first argument is f which takes an initial value of some type I and returns nil or a cons pair of two elements (the first of these two is the next element that goes in the list of some type A and the next initial value again of some type I). The second argument is an initial value of some type I and the return is a list of items of type A. This is what I have so far and I am not sure why it is not working: (define

Scheme: why is Internal Definition faster than External Definition?

会有一股神秘感。 提交于 2019-12-12 15:46:31
问题 I tried running the program below (define (odd-internal x) (define (even x) (if (zero? x) #t (odd-internal (sub1 x)))) (if (zero? x) #f (even (sub1 x)))) (define (odd-external x) (if (zero? x) #f (even (sub1 x)))) (define (even x) (if (zero? x) #t (odd-external (sub1 x)))) (begin (display "Using internal definition\n") (time (odd-internal 40000000))) (begin (display "Using external definition\n") (time (odd-external 40000000))) This is the result in Racket Using internal definition cpu time: