[Haskell-cafe] Java or C to Haskell

Carajillu crespi.albert at gmail.com
Wed Sep 20 05:21:05 EDT 2006


That works good, but I have a problem with the return type, I forgot to
mention... can it be a [char]??

Donald Bruce Stewart wrote:
> 
> 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
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 

-- 
View this message in context: http://www.nabble.com/Java-or-C-to-Haskell-tf2303820.html#a6404324
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list