
@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!

@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
.

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

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

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)

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.

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.

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.

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.

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)

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.

(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))]))

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

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)))

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)))

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.

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

@anurag.rawat0201 has joined the channel

Hi all

Can anyone please help me with scheme?

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.

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

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.

sure I can share what I did

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.

(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))]))

that’s not a bad attempt

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

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

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?

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

we’re using 3 arguments in replace function

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).

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

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

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

Yep. All good. :slightly_smiling_face:

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

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.

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

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.

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

it just return the index value of the desired item

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

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)

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

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

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

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

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

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

+1

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.

Yes, I understand the problem. :slightly_smiling_face:

do I need to use let here?

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.

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

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

I basically not sure I do I actually write that

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

okay I see

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

?

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.

alright, I’ll give it a try

Thanks for the help

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

tried running as admin and get the same issue :s

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

(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))]))

(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))]
)
)
)

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

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

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:

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)

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)

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

> 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

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

I thought you wrote the pseudocode

I’m so sorry for being stupid

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

> 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:

But, I really appreciate your time and help

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

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

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

isn’t indices already a list containing “index”

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

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

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

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

something like this

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)

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

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

otherwise won’t it give the arity mismatch error ?

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

okay

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

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

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))))

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

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

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

okay

I get what you wrote there

the code* of index-of

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

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

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

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

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

> 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

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

no

just change the argument to index
and pass 0 yourself

(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)))]))

is this okay?

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

this is what I get

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

the index with 0 as default value