Is there a simpler way to achieve threading functionality in racket? I know about the threading library but it seems like such a basic functionality that I wonder if there is not some builtin way to do this.
(define (thread x . fns) (foldl (lambda (f a) (f a)) thread fns))
Also, can you express
(lambda (f a) (f a)) in a simpler fashion?
There are lots of ways of doing this. A nice one is to use a macro, a simple (and perhaps not completely correct) version of which is:
(define-syntax (/> stx) (syntax-case stx () [(_ x) #'x] [(_ x f) #'(f x)] [(/> x f fs ...) #'(/> (f x) fs ...)] [/> (identifier? #'/>) #'(λ (x . fns) (for/fold ([r x]) ([f fns]) (f r)))]))
Now, for instance
(/> x sin cos)is expanded to
(cos (sin x)): there is no run-time overhead at all. The last clause means that
(apply /> 1 (list sin cos))will work.
I'm not sure the above macro is completely correct, particularly the last clause.
See also questions close to this topic
Guile Scheme GOOPS slot accessor is not a valid generic function error
I'm learning Guile GOOPS. I have a problem with a slot accessor. The code below
(use-modules (oop goops)) (define-class <person> () (name #:init-keyword #:name #:accessor person-name)) (let ([p (make <person> #:name "Vlad")]) (display (person-name p)))
Gives the error: oop/goops.scm:1585:2: #<procedure %person-name-procedure (s)> is not a valid generic function
What I'm doing wrong?
Dr. Racket and R5RS Scheme: (current-directory) is not working
I am trying to use R5RS, in Dr.Racket. I want to know the directory in which I am working and would like to change the directory. When I try in R5S5, I get the following error:
Welcome to DrRacket, version 6.12 [3m]. Language: R5RS; memory limit: 128 MB. > (current-directory) . . current-directory: undefined; cannot reference undefined identifier >
However, running the same thing in Racket, I am getting the directory.
Welcome to DrRacket, version 6.12 [3m]. Language: racket, with debugging; memory limit: 128 MB. > (current-directory) #<path:/home/shree/> >
I am not able to find what is the corresponding primitive for
Racket function for finding minimum element of list given a function
Is there some function in Racket(Scheme) that can give me the element, which produces the minimum value after applying a given function in a list. Something like
(apply min (map f list))but returning the element before applying f. For example:
(define lst '((1 . 3) (3 . 8) (5 . 6)) (define (f pair) (- (cdr pair) (car pair))) (minimum f lst)
This should give
'(5 . 6).
There should be a one liner for this with higher order functions at least if there is no direct function(as I had to fill in one line to get this behaviour). Any ideas?
How do I emulate Python-style functions in C++?
I am looking for a way to have modifiable-length lists in C++ that can have different types within them. A solution for this could be using boost::any with std::vector, but that doesn't support array types. My intent for this is a way to hold lisp-like S-expressions with nested lists and atoms, but it's really hard to implement in C++. If you know of a library that has a solution, or if you have your own, please let me know!
Recursion tracing in lisp... could use some advice
Could someone spare a moment to help me understand why this keeps evaluating to true throughout this small function in Lisp?
(defun fob (n) (if (= n 10) 'true (fob (+ n 1))))
Keeps evaluating on the trace to:
0: (FOB 5) 1: (FOB 6) 2: (FOB 7) 3: (FOB 8) 4: (FOB 9) 5: (FOB 10) 5: FOB returned TRUE 4: FOB returned TRUE 3: FOB returned TRUE 2: FOB returned TRUE 1: FOB returned TRUE 0: FOB returned TRUE
I don't understand why it's returning 'true' even though obviously 6 does not equal 10. Am I just reading the trace wrong? Any help would be appreciated. :)
Portacle - why does function intellisense stop working when I visit a file (it works in the repl)
I am very new to emacs and lisp, so I am starting with portacle. When it starts up in the repl I can define functions and then when I use them I get some intellisense at the bottom referring to the parameter names. However, when I open a file intellisense does not work for the new file.
How do I get intellisense (parameter names of a function) to work for a file the same way it works when I am in the repl?
Attempt to call `super` outside of overriding method in the trait which has two "mutually recursive" methods, but failed
Recently I'm learning Racket's trait system.
I did some experiments about
supercall in two mutually recursive methods:
I first experimented with mixin, it works as expected:
#lang racket (define fish% (class object% (super-new) (define/public (get-color) (println "fish% get-color")) (define/public (get-price) (println "fish% get-price")) )) (define (spots-mixin %) (class % (super-new) (define/override (get-color) (println "spots-mixin get-color") (super get-price)) (define/override (get-price) (println "spots-mixin get-price") (super get-color)) )) (define spots% (spots-mixin fish%)) (send (new spots%) get-color) (send (new spots%) get-price) ; Output: ; "spots-mixin get-color" ; "fish% get-price" ; "spots-mixin get-price" ; "fish% get-color"
Then I experimented with the trait, but failed:
#lang racket (require racket/trait) (define fish% (class object% (super-new) (define/public (get-color) (println "fish% get-color")) (define/public (get-price) (println "fish% get-price")) )) (define spots-trait (trait (define/override (get-color) (println "spots-trait get-color") (super get-price)) (define/override (get-price) (println "spots-trait get-price") (super get-color)) )) (define spots% ((trait->mixin spots-trait) fish%)) (send (new spots%) get-color) (send (new spots%) get-price)
The REPL told me:
; class*: superclass does not provide an expected method for override ; override name: member68863 ; class name: ...cts/racket/trait.rkt:367:17
I just define 2 methods in the super class
fish%and 2 methods in the trait
spots-traitto override them.
superclass does not provide an expected method for override?
Is there a way to fix this error?
BTW, if I declare an additional
(inherit/super get-color get-price)in the
spots-trait, the REPL will complain:
get-color: duplicate definition of external name in trait ; in: get-color
HTDP2e Exercise 65 Write down the names of the functions (constructors, selectors, and predicates)
I took the following question from HTDP2e (exercise 65):
Exercise 65. Take a look at the following structure type definitions:
(define-struct movie [title producer year])
Write down the names of the functions (constructors, selectors, and predicates).
My Answer is:
make-movie is a constructor. movie? is a predicate. movie-title, movie-producer, and movie-year are selectors. So
(define-struct movie [title producer year]) (define-struct M-1 (make-movie "Parasite" "Bong Joon-ho" "2019")) (movie-title M-1) ; Parasite (movie-producer M-1) ;Bong Joon-ho (movie-year M-1) ; 2019
But I got an error: "define-struct: expected a field name, but found a string" Can you help me? How can I edit my codes?
Dynamically generating rackunit test-suite: tests pass and also raise exception
I am trying to dynamically generate a bunch of test cases.
Basically I want to run the same tests several times with different values. Maybe there's a better way to do that, if so let me know, I haven't found it yet.
make-test-suitewhich says you can pass it a list of
So I made a
for/foldloop which collects the test cases generated for each set of values into a flat list.
What I don't understand is it seems to successfully run all the tests while also raising an exception:
tests->test-suite-action received #<void> in list of tests (#<void> #<void> #<void> #<void> #<void> #<void> #<void> #<void>), which is not a test. context...: /usr/share/racket/pkgs/rackunit-lib/rackunit/private/test-suite.rkt:139:7 /usr/share/racket/pkgs/rackunit-lib/rackunit/private/test-suite.rkt:136:2 /usr/share/racket/pkgs/rackunit-lib/rackunit/private/test-suite.rkt:61:0: apply-test-suite /usr/share/racket/pkgs/rackunit-lib/rackunit/text-ui.rkt:91:0: run-tests "/write-thru-hash/tests.rkt": [running body] temp35_0 for-loop run-module-instance! /usr/share/racket/pkgs/compiler-lib/compiler/commands/test.rkt:179:16 ... 1 1 write-thru-hash/tests.rkt 8 tests passed rkt-write-thru-hash_tests_1 exited with code 1
I put a
writelnin each of my test cases and I see those lines printed out where I abbreviated with
...above. So I know the tests are actually running.
(The tests also ran and worked fine before I multiplied them with the
for/foldloop and just built them in the body of
tests.rktcode looks like:
(define test-cases-list (for/fold ([test-cases (list)]) ([db-type (list 'sqlite 'postgres)]) (append test-cases (list (test-case/fixture "basic operations" ... ) (test-case/fixture "complex serializable keys and values" ... ) (test-case/fixture "custom table-name" ... ) (test-case/fixture "use initial src-hash" ... ))))) (define db-tests (make-test-suite "db-tests" test-cases-list))
(I'm using the
Actually I think the
writelnin my test cases are printing at definition time ... i.e. they are running too early because they are outside of the
I guess the tail
#<void>, so I have filled a list with the results of the tests (voids) rather than the test cases themselves and given that to the test suite, hence the error.
But then I don't see how to actually use