Question about map

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about map

peiskerj
Hi there,

I want to write a reporter that takes a list and returns the sum of squared
deviations from the mean. I would like to use map for this purpose. However,
it throws the error "Expected literal value" if done like below. Does
anybody have an idea?

Best,
Jonas

--
globals [ bob ]

to setup
  clear-all
  set bob [ 1 2 3 ]
  reset-ticks
end

to print-ssdm
  print ssdm [ bob ]
end

to-report ssdm [ mylist ]
  let sdm map [ x -> (x - mean mylist) ^ 2 ][ mylist ]
  report sum sdm
end



--
Sent from: http://netlogo-users.18673.x6.nabble.com/
Reply | Threaded
Open this post in threaded view
|

Re: Question about map

guerrin
Hi Jonas,

it does'nt work because you made syntax errors by putting erroneously
brackets around [bob] in the print-ssdm procedure and [mylist] in the
ssdm reporter.

Below is the correct program :

globals [ bob ]

to setup
     clear-all
     set bob [ 1 5 2 3 ]
     reset-ticks
     end

to print-ssdm
   print ssdm bob
end

to-report ssdm [ mylist ]
   let sdm map [ x -> (x - mean mylist) ^ 2 ] mylist
     report sum sdm
end

Instead of using map you can use foreach as follows:

to-report ssdm [ mylist ]
       let sdm [] ; here you declare a local variable being an empty list
     foreach mylist
         [x -> set sdm lput ((x - mean mylist) ^ 2) sdm] ; here you
build the sdm list of the square deviations for each mylist element
     report sum sdm ; of which you report the sum of elements
end


Le 26/03/2018 à 22:03, peiskerj [hidden email] [netlogo-users] a écrit :

>
> Hi there,
>
> I want to write a reporter that takes a list and returns the sum of
> squared
> deviations from the mean. I would like to use map for this purpose.
> However,
> it throws the error "Expected literal value" if done like below. Does
> anybody have an idea?
>
> Best,
> Jonas
>
> --
> globals [ bob ]
>
> to setup
> clear-all
> set bob [ 1 2 3 ]
> reset-ticks
> end
>
> to print-ssdm
> print ssdm [ bob ]
> end
>
> to-report ssdm [ mylist ]
> let sdm map [ x -> (x - mean mylist) ^ 2 ][ mylist ]
> report sum sdm
> end
>
> --
> Sent from: http://netlogo-users.18673.x6.nabble.com/
>
>

--

SignatureMail-FG.png (33K) Download Attachment