Not allowed to use cons on list; "does not match any of the declared (overloaded) signature patterns"

StackOverflow https://stackoverflow.com/questions/19006717

  •  29-06-2022
  •  | 
  •  

Question

According to this page:

http://tutor.rascal-mpl.org/Rascalopedia/List/List.html

This is how you use cons on lists:

cons(1,[2,3]); //should return [1,2,3]

Trying this in the Rascal console:

import List;
cons(1,[2,3]);

Gives me this error:

|stdin:///|(1,13,<1,1>,<1,14>): The called signature: cons(int, list[int]),
does not match any of the declared (overloaded) signature patterns:
    Symbol = cons(Symbol,str,list[Symbol])
    Production = cons(Symbol,list[Symbol],set[Attr])
    Symbol = cons(Symbol,str,list[Symbol])
    Production = cons(Symbol,list[Symbol],set[Attr])

Is there some name collision with the standard imported functions and datatypes?

Was it helpful?

Solution

Good question. First a straightforward answer. Cons does not exist as a library function for constructing lists (it is rather a part of the reified types API meaning something different).

We write this:

rascal>[1, *[2,3,4]]
list[int]: [1,2,3,4]

rascal>1 + [2,3,4]
list[int]: [1,2,3,4]

rascal>[1] + [2,3,4]
list[int]: [1,2,3,4]

rascal>list[int] myList(int i) = [0..i];
list[int] (int): list[int] myList(int);

rascal>[*myList(10), *myList(20)]
list[int]: [0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]

Now an explanation for the confusion. The Rascalopedia course is on abstract concepts and terms typically good to know when working with Rascal. This may be too basic for you. I would focus on the Rascal language and libraries course for more concrete examples and information relevant to programming in Rascal. See http://tutor.rascal-mpl.org/Rascal/Expressions/Values/List/List.html for the list literals and http://tutor.rascal-mpl.org/Rascal/Libraries/Prelude/List/List.html for library functions on lists.

Some other list handiness, used for deconstructing as opposed to constructing:

rascal>[1,2,3,4][1..]
list[int]: [2,3,4]

rascal>[1,2,3,4][1..2]
list[int]: [2]

rascal>[1,2,3,4][..-1]
list[int]: [1,2,3]

rascal>if ([*x, *y] := [1,2,3,4], size(x) == size(y)) println(<x,y>);
<[1,2],[3,4]>
ok

rascal>for ([*x, *y] := [1,2,3,4]) println(<x,y>);
<[],[1,2,3,4]>
<[1],[2,3,4]>
<[1,2],[3,4]>
<[1,2,3],[4]>
<[1,2,3,4],[]>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top