chris613
2020-3-4 16:04:09

i think i know the answer to this but ….


chris613
2020-3-4 16:04:40

is comparing eq? of 2 numbers substantially faster than for 2 structs ?


chris613
2020-3-4 16:05:10

eq? is reference equality, so its basicly comparing 2 numbers (or pointers or whatever) at some point anyway right ?


chris613
2020-3-4 16:06:22

well i suppose technically im calling members-unique? but the principle question is the same i think


soegaard2
2020-3-4 16:07:54

“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? .


chris613
2020-3-4 16:08:33

TBH im just figuring out if there is a simple way to speed up my CSP process


chris613
2020-3-4 16:08:53

but i feel like its probably a no, b/c the main factor is the search space


chris613
2020-3-4 16:09:26

numbers was a stand in for another “simple” comparison instead of structs


soegaard2
2020-3-4 16:09:31

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.


chris613
2020-3-4 16:10:18

(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”


chris613
2020-3-4 16:10:30

and thats while running on my beefy MBP


chris613
2020-3-4 16:10:56

eventually id like to move this code into iOS / Mobile env’s


soegaard2
2020-3-4 16:12:15

If you need fast comparisons for structs, you can consider making a custom constructor like this:


soegaard2
2020-3-4 16:12:53

For a struct foo make a hash table that maps from elements to the struct.


soegaard2
2020-3-4 16:13:30

In, say, create-foo first look up whether the struct is already present in the hash - and return the same one.


chris613
2020-3-4 16:13:40

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


soegaard2
2020-3-4 16:13:45

If not make a new struct instance and store it in the hash before returning it.


soegaard2
2020-3-4 16:14:17

Wait - shuffle isn’t the shuffle from racket/list ?


chris613
2020-3-4 16:14:45

yes. but the input list of all is only 50 items


samth
2020-3-4 16:15:14

To answer the original question, eq? on numbers and eq? on structs does the same thing and is basically equally fast


soegaard2
2020-3-4 16:24:28

The csp package looks pretty neat. https://docs.racket-lang.org/csp/index.html


soegaard2
2020-3-4 16:24:42

Hadn’t come across it before.


chris613
2020-3-4 16:25:36

yeah, it solves my use case perfectly. realy neatly.


chris613
2020-3-4 16:25:44

but the perf might kill me :disappointed:


chris613
2020-3-4 16:26:45

i think for now ill continue with the idea that the perf can be addressed later if the use-case is validated



chris613
2020-3-4 16:33:42

also, this is a bit odd


chris613
2020-3-4 16:33:53

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


chris613
2020-3-4 16:34:13

its passing sequence? but not the contract ??


soegaard2
2020-3-4 16:35:07

Maybe it is a case of having two different structs both named sequence?


chris613
2020-3-4 16:35:23

hmmmm quite possibly


chris613
2020-3-4 16:35:55

it was my thinkinging, but i initially thought that sequence? must be the same as in the contract


chris613
2020-3-4 16:36:18

but i suppose tht might have been pulled into my scope the same way as the “new” sequence struct


soegaard2
2020-3-4 16:37:50

It’s worth trying on a newer version of Racket - just to rule out any old bugs.


chris613
2020-3-4 16:38:30

oh, am i on an older version?


chris613
2020-3-4 16:38:44

i thought 7.2 was basicly the latest release


hazemalhalabi
2020-3-4 16:38:59

7.6 is the latest


chris613
2020-3-4 16:39:03

oh yeah! just saw


chris613
2020-3-4 16:39:12

is of to update


chris613
2020-3-4 16:39:17

thanks :smile:


hazemalhalabi
2020-3-4 16:39:43

good luck!


hazemalhalabi
2020-3-4 22:05:38

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


samth
2020-3-4 22:52:15

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.