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) …
This is on 7.9 BC. The errors are identical between 7.9 BC and 8.0.0.12
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.
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
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
Taking your last example, just replace memv
with list-prefix?
as follows: (define (extract-2 lst arg)
(filter (curry list-prefix? arg) lst))
(extract-2 '((#\a #\b #\c)
(#\a #\c #\d)
(#\d #\e #\f))
'(#\a #\b))
returns '((#\a #\b #\c))
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))
What if instead of ‘(#\a #\b) I give only 1 char or 3 char
Will it still work ?
It will work with any number of chars.
Ohh thank
I will try these now and let you know
Naturally, you still need to pass arg
as a list i.e. '(#\a)
and not just #\a
list-prefix?
simply indicates whether the arg
matches as many chars as arg
has.
Okay will try now
Thanks a lot for your help. Really appreciated
You’re welcome - have fun with Racket :)
I get the following error @badkins
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.
So how do I do it ?
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
But as a output I want the get that list on t or f
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
)
But can a function have two defined names ?
I mean I define list-prefix can I also define extract ?
One of the wonderful things about Racket is you can define as many functions as you want :)
(define (extract-2 ...) ... )
(define (list-prefix? ...) ... )
...
Okay so I should put the extract function inside the list prefix function ?
I would define then separately since list-prefix?
is a generally useful function.
as in my example just above
If you were to define one inside the other, you would define list-prefix?
inside extract-2
since the latter uses the former.
You’ll be iterating over two lists, so make sure you handle the termination condition for both lists.
Why do this occur?
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)
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.
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
When is the homework due?
Today
Is there a particular book you’re using for the class?
No just notes
Things are not that clear over there
Is there no way to use list-prefix to check with multiple list ?
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.
list-prefix?
accepts two arguments and indicates whether the first argument l
is a prefix of the second argument r
. That’s all.
So you’ll have: (define (list-prefix? l r)
;; your code goes here
)
(define (extract-2 lst arg)
;; existing code you already have
)
Should I not include filter ?
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.
I am such a bad learner
Sorry I am not able to figure it out.
At this point, I’d suggest approaching your teacher for help - they’ll be able to guide you much better.
You’re not a bad learner, you just may need to put in a little more time. You’ll get it.
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
I am trying this for 2 days but couldn’t solve
Do you understand my last example? I’m happy to answer questions about it.
The function returns the count of the number of numbers in the list that are less than n.
Yeah I understand bits of that
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.
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.
As you iterate over both lists, here are some things that might happen:
Like there a 3 list and can be 1 to 3 characters to check
- the first argument is empty (what do you return ?)
- the second argument is empty (what do you return?)
- 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.
But how do I do that as the list have ( list of chars
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.
I wish you the best with the assignment. I need to get back to work, so I won’t be able to help further.
I am able to understand them separately but I can’t join them together
Thank You for your help and effor
Effort
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.
If you get time can you please help me to join them
[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.]
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.
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?
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.
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
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?
With filter
filter
would return ((apple banana cherry))
, but the desired output is (apple banana cherry)
. What does your code that uses filter
look like?
It only works if the the character order matches with list. But I want to match it any order
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?”
Yes
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
ah ok
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)
I tried this but couldn’t figure out
Would me show me code ? So that I better underu
Understand
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.
It’s not my homework. I am making a hangman game, where I will use this function to give a hint of the word.
It would really helpful If you could shoe me the code
I saw in your screenshot you were using language PrettyBig. Was there a reason for that?
Yeah because I learned this only in a course
Have you heard of the function member
?
Yes
memb
memv ! ?
What happens when you type: (member 'a '(a b c))
It gives #t
That’s not what I got, and I set my DrRacket to use language PrettyBig. I got (a b c)
Ohh okay
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:
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?
@raoul.schorer Check also define-sequence-syntax
and :do-in
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.
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.
(And of course many things are already sequences https://docs.racket-lang.org/reference/sequences.html)
Also sometimes it’s easier to make a stream and use the gen:stream
or for/stream
forms.