raoul.schorer
2021-3-12 12:13:17

@soegaard2 Hi, I’m making packed-vectors (int2, int4, etc.) the implementation of which is heavily inspired by your bit-vector. I am wondering what the purpose of the ‘default’ arg in bit-vector-ref is. And more generally, why have a guarded version of bit-vector-ref ? Is it to ensure safety internal to the module, since you could guard unsafe-bit-vector-ref under a contract for requiring modules? Thanks!


soegaard2
2021-3-12 12:35:15

@raoul.schorer The argument default allows the user to decide, what values is returned, when an out of range index is referenced. I believe, the intention was to make bit-vector-ref behave the same way as hash-ref.


soegaard2
2021-3-12 12:36:54

And yes, bit-vector-ref is exported, but unsafe-bit-vector-ref isn’t.


soegaard2
2021-3-12 12:38:38

I guess, one could move the check to a contract.


raoul.schorer
2021-3-12 12:45:51

I suspected it was to match gen:dict in some way! I see that standard vectors apparently don’t implement that. I’ll have to think about that. What I meant regarding unsafe-bit-vector-ref was: couldn’t one just implement the unsafe version and export it as safe by guarding it under contracts, at the expense of internal module safety? (i.e. the user of the module is safe, but the module implementor must be careful because the procedure is unsafe to use internal to the module)


soegaard2
2021-3-12 12:47:25

Yes, that’s a fine way of doing it. I think, I bolted contracts on as the last step. So it made sense to have the check while, testing the library.


soegaard2
2021-3-12 12:53:49

Hmm. I think, you are right about gen:dict I see a #:prop:dict/contract in the code. I had forgotten, there were dict support.


raoul.schorer
2021-3-12 13:02:52

Currently, I am planning to implement gen:dict, but I think I’ll leave the ‘default’ behaviour to dict-ref and not implement it in my packed-vector-ref. I can’t find the source for standard vectors, but seeing that vector-ref doesn’t seem to implement ‘default’ either I suppose that’s the way it was done.


joel
2021-3-12 13:54:08

Anyone here familiar with https://docs.racket-lang.org/data/Enumerations.html?q=Enumerations#%28def._%28%28lib._data%2Fenumerate%2Flib..rkt%29._set%2Fe%29%29\|enumerations? I’m trying to figure out how to build an enumeration of lists of between 1 and n words, where each word is selected from one of n groups and each group is used no more than once.


joel
2021-3-12 14:00:09

So if the words are grouped by what letter they start with, the enumeration would produce ("cc" "a" "bbbbb") but never ("cc" "a" "c") (since that would draw from the C group more than once)


joel
2021-3-12 14:03:07

I could use list/e if I’m willing to limit results to lists of a particular length, or set/e if I don’t care about all possible orderings of a particular set of values. But I’d prefer to have all possible orderings and all possible lengths.


bedeke
2021-3-12 14:09:56

(define (enumerate . lsts) (cond [(null? lsts) ’()] [(null? (cdr lsts)) (map list (car lsts))] [else (define l1 (car lsts)) (apply append (map (λ (x) (map (λ (y) (cons x y)) (apply enumerate (cdr lsts)))) l1))]))


bedeke
2021-3-12 14:10:25

> (enumerate ’(1 2) ’(a b)) ’((1 a) (1 b) (2 a) (2 b))


bedeke
2021-3-12 14:13:48

and if you want different orders: (map (λ (x)(apply enumerate x)) (permutations (list'(1 2) '(a b)))) '(((1 a) (1 b) (2 a) (2 b)) ((a 1) (a 2) (b 1) (b 2)))


bedeke
2021-3-12 14:18:52

