Search a list: Difference between revisions

Content added Content deleted
(Adding Haskell)
m (→‎[[#ALGOL 68]]: add FOR loop version with no exception)
Line 1: Line 1:
{{task|Text processing}}
{{task|Text processing}}
Find the index of a string (needle) in an array of strings (haystack), or else raise an exception if the needle is missing.
Find the index of a string (needle) in an array of strings (haystack), or else raise an exception if the needle is missing.
If there is more then one occurrence then return smallest i such that haystack[i] = needle.
If there is more then one occurrence then return smallest i such that haystack[i] = needle.
Line 34: Line 34:
end "+";
end "+";
Haystack : List := "Zig"+"Zag"+"Ronald"+"Bush"+"Krusty"+"Wally"+"Charlie"+"Bush"+"Bozo";
Haystack : List := "Zig"+"Zag"+"Wally"+"Ronald"+"Bush"+"Krusty"+"Charlie"+"Bush"+"Bozo";


procedure Check (Needle : String) is
procedure Check (Needle : String) is
Line 51: Line 51:
<pre>
<pre>
Washington is not in
Washington is not in
Bushat 4
Bushat 5
</pre>
</pre>
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
===Using a FORMAT "value error" exception===
FORMAT hay stack := $c("Zig","Zag","Ronald","Bush","Krusty","Wally","Charlie","Bush","Bozo")$;
FORMAT hay stack := $c("Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo")$;
[]STRING needles = ("Washington","Bush");
[]STRING needles = ("Washington","Bush");
Line 80: Line 81:
<pre>
<pre>
Washington is not in haystack
Washington is not in haystack
4 Bush
5 Bush
</pre>
===Using a manual FOR loop with no exception===
[]STRING hay stack = ("Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo");
PROC index = ([]STRING hay stack, STRING needle)INT:(
INT index;
FOR i FROM LWB hay stack TO UPB hay stack DO
index := i;
IF hay stack[index] = needle THEN
found
FI
OD;
else:
LWB hay stack - 1
EXIT
found:
index
);
[]STRING needles = ("Washington","Bush");
FOR i TO UPB needles DO
STRING needle := needles[i];
INT result = index(hay stack, needle);
IF result >= LWB hay stack THEN
printf(($d" "gl$, result, needle))
ELSE
printf(($g" "gl$,needle, "is not in haystack"))
FI
OD
Output:<pre>
Washington is not in haystack
5 Bush
</pre>
</pre>


Line 87: Line 120:
import Data.List
import Data.List
haystack=["Zig","Zag","Ronald","Bush","Krusty","Wally","Charlie","Bush","Bozo"]
haystack=["Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo"]
needles = ["Washington","Bush"]
needles = ["Washington","Bush"]
I use 'lambda' notation for readability.
I use 'lambda' notation for readability.
:Find 'just' an index:
:Find 'just' an index:
*Main> map (\x -> (x,findIndex (==x) haystack)) needles
*Main> map (\x -> (x,findIndex (==x) haystack)) needles
[("Washington",Nothing),("Bush",Just 3)]
[("Washington",Nothing),("Bush",Just 4)]
Want to know if there are there more Bushes hiding in the haystack?
Want to know if there are there more Bushes hiding in the haystack?
*Main> map (\x -> (x,findIndices (==x) haystack)) needles
*Main> map (\x -> (x,findIndices (==x) haystack)) needles
[("Washington",[]),("Bush",[3,7])]
[("Washington",[]),("Bush",[4,7])]
To be complete. Here is the 'point free' version of the task:
To be complete. Here is the 'point free' version of the task:
import Control.Monad
import Control.Monad
Line 101: Line 134:


*Main> map (ap (,) (flip findIndex haystack . (==))) needles
*Main> map (ap (,) (flip findIndex haystack . (==))) needles
[("Washington",Nothing),("Bush",Just 3)]
[("Washington",Nothing),("Bush",Just 4)]


=={{header|J}}==
=={{header|J}}==
Line 107: Line 140:
J has a general and optimized lookup function, <tt>i.</tt>. For example:
J has a general and optimized lookup function, <tt>i.</tt>. For example:


H =: ;:'Zig Zag Ronald. Bush Krusty Wally Charlie Bush Bozo' NB. Haystack
H =: ;:'Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo' NB. Haystack
N =: ;:'Washington Bush' NB. Needles
N =: ;:'Washington Bush' NB. Needles
H i. N
H i. N
Line 119: Line 152:
H ;:^:_1@(](>@{:@]|."_1(,.>@{.))i.({;(~:_1+#))1|.'is not in haystack';":&.>@i.@#@[) N
H ;:^:_1@(](>@{:@]|."_1(,.>@{.))i.({;(~:_1+#))1|.'is not in haystack';":&.>@i.@#@[) N
Washington is not in haystack
Washington is not in haystack
3 Bush
4 Bush


To elaborate a bit: Array-oriented languages (like J) consume the input and produce the output ''in toto''.
To elaborate a bit: Array-oriented languages (like J) consume the input and produce the output ''in toto''.
Line 138: Line 171:
import java.util.Arrays;
import java.util.Arrays;


List<String> haystack = Arrays.asList("Zig","Zag","Ronald","Bush","Krusty","Wally","Charlie","Bush","Bozo");
List<String> haystack = Arrays.asList("Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo");


for (String needle : new String[]{"Washington","Bush"}) {
for (String needle : new String[]{"Washington","Bush"}) {
Line 149: Line 182:


for arrays, you have to do it manually:
for arrays, you have to do it manually:
<java>String[] haystack = {"Zig","Zag","Ronald","Bush","Krusty","Wally","Charlie","Bush","Bozo"};
<java>String[] haystack = {"Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo"};


OUTERLOOP:
OUTERLOOP:
Line 169: Line 202:
=={{header|Python}}==
=={{header|Python}}==
<python>
<python>
haystack=["Zig","Zag","Ronald","Bush","Krusty","Wally","Charlie","Bush","Bozo"]
haystack=["Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo"]


for needle in ("Washington","Bush"):
for needle in ("Washington","Bush"):