Parametric polymorphism: Difference between revisions

Added PicoLisp
m (→‎{{header|Go}}: grammar)
(Added PicoLisp)
Line 416:
say $it.perl;</lang>
Output:<p>IntTree.new(value => 42, left => IntTree.new(value => 42, left => Any, right => Any), right => IntTree.new(value => 42, left => Any, right => Any))
 
=={{header|PicoLisp}}==
PicoLisp is dynamically-typed, so in principle every function is polymetric over its arguments. It is up to the function to decide what to do with them. A function traversing a tree, modifying the nodes in-place (no matter what the type of the node is):
<lang PicoLisp>(de mapTree (Tree Fun)
(set Tree (Fun (car Tree)))
(and (cadr Tree) (mapTree @ Fun))
(and (cddr Tree) (mapTree @ Fun)) )</lang>
Test:
<pre style="height:20em;overflow:scroll">(balance 'MyTree (range 1 7)) # Create a tree of numbers
-> NIL
 
: (view MyTree T) # Display it
7
6
5
4
3
2
1
-> NIL
 
 
: (mapTree MyTree inc) # Increment all nodes
-> NIL
 
: (view MyTree T) # Display the tree
8
7
6
5
4
3
2
-> NIL
 
 
: (balance 'MyTree '("a" "b" "c" "d" "e" "f" "g")) # Create a tree of strings
-> NIL
 
: (view MyTree T) # Display it
"g"
"f"
"e"
"d"
"c"
"b"
"a"
-> NIL
 
: (mapTree MyTree uppc) # Convert all nodes to upper case
-> NIL
 
: (view MyTree T) # Display the tree
"G"
"F"
"E"
"D"
"C"
"B"
"A"
-> NIL</pre>
 
=={{header|Scala}}==
Anonymous user