[Haskell-cafe] Java or C to Haskell
Donald Bruce Stewart
dons at cse.unsw.edu.au
Wed Sep 20 04:41:01 EDT 2006
crespi.albert:
>
> I'm trying to write in Haskell a function that in Java would be something
> like this:
>
> char find_match (char[] l1, char[] l2, char e){
> //l1 and l2 are not empty
> int i = 0;
> while (l2){
> char aux = l2[i];
> char[n] laux = l2;
> while(laux){
> int j = 0;
> if(laux[j] = aux) laux[j] = e;
> j++;
> }
> if compare (l1, laux) return aux;
> else i++;
> }
> return '';
> }
Yikes!
>
> compare function just compares the two lists and return true if they are
> equal, or false if they are not.
> it is really a simple function, but I've been thinking about it a lot of
> time and I can't get the goal. It works like this:
>
> find_match "4*h&a" "4*5&a" 'h' ----> returns '5' (5 matches with the h)
> find_match "4*n&s" "4dhnn" "k" ----> returns '' (no match at all - lists
> are different anyway)
That's almost a spec there :)
How about:
import Data.List
findMatch s t c
| Just n <- elemIndex c s = Just (t !! n)
| otherwise = Nothing
Using it in GHCi:
> findMatch "4*h&a" "4*5&a" 'h'
Just '5'
> findMatch "4*n&s" "4dhnn" 'k'
Nothing
-- Don
More information about the Haskell-Cafe
mailing list