Search a list: Difference between revisions
Add Ecstasy example
(Add Ecstasy example) |
|||
(6 intermediate revisions by 6 users not shown) | |||
Line 44:
(defun index-of (e xs)
(index-of-r e xs 0))</syntaxhighlight>
=={{header|Acornsoft Lisp}}==
Acornsoft Lisp does not have strings, so symbols would be used instead. That also allows us to use <code>eq</code> as the test.
<syntaxhighlight lang="lisp">
(defun first-index (word words (i . 0))
(loop
(until (null words)
(error word '! is! missing))
(until (eq word (car words))
i)
(setq words (cdr words))
(setq i (add1 i))))
(defun last-index (word words (i . 0) (last-i . nil))
(loop
(until (null words)
(cond (last-i) (t (error word '! is! missing))))
(cond ((eq word (car words))
(setq last-i i)))
(setq words (cdr words))
(setq i (add1 i))))
</syntaxhighlight>
=={{header|Action!}}==
Line 545 ⟶ 569:
Bush found first at index 5
Bush found last at index 8
</pre>
Line 1,480 ⟶ 1,466:
println(find("Ronald")) # prints 3
println(find("McDonald")) # will throw</syntaxhighlight>
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
haystack$[] = [ "Zig" "Zag" "Wally" "Ronald" "Bush" "Krusty" "Charlie" "Bush" "Boz" "Zag" ]
#
func getind needle$ .
for i to len haystack$[]
if haystack$[i] = needle$
return i
.
.
return 0
.
# arrays are 1 based
for n$ in [ "Bush" "Washington" ]
h = getind n$
if h = 0
print n$ & " not found"
else
print n$ & " found at " & h
.
.
</syntaxhighlight>
=={{header|Ecstasy}}==
The <code>List</code> type has an <code>indexOf()</code> method, and the <code>Array</code> type is a <code>List</code>:
<syntaxhighlight lang="ecstasy">
module test {
@Inject Console console;
void run() {
String[] haystack = ["this", "needle", "is", "a", "test"];
if (Int pos := haystack.indexOf("needle")) {
console.print($"Found the needle at {pos=}");
} else {
console.print("No needle in the haystack");
}
}
}
</syntaxhighlight>
{{out}}
<pre>
x$ xec test
Found the needle at pos=1
</pre>
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 1,490 ⟶ 1,523:
var haystack := new string[]{"Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Bozo"};
new string[]{"Washington", "Bush"}.forEach::(needle)
{
var index := haystack.indexOfElement
if (index == -1)
Line 1,740 ⟶ 1,773:
end program main</syntaxhighlight>
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
window 1, @"Search a list"
void local fn MyEnumeratorCallback( array as CFArrayRef, obj as CFTypeRef, index as NSUInteger, stp as ^BOOL, userData as ptr )
if ( fn StringIsEqual( obj, userData ) )
print obj;@" found at index ";index
*stp = YES// stop enumeration
end if
if ( index == 0 ) then print userData;@" not found"
end fn
void local fn DoIt
CFArrayRef haystack = @[@"Mike",@"Bravo",@"Tango",@"Uniform",@"Golf",
@"Tango",@"Sierra",@"November",@"Zulu",@"Delta",@"Hotel",@"Juliet"]
CFStringRef needle = @"Sierra"
NSInteger index = fn ArrayIndexOfObject( haystack, needle )
if ( index != NSNotFound )
print needle;@" found at index ";index
else
print needle;@" not found"
end if
ArrayEnumerateObjectsWithOptions( haystack, NSEnumerationReverse, @fn MyEnumeratorCallback, (ptr)@"Tango" )
end fn
fn DoIt
HandleEvents
</syntaxhighlight>
{{out}}
<pre>
Sierra found at index 6
Tango found at index 5
</pre>
=={{header|GAP}}==
Line 3,748 ⟶ 3,819:
=={{header|RPL}}==
{{works with|HP|48G}}
{ "Zig" "Zag" "Zig" "Wally" "Ronald" "Bush" "Krusty" "Charlie" "Bush" "Bozo" } '<span style="color:green">HAYSTACK</span>' STO
≪ → up
≪ <span style="color:green">HAYSTACK</span>
'''IF''' up NOT '''THEN''' REVLIST '''END'''
'''
"LOOKHAY Error:
Needle not found" DOERR
'''END'''
"Bush" 1 <span style="color:blue">
"Bush" 0 <span style="color:blue">LOOKHAY</span>
{{out}}
<pre>
2: 6
1: 2
</pre>
Line 4,412 ⟶ 4,471:
=={{header|Wren}}==
{{libheader|Wren-seq}}
<syntaxhighlight lang="
var find = Fn.new { |haystack, needle|
|