or if you want to be able to specify the number of lists: #lang racket/base (define (enumerate lsts) (cond [(null? lsts) '()] [(null? (cdr lsts)) (map list (car lsts))] [else (define l1 (car lsts)) (apply append (map (λ (x) (map (λ (y) (cons x y)) (enumerate (cdr lsts)))) l1))])) (define (permutations LST [nr (length LST)]) (if (<= 1 nr (length LST)) (apply append (for/list ([i (in-list LST)]) (define D (permutations (remove i LST) (sub1 nr))) (if (null? D) (list (list i)) (map (λ (x) (cons i x)) (permutations (remove i LST) (sub1 nr)))))) '())) (map enumerate (permutations (list '(1 2) '(a b) '("$" "µ")) 2)) '(((1 a) (1 b) (2 a) (2 b)) ((1 "$") (1 "µ") (2 "$") (2 "µ")) ((a 1) (a 2) (b 1) (b 2)) ((a "$") (a "µ") (b "$") (b "µ")) (("$" 1) ("$" 2) ("µ" 1) ("µ" 2)) (("$" a) ("$" b) ("µ" a) ("µ" b)))


joel
2021-3-12 14:41:14

I’m pretty clear on how to do it with normal lists :slightly_smiling_face: The enumerations library I linked allows you to make an efficient bijection between natural numbers and the set of possible results in a space, without computing the entire space ahead of time.


ryanc
2021-3-12 14:43:29

There are packed numeric vectors of various types in ffi/vector.


anurag.rawat0201
2021-3-12 16:37:25

@anurag.rawat0201 has joined the channel


anurag.rawat0201
2021-3-12 16:38:33

Hi all


anurag.rawat0201
2021-3-12 16:39:45

Can anyone please help me with scheme?


anurag.rawat0201
2021-3-12 16:43:16

I want to write a function called replace-indices, that takes a list of characters; list of integers representing indices where values 𝑛 ∈ [0. . 𝐿), L the length of the list, and a char, yields a new list obtained from the original where the character at each of the given indices is replaced by the given char.


anurag.rawat0201
2021-3-12 16:44:40

