i think i know the answer to this but ….
is comparing eq? of 2 numbers substantially faster than for 2 structs ?
eq? is reference equality, so its basicly comparing 2 numbers (or pointers or whatever) at some point anyway right ?
well i suppose technically im calling members-unique?
but the principle question is the same i think
“Yes”. An eq?
comparison is basically a pointer comparison. But … if you use eq?
to compare numbers, then you only get correct results for numbers that are representable with a pointer (i.e. fixnums). Use eqv?
instead to compare numbers. (It’s worth doubling checking in the docs - I can remember the Scheme semantics, but I have forgotten whether Racket has made any changes to eq?
.
TBH im just figuring out if there is a simple way to speed up my CSP process
but i feel like its probably a no, b/c the main factor is the search space
numbers was a stand in for another “simple” comparison instead of structs
If you use eq?
in structs, then (eq? a b)
will return true if the structs a and b occupy the same piece of memory. Two structs allocated at different times will not be eq?
even if they have the same values as members.
(define recipe-plan (make-csp))
(add-var! recipe-plan 'a (shuffle all))
(add-var! recipe-plan 'b (shuffle all))
(add-var! recipe-plan 'c (shuffle all))
(add-var! recipe-plan 'd (shuffle all))
(add-var! recipe-plan 'e (shuffle all))
(define (20%-fav? v w x y z)
(= 2 (count recipe-Favorite? (list v w x y z))))
(define (uniq? x y z)
(members-unique? (list x y z)))
this quickly reaches the point where solutions take “real human time”
and thats while running on my beefy MBP
eventually id like to move this code into iOS / Mobile env’s
If you need fast comparisons for structs, you can consider making a custom constructor like this:
For a struct foo
make a hash table that maps from elements to the struct.
In, say, create-foo
first look up whether the struct is already present in the hash - and return the same one.
with all
being a list of ~50 items, 50^5 i end up with a search space ~3.2M options, so i think any sort of computation on that many items will take real human time
If not make a new struct instance and store it in the hash before returning it.
Wait - shuffle
isn’t the shuffle
from racket/list
?
yes. but the input list of all
is only 50 items
To answer the original question, eq? on numbers and eq? on structs does the same thing and is basically equally fast
The csp
package looks pretty neat. https://docs.racket-lang.org/csp/index.html
Hadn’t come across it before.
yeah, it solves my use case perfectly. realy neatly.
but the perf might kill me :disappointed:
i think for now ill continue with the idea that the perf can be addressed later if the use-case is validated
so take
from racket base is (https://docs.racket-lang.org/collections/collections-api.html?q=take#%28def._%28%28lib._data%2Fcollection..rkt%29._take%29%29\|take list n) but take
from data/collections is (https://docs.racket-lang.org/collections/collections-api.html?q=take#%28def._%28%28lib._data%2Fcollection..rkt%29._take%29%29\|take _n_ _seq_)
also, this is a bit odd
clever.rkt> (sequence? plans)
#t
clever.rkt> (first plans)
; first: contract violation
; expected: sequence?
; given: #<sequence>
; in: an and/c case of
; the 1st argument of
; (-> (and/c sequence? (not/c empty?)) any)
; contract from:
; <pkgs>/collections-lib/data/collection/collection.rkt
; blaming: /Users/chrismatheson/Code/open-sauce/clever.rkt
; (assuming the contract is correct)
; at: <pkgs>/collections-lib/data/collection/collection.rkt:33.3
; Context:
; /Applications/Racket v7.2.0.6/collects/racket/repl.rkt:11:26
its passing sequence? but not the contract ??
Maybe it is a case of having two different structs both named sequence?
hmmmm quite possibly
it was my thinkinging, but i initially thought that sequence?
must be the same as in the contract
but i suppose tht might have been pulled into my scope the same way as the “new” sequence struct
It’s worth trying on a newer version of Racket - just to rule out any old bugs.
oh, am i on an older version?
i thought 7.2 was basicly the latest release
7.6 is the latest
oh yeah! just saw
is of to update
thanks :smile:
good luck!
Quick question about Typed Racket: I have this simple example module: #lang typed/racket
(require syntax/parse)
(define (foo stx)
(syntax-parse stx
[(stx)
(display "ok")]))
but I get this error when I run it: Type Checker: missing type for identifier;
consider using `require/typed' to import it
identifier: normalize-context
from module: syntax/parse/private/residual in: (syntax-parse stx ((stx) (display "ok")))
normalize-context
is not provided by syntax-parse
so using require/typed
to import it doesn’t make sense
There probably is an easy explanation but I’m new to Typed Racket and I’ve spent half the day looking for an answer in the docs and other places but I can’t figure out where to look
Typed Racket is unlikely to work well for code that uses syntax-parse. Typically if you use it in a macro then it’s fine because TR just looks at the expanded code.