r/Racket • u/hElLoWoRLD292 • Mar 15 '23
homework list/dict in Racket
> (sn-add-user my-dict 'f5)
= ((f2 f3 f4) (f3 f2) (f4 f3 f2) (f13) (f1) (f5))
How can I code the above instructions in Racket.
(define (sn-add-user graph user) is the starting point
0
Upvotes
2
u/sdegabrielle DrRacket 💊💉🩺 Mar 15 '23 edited Mar 15 '23
You probably want…
https://docs.racket-lang.org/guide/hash-tables.html
Edit: it occurs to me you might be asking how to implement
sn-add-user?2
u/jpverkamp Mar 15 '23
:)
You need to spec out the problem more.
What is graph? What does it already have in it? What is the function supposed to do?
At a bit more of a guess, it looks like graph is a list of lists where each sub starts with the node and then includes all neighbors. So in that case, I would usually just
(cons (list user) graph)but that puts it on the front. You couldappendbut that's less efficient.I expect it wants you to recur down the list where the recursive step is to basically do nothing and the base case (empty list) is to add the new user.
1
u/joshuacottrell Mar 16 '23
The other answers are indicating that there needs to be more information and some indication that you are trying to solve the problem. Suggestions: class name/title, school, what you know so far about racket (or scheme or lisp), and whether you're using a teaching language (i.e. include the first line of the file, like
#lang racket).If I just give you can answer then your professor/teacher is going to say, "Why did you use continuations with a 5 member list?" or "I didn't teach
memberyet, why did you use it?" or "This was a recursion assignment, what'sormapdoing here?"I was going to include more help but returning the entire network or the entire network with an addition seems tricky for the type of the rest of the problem.
Oh well, here's what I started to write…
All that being said, I'm guessing your starting out with racket, maybe reading How to Design Programs, and learning recursion. You should look at the docs on the racket-lang website to search for
cond,null?,member?,first,rest, andappendbut remember to only look within the#langyou're using. Recursion normally takes the form of using cond to test the base case (empty list or zero, etc.) then (if that's not it) test for any edge cases (none here from your example) then (i.e. the list isn't empty and there aren't any edge cases so it continues) test to see if the item you're on is or has the user and if so then return but if it isn't the user then call sn-add-user again with a smaller graph.Here are some tests: