How to iterate over a list in sophia?


I’m translating a dapp from solidity to sophia and I’m currently stuck at trying to translate this function:


The inside getStatus (the one being called in the for loop) is another function that works with a singular id as parameter, which I’ve already implemented.

I’ve gotten this far:


But since we don’t have for loops in sophia, how do I iterate through the ids?

@r0b0t0 :pray: last one I promise I wont bug you anymore :sweat_smile:

You need to do a recursion over the list and then apply whatever action you want to do on it.


@compiler >= 4

contract MyContract =
  entrypoint loop_list() : list(string) =
    let dummy_bytes_list : list(bytes(32)) = [ #fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210
                                             , #fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210
                                             , #fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210
                                             , #fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210]
    // interesting stuff here:
    let bytes_list_to_string_list = map(convert_to_string, dummy_bytes_list)
  function map(f : 'a => 'b, l : list('a)) : list('b) =
		[] => []
		e :: l' => f(e) :: map(f, l')

  function convert_to_string ( bytes': bytes(32) ) : string =

4 Likes is part of the sophia standard library now, no need to define it yourself.

Use it as:

include "List.aes"

..., [your_list])

Thanks @piwo! Totally forgot about that… :smiley: I haven’t used to having this library still… :slight_smile:

1 Like

@piwo It works in, thank you so much!

But where can I find the List.aes file, in order to put it in my aeproject files in vscode to compile it from there as well?

I know it sounds stupid but using

and it gives me:

I think we should make a little video explaining this step by step. I will try to do that later. =)

1 Like

Yes, that would be very helpful :smile:

aeproject is not yet compatible with the standardlibrary use, as you won’t need the “List.aes” file in your project, the neccessarry functions will be included by the compiler. (cc @MartinG)

Aeproject did implement logic here that did not behave to the api set by the node…

1 Like