> (replace-indices '(#\a #\* #\*) '(1 2) #\b (#\a #\b #\b)


massung
2021-3-12 16:47:05

Not sure if there are any “rules” of the channel, but this very much sounds like a school assignment/homework. Plenty of people are willing to help, but most likely none here will “do it for you”. You should instead post an attempt and then let people help correct mistakes/teach.


anurag.rawat0201
2021-3-12 16:47:37

sure I can share what I did


mflatt
2021-3-12 16:47:56

I’ve set up a minimal page to access the heartbeats that each Racket service registers: https://heartbeat.racket-lang.org/

If someone wants to make a nicer web page that shows the referenced JSON content and highlights services that haven’t succeeded in 24 hours, I’d be happy to drop it into place.

The pkg-build service had gotten stuck because it ran out of disk space. I cleared out some space and reset it, and then 25% of the disk became freed later — suggesting a large Docker container that was reset, or something like that. Maybe more is needed to avoid running out of space again.


anurag.rawat0201
2021-3-12 16:49:14

(define (replace lst old new) ;; (cond [(empty? lst) empty] [(empty? old) lst] [(equal? (indices lst (car lst)) (list (car old))) (cons new (replace (rest lst) (rest old) new))] [else (cons (first lst) (replace (rest lst) (rest old) new))]))


massung
2021-3-12 16:50:43

that’s not a bad attempt


anurag.rawat0201
2021-3-12 16:50:44

indices on line 5 is another function I created, which is similar to indexes-of (in-build func in racket) **


anurag.rawat0201
2021-3-12 16:51:18

I’m really new to this.. and have been struggling a lot to be honest… would really appreciate some help


massung
2021-3-12 16:52:55

instead of the indices function, try it with your function signature looking like this: (define (replace lst indices with current-index) ...) You’d call it like so: (replace '(#\a #\* #\*) '(1 2) #\b 0) How do you think your solution might change then?


anurag.rawat0201
2021-3-12 16:54:16

but the number of arguments aren’t the same no?


anurag.rawat0201
2021-3-12 16:54:40

we’re using 3 arguments in replace function


massung
2021-3-12 16:55:02

No, but that’s okay for now. You could name the function “replace-helper” and just have “replace” call it. Or you could make the last argument optional (with a default value of 0).


massung
2021-3-12 16:55:24

What’s more important is understanding the algorithm and getting something that works. Then riffing off that to get something acceptable for the class.


massung
2021-3-12 16:56:35

(note: there are much easier solutions in racket to this problem, but i’m trying to stay true to your original attempt)


anurag.rawat0201
2021-3-12 16:59:16

As I’m very new to the scheme, the majority of things are out of my knowledge.


massung
2021-3-12 16:59:30

Yep. All good. :slightly_smiling_face:


anurag.rawat0201
2021-3-12 17:00:32

Just to confirm, the function name and the arguments are it has, I cannot change them


massung
2021-3-12 17:01:19

Sure, but you made a function called indices to help you, so just make the function above called replace-helper and then make replace just call it.


anurag.rawat0201
2021-3-12 17:01:42

can you tell me what’s exactly wrong with my attempt? which line exactly


massung
2021-3-12 17:03:43

I can’t know exactly because I don’t know what your indices function does. But pretty sure the equal? and the else conditions don’t do what you want.


anurag.rawat0201
2021-3-12 17:04:34

> (indices '(#\a #\b #\b) #\b) (1 2)


anurag.rawat0201
2021-3-12 17:05:00

it just return the index value of the desired item


massung
2021-3-12 17:05:11

But you’re attempt is on the right track. You basically have.. • is the original list empty? done • is the indices list empty? done, return the rest of the original list as is • … ? • otherwise, take the first item of the original as-is and recurse with the rest


massung
2021-3-12 17:06:19

That 3rd bullet point is where you need something a bit easier… You basically want “is the current item the next index to update?” (assuming the list of indices is always sorted)


laurent.orseau
2021-3-12 17:06:59

(pushed to master. Will be available in the define2 package when the server has caught up with it.)


anurag.rawat0201
2021-3-12 17:07:55

I wish the list would contain integers, only then it can be sorted (if I’m not wrong)…. it only takes characters


massung
2021-3-12 17:09:06

But as you recurse, you have this problem… • first call arguments == '(a * *) '(1 2) b • recursive call arguments == '(* *) '(2) b


massung
2021-3-12 17:10:09

Think about what you want the call arguments to be each time around… • first call == '(a * *) '(1 2) b • second call should be '(* *) '(1 2) b • third call should be '(*) '(2) b


massung
2021-3-12 17:10:59

you only should remove the “index” to replace if there was a match


ben.knoble
2021-3-12 17:11:38

^well, remove the index after processing the item at that index, regardless of a match or not


massung
2021-3-12 17:11:54

+1


anurag.rawat0201
2021-3-12 17:13:29

Sorry but the purpose of the function is to replace the existing elements in the list with the given element based on the given list of indexes.


massung
2021-3-12 17:15:24

Yes, I understand the problem. :slightly_smiling_face:


anurag.rawat0201
2021-3-12 17:20:01

do I need to use let here?


massung
2021-3-12 17:36:03

you could use let to create a helper function if that’s what you mean. not sure what to do beyond what i have suggested.


anurag.rawat0201
2021-3-12 17:37:27

so basically I shall create a helper function for replacing the values (if that’s correct) ?


massung
2021-3-12 17:37:48

you dont need to, but it will be much easier (given your current approach) if you do


anurag.rawat0201
2021-3-12 17:38:47

I basically not sure I do I actually write that


massung
2021-3-12 17:39:54

example: (define (say-something msg) (let ([helper (lambda (x) (print x))]) (helper msg)))


anurag.rawat0201
2021-3-12 17:40:51

okay I see


anurag.rawat0201
2021-3-12 17:41:16

And what about the main function, that’ll be used just for getting the index values


anurag.rawat0201
2021-3-12 17:41:17

?


massung
2021-3-12 17:42:59

I strongly recommend trying to write it with the ‘current-index’: (replace lst indices with current-index) Maybe that version of replace is your helper, whatever.


anurag.rawat0201
2021-3-12 17:54:20

alright, I’ll give it a try


anurag.rawat0201
2021-3-12 17:54:27

Thanks for the help


jestarray
2021-3-12 19:52:32

trying to use copy-file and i get access is denied win_err=5


jestarray
2021-3-12 19:56:42

tried running as admin and get the same issue :s


anurag.rawat0201
2021-3-12 19:58:09

I am sorry, i am very new and struggling to understand.


anurag.rawat0201
2021-3-12 19:59:32

(define (replace lst old new) ;; (cond [(empty? lst) empty] [(empty? old) lst] [(equal? (indices lst (car lst)) (list (car old))) (cons new (replace (rest lst) (rest old) new))] [else (cons (first lst) (replace (rest lst) (rest old) new))]))


anurag.rawat0201
2021-3-12 20:00:23

(define (indices word char) (let loop ((word word) (ol '()) (idx 0)) (cond [(empty? word) (reverse ol)] [(equal? (car word) char) (loop (rest word) (cons idx ol) (+ 1 idx))] [else (loop (rest word) ol (+ 1 idx))] ) ) )


jestarray
2021-3-12 20:01:05

nevermind, i didnt know the destination had to have a filename specified


jestarray
2021-3-12 20:01:38

e.g copying src/test.png to public/ , i thought copy-file would just omit the need for putting in test.png


massung
2021-3-12 22:20:31

Assuming you may have been at this for a while now, I’ll share a solution close to what you started with, along with comments, so you can see… (define (replace lst indices with [index 0]) (cond [(empty? lst) lst] [(empty? indices) lst] ; is the current index the same as the next index to replace? [(= index (car indices)) ; replace, pop index, advance to next index (cons with (replace (cdr lst) (cdr indices) with (+ index 1)))] ; don't replace, advance to next index, but don't pop indices [else (cons (car lst) (replace (cdr lst) indices with (+ index 1)))])) Please don’t just copy/paste this as a solution as opposed to learning from it. If you have questions, please feel free to ask. :wink:


anurag.rawat0201
2021-3-12 23:11:04

Okay, firstly thank you so much for the help and secondly, I pretty much understand what going on in each line, and yes, an increment on the index does make sense. But my question is where in this function have we passed the ‘new’ (the value that it’ll be replaced with)


anurag.rawat0201
2021-3-12 23:17:01

So, what I understood is that - (replace '(a b c d) '(1 2) e) suppose this is my function, we pass: • list containing characters • list containing indexes of characters in list 1 (as desired by the user) • the element that will replace the chars (in this case - (b c) ) Result - (a e e d)


anurag.rawat0201
2021-3-12 23:18:31

So, acc to. my solution, i think I don’t need to touch the base case. Problem is in the recursion step


massung
2021-3-12 23:20:16

> But my question is where in this function have we passed the ‘new’ (the value that it’ll be replaced with) It’s the with argument


anurag.rawat0201
2021-3-12 23:21:07

omg, with was argument.. oh I’m sorry


anurag.rawat0201
2021-3-12 23:21:37

I thought you wrote the pseudocode


anurag.rawat0201
2021-3-12 23:22:17

I’m so sorry for being stupid


massung
2021-3-12 23:22:38

no problem. One possible issue with the solution i provided is that it assumes indices is a sorted list. if it isn’t sorted, you wouldn’t pop on match, and you wouldn’t compare against the car of it either (as opposed to just checking if index was in the list of indices, like you were doing in your original version


massung
2021-3-12 23:23:08

> I’m so sorry for being stupid No, we all have those moments. I had one literally 15 minutes ago w/ something else. :slightly_smiling_face:


anurag.rawat0201
2021-3-12 23:23:49

But, I really appreciate your time and help


massung
2021-3-12 23:24:43

as long as you pay it forward, it’s my pleasure :wink:


anurag.rawat0201
2021-3-12 23:25:48

Sir, I’m so sorry but I think I still don’t get it


massung
2021-3-12 23:27:03

just paste the code you dont follow, say what you think is happening, and ill do my best


anurag.rawat0201
2021-3-12 23:27:11

isn’t indices already a list containing “index”


massung
2021-3-12 23:28:24

here.. answer this question:

Given a list of stuff (e.g. (a b c d)), how do you know what index c is at?


anurag.rawat0201
2021-3-12 23:28:25

No, it’s the logic basically that I’m not getting


anurag.rawat0201
2021-3-12 23:29:25

Okay, so for this what I would do (using recursion) is :


anurag.rawat0201
2021-3-12 23:32:22

[(equal? (car stuff) idx) (cons (car stuff) (function_name (cdr stuff) idx))]


anurag.rawat0201
2021-3-12 23:32:28

something like this


massung
2021-3-12 23:33:48

so, idx is what you’re trying to find out, so you can’t really test against it (for the “what index is this?” question)


massung
2021-3-12 23:34:22

the recursion idea is good, and testing the first element, or then calling agin with the rest is good


anurag.rawat0201
2021-3-12 23:36:01

shouldn’t the increment of index be inside indices list (2nd argument)?


anurag.rawat0201
2021-3-12 23:36:42

otherwise won’t it give the arity mismatch error ?


massung
2021-3-12 23:36:44

im trying to explain the algorithm by using a totally differnet problem


anurag.rawat0201
2021-3-12 23:37:06

okay


massung
2021-3-12 23:37:27

a completely separate problem is stated above: given a list, what is the index of an item in that list?


anurag.rawat0201
2021-3-12 23:37:56

i see, so the code I wrote is it incorrect ?


massung
2021-3-12 23:38:56

here’s a solution… tell me if you understand it: (define (index-of lst item [index 0]) (if (eq? (car lst) item) index (index-of (cdr lst) item (+ index 1))))


anurag.rawat0201
2021-3-12 23:41:29

firstly, to make sure we’re on the same page.. I’m using Dr. Racket with (Pretty Big Language). Is that what you’re using too? Also, when you pass [index 0] as an argument, that something that I’m unaware of..


massung
2021-3-12 23:42:13

the [index 0] just means an argument named index with a default value of 0 if you don’t pass a value in for it


massung
2021-3-12 23:42:28

I don’t know “Pretty Big Language”, just #lang racket


anurag.rawat0201
2021-3-12 23:42:41

okay


anurag.rawat0201
2021-3-12 23:42:50

I get what you wrote there


anurag.rawat0201
2021-3-12 23:43:41

the code* of index-of


massung
2021-3-12 23:44:50

ok. so then, the problem you have is almost the same. given a list, what is the index of each element in that list?


massung
2021-3-12 23:45:13

and then, once you know that, replace the items with an index that happens to also be in another list


massung
2021-3-12 23:46:14

as a good warm-up to the final one.. the idea would be to change index-of and instead of returning the index, how about - instead - making it print the item it finds at a given index


anurag.rawat0201
2021-3-12 23:46:57

define: not an identifier for procedure argument in: (index 0) gives me this error


massung
2021-3-12 23:47:01

(define (print-index-of lst index-to-print [this-index 0]) ;; what goes here? )


massung
2021-3-12 23:47:52

> define: not an identifier for procedure argument in: (index 0) I guess your #lang doesn’t allow for that. just change it to index and just pass 0 when you call it


anurag.rawat0201
2021-3-12 23:51:10

should I just (define index 0) just above the replace function ?


massung
2021-3-12 23:51:31

no


massung
2021-3-12 23:51:45

just change the argument to index and pass 0 yourself


anurag.rawat0201
2021-3-12 23:57:25

(define (replace lst indices with index) (cond [(empty? lst) lst] [(empty? indices) lst] [(= 0 (car indices)) (cons with (replace (cdr lst) (cdr indices) (+ 0 1)))] [else (cons (car lst) (replace (cdr lst) indices with (+ 0 1)))]))


anurag.rawat0201
2021-3-13 00:00:12

is this okay?


anurag.rawat0201
2021-3-13 00:01:39

> (replace '(1 2 3 4) '(0 1) 6 0) (6 2 3 4)


anurag.rawat0201
2021-3-13 00:01:57

this is what I get


anurag.rawat0201
2021-3-13 00:10:24

I think I’m getting it quite a bit.. so if #lang doesn’t allow that so how should I write it?


anurag.rawat0201
2021-3-13 00:10:39

the index with 0 as default value