raoul.schorer
2021-3-18 09:16:26

after adding the missing require, I get ../../../../../usr/share/racket/collects/racket/private/define.rkt:24:13: begin-for-syntax: not in a definition context in: (begin-for-syntax (define-values (for_/fXvector) …


raoul.schorer
2021-3-18 09:17:15

This is on 7.9 BC. The errors are identical between 7.9 BC and 8.0.0.12


anything
2021-3-18 12:29:41

The culture here (and many other programming places) is for you to post your question or comment first. Many will be willing to help, but you must [I mean, you should] state the request as clearly as you can first.


badkins
2021-3-18 12:57:25

In your example, it seems you considered '(#\a #b) to match '(#\a #\b #\c), so I would suggest using the list-prefix? function. (list-prefix? '(#\a #\b) '(#\a #\b #\c)) returns #t


ahnaaf20
2021-3-18 12:59:36

As I am a beginner so I am new to new to this coding. Can you please me the full code for my better understanding. Also this a b c are not fixed they can be any letters


badkins
2021-3-18 13:01:02

Taking your last example, just replace memv with list-prefix? as follows: (define (extract-2 lst arg) (filter (curry list-prefix? arg) lst))


badkins
2021-3-18 13:01:42

(extract-2 '((#\a #\b #\c) (#\a #\c #\d) (#\d #\e #\f)) '(#\a #\b)) returns '((#\a #\b #\c))


badkins
2021-3-18 13:03:14

While curry is handy, there can be a performance loss (probably not an issue for a hangman game), so alternatively: (define (extract-2 lst arg) (filter (λ (lst) (list-prefix? arg lst)) lst))


ahnaaf20
2021-3-18 13:03:15

What if instead of ‘(#\a #\b) I give only 1 char or 3 char


ahnaaf20
2021-3-18 13:03:41

Will it still work ?


badkins
2021-3-18 13:03:44

It will work with any number of chars.


ahnaaf20
2021-3-18 13:04:03

Ohh thank


ahnaaf20
2021-3-18 13:04:13

I will try these now and let you know


badkins
2021-3-18 13:04:17

Naturally, you still need to pass arg as a list i.e. '(#\a) and not just #\a


badkins
2021-3-18 13:04:56

list-prefix? simply indicates whether the arg matches as many chars as arg has.


ahnaaf20
2021-3-18 13:05:55

Okay will try now


ahnaaf20
2021-3-18 13:06:06

Thanks a lot for your help. Really appreciated


badkins
2021-3-18 13:06:18

You’re welcome - have fun with Racket :)


ahnaaf20
2021-3-18 13:13:56

I get the following error @badkins


badkins
2021-3-18 13:15:12

Ok, it looks like your teacher wants you to use a subset of Racket that doesn’t include list-prefix? , so you may need to write that function yourself.


ahnaaf20
2021-3-18 13:15:55

So how do I do it ?


badkins
2021-3-18 13:16:42

It’s pretty simple, you just want to grab an element from both lst and arg one at a time and see if they match. If they all match, return #t if you encounter one that doesn’t match, return #f


ahnaaf20
2021-3-18 13:18:30

But as a output I want the get that list on t or f


badkins
2021-3-18 13:22:16

I was referring only to the list-prefix? function that you need to write. The original extract-2 function will work fine once you have defined list-prefix? , for example: (define (list-prefix? l r) ;; you write code here )



ahnaaf20
2021-3-18 13:27:31

But can a function have two defined names ?


ahnaaf20
2021-3-18 13:27:52

I mean I define list-prefix can I also define extract ?


badkins
2021-3-18 13:28:14

One of the wonderful things about Racket is you can define as many functions as you want :)


badkins
2021-3-18 13:28:55

(define (extract-2 ...) ... ) (define (list-prefix? ...) ... ) ...


ahnaaf20
2021-3-18 13:29:42

Okay so I should put the extract function inside the list prefix function ?


badkins
2021-3-18 13:30:13

I would define then separately since list-prefix? is a generally useful function.


badkins
2021-3-18 13:30:19

as in my example just above


badkins
2021-3-18 13:31:22

If you were to define one inside the other, you would define list-prefix? inside extract-2 since the latter uses the former.


badkins
2021-3-18 13:32:18

You’ll be iterating over two lists, so make sure you handle the termination condition for both lists.


ahnaaf20
2021-3-18 13:35:00

Why do this occur?


badkins
2021-3-18 13:37:28

You should use list-prefix? only to compare two lists, for example you want to know if '(1 2) is a prefix of '(1 2 3)


badkins
2021-3-18 13:38:39

I think you may be rushing this too much. I’d suggest taking a step back and studying the fundamental concepts of working with lists, or lists of lists, etc.


ahnaaf20
2021-3-18 13:40:20

Sorry I am troubling you but for only this function I am stuck with the game. If you please show the complete code it will be really very helpful. I never would have asked if this was not urgent. Hope you understand


badkins
2021-3-18 13:40:38

When is the homework due?


ahnaaf20
2021-3-18 13:40:53

Today


badkins
2021-3-18 13:41:10

Is there a particular book you’re using for the class?


ahnaaf20
2021-3-18 13:41:35

No just notes


ahnaaf20
2021-3-18 13:41:46

Things are not that clear over there


ahnaaf20
2021-3-18 13:42:57

Is there no way to use list-prefix to check with multiple list ?


badkins
2021-3-18 13:43:54

You don’t need to use list-prefix to check a list of lists. extract–2 works fine if it had a list-prefix? to use i.e. you’re not replacing extract-2, you just need to code the list-prefix? function since your language doesn’t have access to it.


badkins
2021-3-18 13:44:28

list-prefix? accepts two arguments and indicates whether the first argument l is a prefix of the second argument r. That’s all.


badkins
2021-3-18 13:45:28

So you’ll have: (define (list-prefix? l r) ;; your code goes here ) (define (extract-2 lst arg) ;; existing code you already have )


ahnaaf20
2021-3-18 13:50:32

Should I not include filter ?


badkins
2021-3-18 13:50:37

Here is an example using a “named let” that may help you get started: (define (count< lst n) (let loop ([ lst lst ][ sum 0 ]) (cond [ (null? lst) sum ] [ else (if (< (car lst) n) (loop (cdr lst) (add1 sum)) (loop (cdr lst) sum)) ]))) (count< '(1 2 3 4 5) 4) ;; returns 3 (count< '(1 2 3 4 5) 9) ;; returns 5 (count< '() 4) ;; returns 0 Your loop will have to iterate over two lists though, but otherwise, it’s similar.


ahnaaf20
2021-3-18 13:52:11

I am such a bad learner


ahnaaf20
2021-3-18 13:52:28

Sorry I am not able to figure it out.


badkins
2021-3-18 13:52:51

At this point, I’d suggest approaching your teacher for help - they’ll be able to guide you much better.


badkins
2021-3-18 13:53:41

You’re not a bad learner, you just may need to put in a little more time. You’ll get it.


ahnaaf20
2021-3-18 13:54:09

I tried but they are not helping. Am I asking too much if you just show me the complete code. It would really make me understand with some comments in it. I won’t bother you further


ahnaaf20
2021-3-18 13:54:22

I am trying this for 2 days but couldn’t solve


badkins
2021-3-18 13:55:09

Do you understand my last example? I’m happy to answer questions about it.


badkins
2021-3-18 13:55:35

The function returns the count of the number of numbers in the list that are less than n.


ahnaaf20
2021-3-18 13:56:12

Yeah I understand bits of that


greg
2021-3-18 13:56:14

The “not in a definition context” sounds like you’re using define-vector-wraps someplace where even a plain old define would not be allowed.


badkins
2021-3-18 13:56:15

list-prefix? will also iterate over a list like this, but it will iterate over two at once, so you’ll have two checks for an empty list.


badkins
2021-3-18 13:56:42

As you iterate over both lists, here are some things that might happen:


ahnaaf20
2021-3-18 13:56:48

Like there a 3 list and can be 1 to 3 characters to check


badkins
2021-3-18 13:58:01
  1. the first argument is empty (what do you return ?)
  2. the second argument is empty (what do you return?)
  3. the elements of the two lists do not match (what do you return?) If you haven’t reached the end of either list, and the two elements match, you loop.

ahnaaf20
2021-3-18 13:59:56

But how do I do that as the list have ( list of chars


badkins
2021-3-18 14:00:42

list-prefix? accepts two lists of elements - neither is a list of lists. It’s extract-2 that accepts a list of lists of chars.


badkins
2021-3-18 14:01:03

I wish you the best with the assignment. I need to get back to work, so I won’t be able to help further.


ahnaaf20
2021-3-18 14:01:21

I am able to understand them separately but I can’t join them together


ahnaaf20
2021-3-18 14:01:29

Thank You for your help and effor


ahnaaf20
2021-3-18 14:01:33

Effort


badkins
2021-3-18 14:01:55

You don’t need to join them together - Racket does that for you automatically. In other words, once you successfully define list-prefix?, your program will work.


ahnaaf20
2021-3-18 14:01:56

If you get time can you please help me to join them


greg
2021-3-18 14:05:09

[BTW in Racket libraries the convention is that stuff under private/ is provided for the library implementation to use — but not really for outside users of the library. It might not be supported, or documented. It’s also OK for the library to change it incompatibly someday — even delete it! So you wouldn’t want to do this in “real code”, for production or release.]


rokitna
2021-3-18 17:35:14

I think I’ve seen an error like that when I use begin-for-syntax in a local definition context. (If I remember right, it only works at the top level or the module level. Other things that expand to it only work there too.) The error message is pretty confusing, since it’s not clear whether the problem is that it’s in a kind of definition context it shouldn’t be in, that it’s not currently in the kind of definition context that it should be in, or both.


greg
2021-3-18 18:10:34

Ah interesting. So, I have no real experience with units, but it seems that #lang racket/unit expressions end up effectively “inside” a define-unit form? So if you tried to put define-vector-wraps inside a define-unit form, would you get a similar error?


greg
2021-3-18 18:11:18

If so, you may just need to stick with #lang racket or #lang racket/base and use an explicit define-unit form, putting the define-vector-wraps thing outside that form — truly at the file module level.


raoul.schorer
2021-3-18 19:06:20

That’s the thing I’m gonna try next. I suspect I indeed need to be explicit with units and stick with #lang racket/base


gknauth
2021-3-18 19:17:22

I saw the discussion in #beginners. Let me try something else. I’ll assume that Ahnaf is not only a human but also a function. How does Ahnaf the human approach: (ahnaf '((apple banana cherry) (apple cherry date) (date elderberry fig)) '(apple banana)) That is, you see a long table: apple banana cherry apple cherry date date elderberry fig ... 1000 more like this ... Your mother tells you she needs: apple banana and you quickly say, “I found it!” apple banana cherry How did you, Ahnaf the human, accomplish that?


ahnaaf20
2021-3-18 19:30:20

With filter


gknauth
2021-3-18 19:39:19

filter would return ((apple banana cherry)), but the desired output is (apple banana cherry). What does your code that uses filter look like?


ahnaaf20
2021-3-18 19:39:59

ahnaaf20
2021-3-18 19:40:33

It only works if the the character order matches with list. But I want to match it any order


gknauth
2021-3-18 19:45:02

So, if I understand you correctly, in your lambda function that you pass to filter, instead of asking “is list-A a prefix of list-B?” you want to ask, “Is every member of list-A in list-B?”


ahnaaf20
2021-3-18 19:50:23

Yes


ahnaaf20
2021-3-18 19:52:54

But It should be “Is every member of list-B present in list A”. Out of 3 sub list in list A it will give the sublist which match all the members of list B


juanesgonzalez746
2021-3-18 19:56:47

ah ok


gknauth
2021-3-18 20:18:27

That sounds right. So, all you have to do is figure out how to do that in your lambda function. The lambda function gets one list-A-sublist at a time. For each element of list-B, that element has to be checked to see if it is a member of list-A-sublist. That could be another lambda function (within the first one), or it could be a function defined elsewhere that does that checking. Each lambda function should do one thing very well, such as, given ’(A B C) and ‘A to be able to tell if ’A is in ’(A B C). Actually there already exists a function that will do that, but it’s easy enough to write if that’s part of what you have to do. Once you have something that can tell if ‘A is in ’(A B C), you can use that to write something that can tell if everything in ’(A B) is in ’(A B C)


ahnaaf20
2021-3-18 20:19:39

I tried this but couldn’t figure out


ahnaaf20
2021-3-18 20:19:53

Would me show me code ? So that I better underu


ahnaaf20
2021-3-18 20:19:56

Understand


gknauth
2021-3-18 20:20:49

I don’t know if this is homework, so I’m hesitant to show code. I’d rather see what you tried that didn’t work, then I could see where it didn’t work.


ahnaaf20
2021-3-18 20:21:47

It’s not my homework. I am making a hangman game, where I will use this function to give a hint of the word.


ahnaaf20
2021-3-18 20:22:41

It would really helpful If you could shoe me the code


gknauth
2021-3-18 20:22:59

I saw in your screenshot you were using language PrettyBig. Was there a reason for that?


ahnaaf20
2021-3-18 20:24:10

Yeah because I learned this only in a course


gknauth
2021-3-18 20:26:13

Have you heard of the function member ?


ahnaaf20
2021-3-18 20:30:04

Yes


ahnaaf20
2021-3-18 20:30:08

memb


ahnaaf20
2021-3-18 20:30:17

memv ! ?


gknauth
2021-3-18 20:30:38

What happens when you type: (member 'a '(a b c))


ahnaaf20
2021-3-18 20:33:56

It gives #t


gknauth
2021-3-18 20:35:18

That’s not what I got, and I set my DrRacket to use language PrettyBig. I got (a b c)


ahnaaf20
2021-3-18 20:36:03

Ohh okay


raoul.schorer
2021-3-18 21:00:39

Hmm… unfortunately, I think that begin-for-syntax makes unit and define-vector-wraps incompatible because I can’t see how to make a module-level macro work inside a unit from that module. I’ll have to code around that. :unamused:


raoul.schorer
2021-3-18 21:22:09

Hi, am I correct in understanding that prop:sequence and make-do-sequence are the way to make new iterators such as in-list, in-vector etc?


soegaard2
2021-3-18 21:48:57

@raoul.schorer Check also define-sequence-syntax and :do-in


greg
2021-3-18 23:15:57

I think they’re the most fundamental/flexible way to make a sequence? But definitely not the only way, or necessarily the most convenient way, to make a sequence.


greg
2021-3-18 23:19:27

As one example, if you’re doing “stateful” things like I/O, and have a function like read, you can make it into a sequence using in-producer https://docs.racket-lang.org/reference/sequences.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._in-producer%29%29.


greg
2021-3-18 23:20:25

(And of course many things are already sequences https://docs.racket-lang.org/reference/sequences.html)


samdphillips
2021-3-18 23:36:55

Also sometimes it’s easier to make a stream and use the gen:stream or for/stream forms.