racket

Functional variant of 'oneof' function in Racket

孤街浪徒 提交于 2019-11-28 05:42:51
问题 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

Building accumulator for lazy lists in Racket

空扰寡人 提交于 2019-11-28 05:12:21
问题 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

Is there a way to view a function's source code from within the Racket REPL?

半城伤御伤魂 提交于 2019-11-28 04:34:41
问题 I'm currently trying to dive into Racket/Scheme a bit. I have an instance of the (X)REPL running next to my editor, which helps me immensely to explore the language. However, I can't seem to find an XREPL command or macro (or whatever) which would show me the source code of a function. All the needed parts seem to be there: XREPL's describe command knows the file: -> ,describe string-join ; `string-join' is a bound identifier, ; defined in racket/string.rkt ; required directly and get

Collection of Great Applications and Programs using Macros

我与影子孤独终老i 提交于 2019-11-28 02:49:07
I am very very interested in Macros and just beginning to understand its true power. Please help me collect some great usage of macro systems. So far I have these constructs: Pattern Matching: Andrew Wright and Bruce Duba. Pattern matching for Scheme, 1995 Relations in the spirit of Prolog: Dorai Sitaram. Programming in schelog. http://www.ccs.neu.edu/home/dorai/schelog/schelog.html Daniel P. Friedman, William E. Byrd, and Oleg Kiselyov. The Reasoned Schemer. The MIT Press, July 2005 Matthias Felleisen. Transliterating Prolog into Scheme. Technical Report 182, Indiana University, 1985.

How do I pass a list as a list of arguments in racket?

五迷三道 提交于 2019-11-27 23:30:37
问题 I have a statement like this: ((lambda (a b c) (+ a b c)) 1 2 3) ; Gives 6 And I would like to be able to also pass it a list as so: ((lambda (a b c) (+ a b c)) (list 1 2 3)) ...except this doesn't work because the entire list is passed as 'a.' Is there is a way to explode the list into arguments? What I'm looking for is something similar to the * character in Python. For those of you unfamiliar with the syntax: def sumthree(a, b, c): print a + b + c sumthree(1, 2, 3) # Prints 6 sumthree(*(1,

set-car!, set-cdr! unbound in racket?

淺唱寂寞╮ 提交于 2019-11-27 22:50:10
I am just trying to do very simple code with set-car! and set-cdr! in racket , but I got the error: expand: unbound identifier in module in: set-car! and expand: unbound identifier in module in: set-cdr! Aren't they defined in racket ? Could anyone help? You need to import mutable-pairs-6 , like this: (require rnrs/mutable-pairs-6) Those procedures were moved to a different module and renamed to mcons , mcar , mcdr , set-mcar! , set-mcdr! , mlist to emphasize that they operate on mutable data, unlike their immutable counterparts. Óscar López's answer is correct, but doesn't explain why normal

How to test if two functions are the same?

主宰稳场 提交于 2019-11-27 21:54:08
问题 I found a code snippet somewhere online: (letrec ([id (lambda (v) v)] [ctx0 (lambda (v) `(k ,v))] ..... ..... (if (memq ctx (list ctx0 id)) <---- condition always return false ..... where ctx is also a function: However I could never make the test-statement return true. Then I have the following test: (define ctx0 (lambda (v) `(k ,v))) (define ctx1 (lambda (v) `(k ,v))) (eq? ctx0 ctx1) => #f (eqv? ctx0 ctx1) => #f (equal? ctx0 ctx1) => #f Which make me suspect that two function are always

Little Schemer and Racket

纵饮孤独 提交于 2019-11-27 20:24:03
问题 I'm starting to read the Little Schemer and now instead of PLT Scheme we have Racket. I would like to know if Racket is suitable for doing the exercises in the book or do I need to get another true Scheme compiler. Before I forgot to tell you, my OS is Windows x64. The book, language and paradigm is complex enough, I would love to avoid struggling with a compiler. Thanks a lot in advance. 回答1: DrRacket is the (r)evolution of DrScheme; DrRacket will work perfectly for the exercises in "The

a tail-recursion version list appending function

别说谁变了你拦得住时间么 提交于 2019-11-27 16:05:54
i see several examples of implementing append an element to a list, but all are not using tail recursion . how to implement such a function in a functional style? (define (append-list lst elem) expr) The following is an implementation of tail recursion modulo cons optimization, resulting in a fully tail recursive code. It copies the input structure and then appends the new element to it, by mutation, in the top-down manner. Since this mutation is done to its internal freshly-created data, it is still functional on the outside (does not alter any data passed into it and has no observable

How do I find the index of an element in a list in Racket?

ⅰ亾dé卋堺 提交于 2019-11-27 13:47:08
This is trivial implement of course, but I feel there is certainly something built in to Racket that does this. Am I correct in that intuition, and if so, what is function? Strangely, there isn't a built-in procedure in Racket for finding the 0-based index of an element in a list (the opposite procedure does exist, it's called list-ref ). However, it's not hard to implement efficiently: (define (index-of lst ele) (let loop ((lst lst) (idx 0)) (cond ((empty? lst) #f) ((equal? (first lst) ele) idx) (else (loop (rest lst) (add1 idx)))))) But there is a similar procedure in srfi/1 , it's called