Netlogo: delete list content - list

How do I delete the content of a list in Netlogo?
This is a tuned-down version of my code to function as an example:
to calculate_SN
ask turtles [
set subjective_norm_list []
set subjective_norm_list [1 2 3 4 5]
set subjective_norm ( sum subjective_norm_list / length subjective_norm_list)
*delete content of subjective_norm_list so that it is empty again*
end
The part between asterisks I don't know.

Based on your shared code so far, you should take a different approach: create a function.
to-report subjective-norm [#lst]
report (sum #lst) / (length #lst)
end
It is unclear that you will ever need to assign a variable name to your list. You may be able to use it upon creation and then forget about it. (It will be garbage collected.)

If you want subjective_norm_list to be an empty list, you can set it to an empty list, just like you did when you initialized it the first time around:
set subjective_norm_list []
Note that, technically, NetLogo lists are immutable, so you're not deleting the elements in the list: you're just creating a new list with no elements in it and assigning it to the same variable. But for all intents and purposes, it's the same: subjective_norm_list is empty again.

Related

finding member in nested lists netlogo

i'm trying to solve a problem in netlogo that has me stuck for a while now. i've got two lists (of turtles i've collaborated with and of "successful"/"unsuccessful" judgments). the two lists are mapped like so [[(turtle 10) "successful"] [(turtle 11) "unsuccessful"] with the following:
let general-history (map list collaborators my-success)
where the collaborators are the who numbers and my-success is a string (either "successful" or "unsuccessful")
now, i would like to check whether a turtle has, in its general-history list, at least one successful and one unsuccessful collaborator, to be able to proceed. this is where i've gotten to:
ifelse not empty? general-history and member? "successful " last general-history and member? "unsuccessful" last general-history
i know this is wrong because last here implies that i'll be looking only at the last list of general-history (i.e., [] [] [this one]). what i want it to do is assess whether there are at least two lists (one with "successful" as index 1 and one with "unsuccessful" as index 1) in the whole general-history nested list.
would foreach work better here or is it possible to still use member? but with some kind of element + list indexing? thank you very much for the help!
If I have understood your question correctly, you can use map to create a list of all the last items (success or not) and then apply member? to that list. Here is a complete model example that constructs some test data and the applies this approach.
to testme
clear-all
; create some test data
create-turtles 10
let collaborators sort n-of 3 turtles
let list-both (map list collaborators (list "yes" "no" "no"))
print list-both
; check condition
print member? "yes" map last list-both
end

NETLOGO: adding lists of agents' attributes to a list of lists

Hello I am using Netlogo and I am trying to create a list of lists in which every sublist is a couple of agents' attributes. In particular I declare the list as a global variable and I initialize it to an empty list. Then I ask every agent to add a list of their attribute_1 and attribute_2 to the main list. Like this:
globals[mainlist]
set mainlist []
ask agents[
set mainlist sentence [mainlist] [attribute_1 attribute_2 ]
]
This should create a new list composed by the previous mainlist and the list [attribute_1 attribute_2].
Unfortunately this doesn't work and I get the error: EXPECTED LITERAL VALUE referring to "mainlist".
How should I write my code to create my list of lists in the correct way?
Tyr's answer is technically correct, but I would like to suggest a more netlogoish way to do it. Usually, in NetLogo, you don't have to build lists one element at a time. If you find yourself doing that, you might want to stop and try to approach the problem differently.
In this particular case, you can simply take advantage of the of primitive:
[ (list attribute_1 attribute_2) ] of agents
Here is a fully working example:
breed [agents an-agent]
agents-own [attribute_1 attribute_2]
globals [mainlist]
to setup
clear-all
create-agents 10 [
set attribute_1 random 10
set attribute_2 random 10
]
set mainlist [ (list attribute_1 attribute_2) ] of agents
print mainlist
end
You can use lput or fput to add further elements to a list (lput will add the new list item at the end (l=last), whereas fput will add the new item at front (f=first)).
Additionally, you have to use the (list ... ...) primitive instead of [ ] brackets when you want to store variables in a list (brackets only work with constants).
Here is a working example:
globals[mainlist]
to test
ask n-of 20 patches [sprout 1]
set mainlist []
ask turtles
[
set mainlist lput (list xcor ycor) mainlist
]
print mainlist
end

How to check and change the value of a list?

I would like to check all list values in a list and change them if necessary.
p.e.
I want to check the next lists if there are values higher or lower then the next values:
min-value = 6
max-value = 22
mylist = ['4-8','25','16-19','21-32']
if one of the list values is below the min-value or higher then the max-value, the list values must be changed to the min-value and max-value. p.e. in example, the new list must be:
mylist = ['6-8','22','16-19','21-22']
if the entire value of the list item is below the min-value or higher then the max-value the list item can be removed.
How can I check my list values and change them?
There are two approaches. In the procedural one, you iterate over the list items and modify or skip the element:
let newlist = []
for element in mylist
" Parse element.
if ! OutsideBounds(element)
call add(newlist, AdjustBounds(element))
endif
endfor
In the functional programming approach, you use the built-in map() to modify elements (i.e. adjust the bounds), but that one cannot remove elements. So just empty those elements and then do a second pass with filter() to remove them. Note that both functions modify the original lists, so use copy() if you need to keep the original.
call filter(map(mylist, 'AdjustBounds(v:val)'), '! OutsideBounds(v:val)')
I hope I don't need to tell you how to write the AdjustBounds() and OutsideBounds() functions...

Haskell - get n number of lists from a list of lists

Hey guys so I'm trying and get the n number of lists from a list of lists. I was wondering if there is a method in haskell that works similar to the "take" and "drop" method but instead if would work in my situation. For example:
Input = [ [1,2,3,4], [5,6,7,8], [9,1,2,3], [4,5,6,7], [8,9,1,2], [3,4,5,6] ]
I want to be able to take the first 3 elements from this list of lists and end up with something like this:
Output = [ [1,2,3,4], [5,6,7,8], [9,1,2,3]]
I also want to be able to drop the first 3 elements from this list of lists and end up with something like this:
Output = [[4,5,6,7], [8,9,1,2], [3,4,5,6]]
Is it possible to do something like this in haskell.? Can anyone point me into the right direction on how to tackle this problem. Thanks in advance.
take and drop do exactly that. They work the same for all element types, even if the element type is a list type.
Prelude> take 3 [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]]
[[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Prelude> drop 3 [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]]
[[13,14,15,16],[17,18,19,20]]

Prolog Insert list as element into another List

In my program my P = [0,1,2] I want to store it into another LIST, because P will keep changing in a loop so I want to store P into a LIST, so my LIST will be like below :
eg.
LIST = [[0,1,2],[3,4,5],[6,7,8]]
create_list([],[]).
create_list(G, [H|G]).
This is what I did, create_list(P,LIST). I not sure how to do it as it keep return me no. But I am pretty sure I can get different P because I am able to print them out each time P changed.
You need to create a predicate that receives the item (list in this case) you want to append to another input list, and this would give you a new list with the which has all the items of your input list plus the new item.
So, it would be something like:
create_list(Item, List, [Item|List]).
Initially the input List would be an empty list ([]), so you might call it
create_list([0,1,2], [], List1),
create_list([3,4,5], List1, List2),
create_list([6,7,8], List2, List).
This will result in List instantiated with [[0,1,2],[3,4,5],[6,7,8]]