[Haskell-cafe] Problems interpreting
Hi, I'm a student and I have to do a program with Haskell. This is the first time I use this languaje, and I'm having problems with the indentation. I want to check if this function is correct, but when I try to make the GHCi interpret it, I get "line 18:parse error (possibly incorrect indentation)" The function is: -- Replaces a wildcard in a list with the list given as the third argument substitute :: Eq a => a -> [a] -> [a] -> [a] substitute e l1 l2= [check_elem c | c <- l1] where check_elem::Eq a => a -> [a] check_elem x = if x == e then return l2 -- Tries to match two lists. If they match, the result consists of the sublist -- bound to the wildcard in the pattern list. (line 18) match :: Eq a => a -> [a] -> [a] -> Maybe [a] match _ _ _ = Nothing {- TO BE WRITTEN -} Thank you for your attention! -- View this message in context: http://www.nabble.com/Problems-interpreting-tf2290155.html#a6360687 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Problems interpreting
Wow! I'm starting to love this languaje, and the people who uses it!:) Andrea Rossato wrote: > > On Mon, Sep 18, 2006 at 12:54:34PM +0200, Albert Crespi wrote: >> Thank you very much for your reply! >> As I said, it is my first experience with Haskell, I have been >> programming >> in Java and C for some years, and I find this language very different >> from >> them. Anyway I'll try to fix the function with the information that you >> gave >> me. >> Thanks again! >> > You're welcome. > > By the way, this is what the comments say you are trying to do: > > -- Replaces a wildcard in a list with the list given as the third argument > substitute :: Eq a => a -> [a] -> [a] -> [a] > substitute e l1 l2= [c | c <- check_elem l1] > where check_elem [] = l1 > check_elem (x:xs) = if x == e then (l2 ++ xs) else check_elem xs > > This is the result: > > *Main> substitute 1 [1,2,3] [] > [2,3] > *Main> substitute 1 [1,2,3] [7,8,9] > [7,8,9,2,3] > *Main> > > Have fun with Haskell. > > Ciao > Andrea > ___ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > > -- View this message in context: http://www.nabble.com/Problems-interpreting-tf2290155.html#a6361815 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Problems interpreting
Finally I took Andrea's solution "check_elem (x:xs) = if x == e then (l2 ++ xs) else [x] ++ check_elem xs" I think it's easy to understand for me ( in my noob level), than the recursive one. I'm testing it and it's working really well. The other solutions are a little complicated for me, but I'm still trying to undestand them. Thanks! Andrea Rossato wrote: > > On Mon, Sep 18, 2006 at 12:25:21PM +0100, Neil Mitchell wrote: >> Why not: >> > check_elem (x:xs) = if x == e then (l2 ++ xs) else x : check_elem xs >> >> Thanks > > Thank you! > Lists are my personal nightmare...;-) > > Andrea > ___ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > > -- View this message in context: http://www.nabble.com/Problems-interpreting-tf2290155.html#a6362822 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Problems interpreting
Not a good solution, it just substitutes the first occurrence of the item in the list. I'll try the others Carajillu wrote: > > Finally I took Andrea's solution "check_elem (x:xs) = if x == e then (l2 > ++ xs) else [x] ++ check_elem xs" > I think it's easy to understand for me ( in my noob level), than the > recursive one. > I'm testing it and it's working really well. The other solutions are a > little complicated for me, but I'm still trying to undestand them. > Thanks! > > > Andrea Rossato wrote: >> >> On Mon, Sep 18, 2006 at 12:25:21PM +0100, Neil Mitchell wrote: >>> Why not: >>> > check_elem (x:xs) = if x == e then (l2 ++ xs) else x : check_elem xs >>> >>> Thanks >> >> Thank you! >> Lists are my personal nightmare...;-) >> >> Andrea >> ___ >> Haskell-Cafe mailing list >> Haskell-Cafe@haskell.org >> http://www.haskell.org/mailman/listinfo/haskell-cafe >> >> > > -- View this message in context: http://www.nabble.com/Problems-interpreting-tf2290155.html#a6362912 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Problems interpreting
Definitely I'll take this solution, I'm reading about Pointfree, I think it's not that dificult to understand. And moreover it's the simpliest way to write code. Jón Fairbairn-2 wrote: > > Andrea Rossato <[EMAIL PROTECTED]> writes: > >> On Mon, Sep 18, 2006 at 04:16:55AM -0700, Carajillu wrote: >> > >> > Wow! I'm starting to love this languaje, and the people who uses it!:) >> > >> >> You spoke too early. My code had a bug, a huge one... >> >> this is the right one: >> >> -- Replaces a wildcard in a list with the list given as the third >> argument >> substitute :: Eq a => a -> [a] -> [a] -> [a] >> substitute e l1 l2= [c | c <- check_elem l1] >> where check_elem [] = l1 >> check_elem (x:xs) = if x == e then (l2 ++ xs) else [x] ++ >> check_elem xs > > > I think it's nicer to do it like this: > >substitute e l l' >= concat (map subst_elem l) > where subst_elem x >| x == e = l' >| otherwise = [x] > > since "subst_elem" has a more straightforward meaning than > "check_elem", and the concatenation is handled by a well > known standard function. > > Also, it would usually be more useful to have the argument > to replace /with/ before the argument to replace /in/, so > that ("substitute '*' "wurble") is a function that replaces > all the '*'s in it's argument with "wurble"s. > > And if you do that, you can write it like this: > >subst e l' >= concat . map subst_elem > where subst_elem x >| x == e = l' >| otherwise = [x] > > -- > Jón Fairbairn [EMAIL PROTECTED] > > ___ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > > -- View this message in context: http://www.nabble.com/Problems-interpreting-tf2290155.html#a6363827 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Java or C to Haskell
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 ''; } 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) I'm trying to use map for scanning the list l2, then using some function for substitute the char, and finally something like zipWith x == y laux l1 for comparing the lists, but I can't find a way for putting all this together!! -- View this message in context: http://www.nabble.com/Java-or-C-to-Haskell-tf2303820.html#a6403589 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
wow, the simpliest ever! Andrea Rossato wrote: > > On Wed, Sep 20, 2006 at 01:31:22AM -0700, Carajillu wrote: >> 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. > > I forgot, obviously, that lists are an instance of the Eq class... > so, this is enough: > comp l1 l2 = if l1 == l2 then True else False > > You never stop learning! > andrea > ___ > Haskell-Cafe mailing list > Haskell-Cafe@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#a6404305 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
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@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. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
Yes, they must be equal the whole way, I like this recursive solution :) Ketil Malde-3 wrote: > > Carajillu <[EMAIL PROTECTED]> writes: > >> compare function just compares the two lists and return true if they are >> equal, or false if they are not. > >> 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) > > Must they be equal the whole way, or just up to the occurrence of the > searched-for character? > > find_match (x:xs) (y:ys) c | x==c = Just y > | x/=y = Nothing > | True = find_match xs ys c > find_match [] [] _ = Nothing > > Or, to check the whole list: > > find_match (x:xs) (y:ys) c | x==c && xs == ys = Just y > | x/=y = Nothing > | True = find_match xs ys c > find_match [] [] _ = Nothing > > -k > -- > If I haven't seen further, it is by standing in the footprints of giants > > ___ > Haskell-Cafe mailing list > Haskell-Cafe@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#a6404344 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe