Search a list: Difference between revisions

Added Wren
(Added Wren)
Line 3,964:
pos 24 '(1 2 3 4 5)
=> nil</pre>
 
=={{header|Wren}}==
{{libheader|Wren-seq}}
<lang ecmascript>import "/seq" for Lst
 
var find = Fn.new { |haystack, needle|
var res = Lst.indicesOf(haystack, needle)
if (!res[0]) Fiber.abort("Needle not found in haystack.")
System.print("The needle occurs %(res[1]) time(s) in the haystack.")
if (res[1] == 1) {
System.print("It occurs at index %(res[2][0])")
} else {
System.print("It first occurs at index %(res[2][0])")
System.print("It last occurs at index %(res[2][-1])")
}
System.print()
}
 
var haystack = ["Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Boz", "Zag"]
System.print("The haystack is:\n%(haystack)\n")
var needles = ["Wally", "Bush", "Zag", "George"]
for (needle in needles) {
System.print("The needle is %(needle).")
find.call(haystack, needle)
}</lang>
 
{{out}}
<pre>
The haystack is:
[Zig, Zag, Wally, Ronald, Bush, Krusty, Charlie, Bush, Boz, Zag]
 
The needle is Wally.
The needle occurs 1 time(s) in the haystack.
It occurs at index 2
 
The needle is Bush.
The needle occurs 2 time(s) in the haystack.
It first occurs at index 4
It last occurs at index 7
 
The needle is Zag.
The needle occurs 2 time(s) in the haystack.
It first occurs at index 1
It last occurs at index 9
 
The needle is George.
Needle not found in haystack.
[./search_list line 5] in new(_) block argument
[./search_list line 21] in (script)
</pre>
 
=={{header|XPL0}}==
9,488

edits