[Haskell-cafe] Problems interpreting

2006-09-18 Thread Carajillu

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

2006-09-18 Thread Andrea Rossato
On Mon, Sep 18, 2006 at 02:51:34AM -0700, Carajillu wrote:
 
 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:
 
...
   if x == e then return l2

I did not understand what you are trying to do, anyway the error
message is due to the expression above, and not to indentation.
The Haskell if constructions is: if ... then ... else, and else
cannot be missing.

Moreover, return is not a way to return a value. It is a special
function that works with the monad class. Actually it is one of the
two methods of the monad class, and it inserts a value into a monad.

As far as your code goes, I'd suggest you to read some tutorials, like
The Gentle Introduction, or Hasekll for C Programmers.

Have a look here:
http://haskell.org/haskellwiki/Learning_Haskell
and here
http://haskell.org/haskellwiki/Books_and_tutorials  

Hope this helps.

Andrea
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Problems interpreting

2006-09-18 Thread Ketil Malde
Carajillu [EMAIL PROTECTED] writes:

 I get line 18:parse error (possibly incorrect indentation)

..which is a bit misleading, as the problem is on the preceeding line
of code.

   if x == e then return l2

And if x /= e?  What is check_elem then?¹  

 -- Tries to match two lists. If they match, the result consists of the
 sublist
 -- bound to the wildcard in the pattern list.
 (line 18) 

-k

¹ If you really don't want to provide that option, you could perhaps do:
check_elem x | x == e = return l2

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Problems interpreting

2006-09-18 Thread Andrea Rossato
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


Re: [Haskell-cafe] Problems interpreting

2006-09-18 Thread Carajillu

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

2006-09-18 Thread Andrea Rossato
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


Ciao,
Andrea
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Problems interpreting

2006-09-18 Thread Neil Mitchell

Hi


  check_elem (x:xs) = if x == e then (l2 ++ xs) else [x] ++ check_elem 
xs


Why not:

  check_elem (x:xs) = if x == e then (l2 ++ xs) else x : check_elem xs


Thanks

Neil
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Problems interpreting

2006-09-18 Thread Andrea Rossato
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


[Haskell-cafe] Re: Problems interpreting

2006-09-18 Thread Jón Fairbairn
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 wurbles.

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


[Haskell-cafe] Re: Why is type 'b' forced to be type 'm a' and not possibly 'm a - m a' (Anatoly Zaretsky)

2006-09-18 Thread Vivian McPhail
 Message: 6
 Date: Fri, 15 Sep 2006 18:36:35 +0300
 From: Anatoly Zaretsky [EMAIL PROTECTED]
 Subject: Re: [Haskell-cafe] Why is type 'b' forced to be type 'm a'
   and not possibly 'm a - m a'
 To: Vivian McPhail [EMAIL PROTECTED]
 Cc: Haskell Cafe haskell-cafe@haskell.org
 Message-ID:
   [EMAIL PROTECTED]
 Content-Type: text/plain; charset=ISO-8859-1; format=flowed
 
 On 9/15/06, Vivian McPhail [EMAIL PROTECTED] wrote:
 
  class Forkable a where
  fork :: String - a - a - a
 
  ...
  {-
  instance (Monad m, Forkable (m a), Forkable b) = Forkable (m a - b)
where
  fork n a1 a2 a = do
   a' - a
   fork n (a1 $ return a') (a2 $ return a')
  -}
 
 
 Let's do manual type checking.
 First, fork :: Forkable a = String - a - a - a
 So for Forkable (m a - b)
   fork :: String - (m a - b) - (m a - b) - m a - b
 Then
   fork n a1 a2 a :: b
 But you define it as
   fork n a1 a2 a = do {...}
 So it should be of type Monad t = t a, not just any `b'.
 
 Instead, you can define
   instance (Monad m, Forkable (m b)) = Forkable (m a - m b) where
 ...
 

Well, I can partially instantiate what I am trying to achieve by enumerating
cases.  Note that when the first type is a monadic type the computation gets
evaluated and then forked, but when the first type is a function it merely
gets passed.  My problem is that there are a very large number of possible
cases.  So in the case Forkable (m a - b), a number of instances of which I
can implement (e.g. Forkable (m a - m a - m a), Forkable ((m a - m a) -
m a), and Forkable (m a - (m a - m a) - m a)), I don't see why 'b' should
necessarily typecheck to 't t1'.

What I would like to be able to do is differentiate between Forkable (m a -
b) and Forkable (function type - b).

By the way, the following code typechecks and runs correctly, my problem is
that enumerating all possible types requires five factorial (120) different
instances, and to a lazy functional programmer who can 'see' the pattern it
seems that there must be a nicer way of achieving my end.

\begin{code}
instance (Monad m, Forkable (m a)) = Forkable (m a - m a) where
fork n a1 a2 a = do
 a' - a
 fork n (a1 $ return a') (a2 $ return a')

instance (Monad m, Forkable (m a)) = Forkable (m a - m a - m a) where
fork n a1 a2 a b = do
   a' - a
   fork n (a1 $ return a') (a2 $ return a') b

instance (Monad m, Forkable (m a)) = Forkable ((m a - m a) - m a) where
fork n a1 a2 a = do
 fork n (a1 a) (a2 a)

instance (Monad m, Forkable (m a)) = Forkable (m a - (m a - m a) - m a)
where
fork n a1 a2 a b = do
   a' - a
   fork n (a1 $ return a') (a2 $ return a') b

\end{code}

 Note that to compile it you also need -fallow-undecidable-instances
 and -fallow-overlapping-instances.
 
 --
 Tolik
 

Thanks for your help so far!



Cheers,

Vivian

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.12.4/449 - Release Date: 15/09/2006
 

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Problems interpreting

2006-09-18 Thread Bulat Ziganshin
Hello Carajillu,

Monday, September 18, 2006, 1:51:34 PM, you wrote:

 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)

i want to add to Andrea's answer:

 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

Haskell functions are like mathematical ones - first, you don't need
to use 'return' to denote result - just write it. second, you should
provide results for any possible input values, otherwise function will
just fail on return values for which you don't provided result. 'if'
construct ensures this by forcing to write both 'then' and 'else'
parts. so, your function, probably, should return l2 if comparison
succeeds, and x otherwise:

 check_elem x =
 if x == e then l2 else x

next problem is that x and l2 had different types and type-checking
will not allow you to write this. It's right - you can't construct
list, whose some elements are, for example,

-- i've got a break to rescue a bat that was flied into our house :)

Int and some - [Int]:  [1,[0,0,0],3,4] is impossible

if your need to replace all elements of l1 that are equal to e,
with _several_ list elements, i.e.

substitute 2 [1,2,3,4] [0,0,0] = [1,0,0,0,3,4]

you should use the following technique:

replace all elements of l1 with either l2 or [x] (where x is original
list element). result of such operation:

substitute1 2 [1,2,3,4] [0,0,0] = [[1],[[0,0,0],[3],[4]]

i.e. now all elements are lists, although some have just one element.
then you should use concat operation to transform this list into what
you need:

concat [[1],[[0,0,0],[3],[4]] = [1,0,0,0,3,4]

i think that you are interested to write the complete solution yourself.
just note that there is concatMap function that will allow you to
further simplify the code




 -- 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 -}

this definition is perfectly ok :)



-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Problems interpreting

2006-09-18 Thread Andrea Rossato
On Mon, Sep 18, 2006 at 12:42:59PM +0100, Jón Fairbairn wrote:
 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]

Pretty. Just to many keystrokes.
This should take two keystrokes less, probably:

subst e l [] = []
subst e l (x:xs) = if x == e then l ++ xs else x : subst e l xs

;-)
andrea
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Problems interpreting

2006-09-18 Thread Carajillu

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

2006-09-18 Thread Carajillu

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[2]: [Haskell-cafe] Problems interpreting

2006-09-18 Thread Bulat Ziganshin
Hello Andrea,

Monday, September 18, 2006, 3:22:43 PM, you wrote:

 substitute e l1 l2= [c | c - check_elem l1]

why not just

substitute e l1 l2= check_elem l1

? :)

 where check_elem [] = l1

should be

 where check_elem [] = []

otherwise you just append second (backup? :) copy of original list to
result :)




-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Problems interpreting

2006-09-18 Thread Andrea Rossato
On Mon, Sep 18, 2006 at 05:42:47AM -0700, Carajillu wrote:
 
 Not a good solution, it just substitutes the first occurrence of the item in
 the list. I'll try the others

I did not get this point.
You must take Jón's approach with map.
andrea
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Problems interpreting

2006-09-18 Thread Andrea Rossato
On Mon, Sep 18, 2006 at 02:52:45PM +0200, Andrea Rossato wrote:
 On Mon, Sep 18, 2006 at 05:42:47AM -0700, Carajillu wrote:
  
  Not a good solution, it just substitutes the first occurrence of the item in
  the list. I'll try the others
 
 I did not get this point.
 You must take Jón's approach with map.

or use this line in mine: 
check_elem (x:xs) = if x == e then l2 ++ check_elem xs else x : check_elem xs 

that is to say, you must check also the tail after the matched element.
andrea

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Problems interpreting

2006-09-18 Thread Andrea Rossato
On Mon, Sep 18, 2006 at 05:35:47AM -0700, Carajillu wrote:
 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.

Jón's approach (the last version of his message), usually cleaner and
more concise, is called point-free and is quite common in functional
programming. It can be a bit confusing to newcomers, though, since
part of the function's arguments do not appear explicitly in the
expressions' body (as the list to be matched and modified in your
example). 

You can read something more about this style here:
http://haskell.org/haskellwiki/Pointfree

Hope this helps.
Andrea


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Problems interpreting

2006-09-18 Thread Carajillu

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 wurbles.
 
 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


Re[2]: [Haskell-cafe] Re: Problems interpreting

2006-09-18 Thread Bulat Ziganshin
Hello Andrea,

Monday, September 18, 2006, 4:23:21 PM, you wrote:

subst e l'
= concat . map subst_elem
  where subst_elem x
| x == e = l'
| otherwise = [x]

 Pretty. Just to many keystrokes.
 This should take two keystrokes less, probably:

 subst e l [] = []
 subst e l (x:xs) = if x == e then l ++ xs else x : subst e l xs

but the goal is not keystrokes itself but easy of understanding. for
me, first solution looks rather idiomatic and intuitively
understandable. second solution requires more time to got it, but
seems easier for novices that are not yet captured higher-level
Haskell idioms. i just want to said that it will be easier to read it
if you split it into several lines:

subst e l [] = []
subst e l (x:xs) = if x == e
 then l ++ xs
 else x : subst e l xs

or

subst e l [] = []
subst e l (x:xs) | x==e  = l ++ xs
 | otherwise = x : subst e l xs

and that your solution substitutes only first match in a list:

subst 1 [1,1] [0] = [0,1]


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Problems interpreting

2006-09-18 Thread Andrea Rossato
On Mon, Sep 18, 2006 at 04:52:33PM +0400, Bulat Ziganshin wrote:
 but the goal is not keystrokes itself but easy of understanding. for
 me, first solution looks rather idiomatic and intuitively
 understandable. second solution requires more time to got it, but
 seems easier for novices that are not yet captured higher-level
 Haskell idioms. 

I was obviously kidding, as the ;-) should have made clear.
;-)

Apart for the bug (I did not understand that all the occurrences should
be replaced) I wrote something that was as close as possible to
Albert's first attempt.

For the rest, I completely agree with you and find the second one
a lot easier...

Andrea 
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Problems interpreting

2006-09-18 Thread Jón Fairbairn
Andrea Rossato [EMAIL PROTECTED] writes:

 On Mon, Sep 18, 2006 at 12:42:59PM +0100, Jón Fairbairn wrote:
  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]
 
 Pretty. Just to many keystrokes.

Keystrokes? Learn to touchtype!

 This should take two keystrokes less, probably:
 
 subst e l [] = []
 subst e l (x:xs) = if x == e then l ++ xs else x : subst e l xs

but if you want short, do this:

 subst e l' = concat.map (\x-if x==e then l' else [x])

which beats yours by twenty seven characters and one bug ;-P

-- 
Jón Fairbairn [EMAIL PROTECTED]
http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html  (updated 2006-09-13)

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Problems interpreting

2006-09-18 Thread Neil Mitchell

Hi


subst e l' = concat.map (\x-if x==e then l' else [x])


subst e l' = concatMap (\x-if x==e then l' else [x])

Let's save an extra character :)

Thanks

Neil
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Problems interpreting

2006-09-18 Thread Joachim Breitner
Hi,

Am Montag, den 18.09.2006, 16:00 +0100 schrieb Neil Mitchell:
  subst e l' = concat.map (\x-if x==e then l' else [x])
 subst e l' = concatMap (\x-if x==e then l' else [x])
 Let's save an extra character :)
We are talking keystrokes here, so count the shift key!

Greetings,
Joachim
-- 
Joachim Breitner
  e-Mail: [EMAIL PROTECTED]
  Homepage: http://www.joachim-breitner.de
  ICQ#: 74513189
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Problems interpreting

2006-09-18 Thread wld

Hi,
On 9/18/06, Joachim Breitner [EMAIL PROTECTED] wrote:

Hi,

Am Montag, den 18.09.2006, 16:00 +0100 schrieb Neil Mitchell:
  subst e l' = concat.map (\x-if x==e then l' else [x])
 subst e l' = concatMap (\x-if x==e then l' else [x])
 Let's save an extra character :)
We are talking keystrokes here, so count the shift key!

Greetings,
Joachim


Sorry, couldn't resist... If we *really* talking keystrokes, it much
depends on auto-completion features of your editor! :)

V.Rudenko
--
λ is the ultimate
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Problems interpreting

2006-09-18 Thread Lennart Augustsson

Or even shorter:

subst e l = concatMap $ \x-if x==e then l else [x]

I kinda like the list comprehension version too

subst e l1 l2 = [ r | x - l2, r - if x==e then l1 else [x] ]


On Sep 18, 2006, at 10:54 , Jón Fairbairn wrote:


Andrea Rossato [EMAIL PROTECTED] writes:


On Mon, Sep 18, 2006 at 12:42:59PM +0100, Jón Fairbairn wrote:

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]


Pretty. Just to many keystrokes.


Keystrokes? Learn to touchtype!


This should take two keystrokes less, probably:

subst e l [] = []
subst e l (x:xs) = if x == e then l ++ xs else x : subst e l xs


but if you want short, do this:


subst e l' = concat.map (\x-if x==e then l' else [x])


which beats yours by twenty seven characters and one bug ;-P

--  
Jón Fairbairn  
[EMAIL PROTECTED]
http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html  (updated  
2006-09-13)


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Problems interpreting

2006-09-18 Thread Neil Mitchell

Hi,

Out of curiosity, I've been developing a tool called Dr Haskell, for a
sample run:


module Test where

substitute1 :: Eq a = a - [a] - [a] - [a]
substitute1 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


substitute2 e l l'
 = concat (map subst_elem l)
   where subst_elem x
 | x == e = l'
 | otherwise = [x]

subst3 e l [] = []
subst3 e l (x:xs) = if x == e then l ++ xs else x : subst3 e l xs


subst4 e l' = concat.map (\x-if x==e then l' else [x])



drhaskell Test.hs


I can apply Hints.concat_map in Test.subst4
I can apply Hints.concat_map in Test.substitute2
I can apply Hints.box_append in Test.Test.Prelude.200.check_elem

For the curious, see the darcs repo:

http://www.cs.york.ac.uk/fp/darcs/drhaskell/

(Requires Yhc)

Thanks

Neil

PS. dons also contributed some of the earlier discussion to this tool,
so deserves some credit.


On 9/18/06, wld [EMAIL PROTECTED] wrote:

Hi,
On 9/18/06, Joachim Breitner [EMAIL PROTECTED] wrote:
 Hi,

 Am Montag, den 18.09.2006, 16:00 +0100 schrieb Neil Mitchell:
   subst e l' = concat.map (\x-if x==e then l' else [x])
  subst e l' = concatMap (\x-if x==e then l' else [x])
  Let's save an extra character :)
 We are talking keystrokes here, so count the shift key!

 Greetings,
 Joachim

Sorry, couldn't resist... If we *really* talking keystrokes, it much
depends on auto-completion features of your editor! :)

V.Rudenko
--
λ is the ultimate

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Problems interpreting

2006-09-18 Thread Aaron Denney
On 2006-09-18, Jón Fairbairn [EMAIL PROTECTED] wrote:
 Andrea Rossato [EMAIL PROTECTED] writes:

 On Mon, Sep 18, 2006 at 12:42:59PM +0100, Jón Fairbairn wrote:
  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]
 
 Pretty. Just to many keystrokes.

 Keystrokes? Learn to touchtype!

One has only a finite number of keystrokes before one's hands give out.
Use them wisely.

-- 
Aaron Denney
--

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Problems interpreting

2006-09-18 Thread Sam Pointon

On 18/09/06, Aaron Denney [EMAIL PROTECTED] wrote:

One has only a finite number of keystrokes before one's hands give out.
Use them wisely.


i agr rdndncy = bd  shd b stmpd out wsts bndwth 2

Slightly more seriously, a few extra keystrokes can -really- improve
clarity. For example, you can write English and still be understood
(reasonably) well without most of the vowels. But how on Earth do you
interpret ld mn gd t shp? Or any Perl/Ruby/whatever golf entry?

I'd rather waste keystrokes writing clearer code than brain cycles
understanding obfuscated code, personally.

--Sam
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Problems interpreting

2006-09-18 Thread Andrea Rossato
On Mon, Sep 18, 2006 at 11:04:27AM -0400, Lennart Augustsson wrote:
 Or even shorter:
 
 subst e l = concatMap $ \x-if x==e then l else [x]
 
 I kinda like the list comprehension version too
 
 subst e l1 l2 = [ r | x - l2, r - if x==e then l1 else [x] ]

This is the version I first wanted to (try to) implement (improvements
thanks to the thread, obviously :-):

newtype SF a b = SF { runSF :: [a] - [b] } 
instance Arrow SF where
arr f = SF (map f)
SF f  SF g = SF (f  g)
first (SF f) = SF (unzip  first f  uncurry zip)

substitute e l = arr (\x-if x==e then l else [x])  SF concat

I was studying Hughes when I read the first mail of this thread. But
you can see it yourself...

Andrea
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] darcs get-together tuesday evening (2006-09-19 18:00)

2006-09-18 Thread Eric Y. Kow
Dear darcs users, hackers and observers (*),

We're getting together around 18:00 this Tuesday to discuss and/or hack
on darcs.  This will be after the ICFP sessions for that day, in
Portland, Oregon, specifically the Marriott downtown waterfront LL1.

David Roundy will be there, Ian Lynagh and me too.
Hope to see you there.

(*) Sorry for the spam, Haskellers.  I figured we might as well try
to catch ICFP bystanders.

-- 
Eric Kow http://www.loria.fr/~kow
PGP Key ID: 08AC04F9 Merci de corriger mon français.


pgpMDJnheBANk.pgp
Description: PGP signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: foreach

2006-09-18 Thread Benjamin Franksen
Brandon Moore wrote:
 Couldn't '\' delimit a subexpression, as parentheses do? Would there be
 any ambiguity in accepting code like State \s - (s, s) instead of
 requiring State $ \s - (s, s), or taking
 
 main = do
 args - getArgs
 foreach args \arg - do
 foreach [1..3] \n - do
 putStrLn ((show n) ++ )  ++ arg
 
 It would be a bit odd to have a kind of grouping the always starts
 explicitly and ends implicitly, but other than that it seems pretty
 handy, harmless, and natural (I know I've tried to write this sort of
 thing often enough)

Sounds like an extremely good idea to me.

Ben

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] foreach

2006-09-18 Thread Twan van Laarhoven
Couldn't '\' delimit a subexpression, as parentheses do? Would there be 
any ambiguity in accepting code like State \s - (s, s) instead of 
requiring State $ \s - (s, s), or taking


Looking at the Haskell 98 language definition it seems that a whole 
class of these expressions are disallowed inside function applications:

 exp10   - \ apat1 ... apatn - exp
 | let decls in exp
 | if exp then exp else exp
 | case exp of { alts }
 | do { stmts }
 | fexp

This means that none of the following are legal Haskell declarations, 
even though they are unambiguous:

 a = State \s - (s, s)
 b = map let f x = x + 1 in f
 c = return if a then b else c
 d = catch do x - getLine
  return x

It can be argued that this is mostly obfuscation, and that it can 
sometimes be confusing, especially with let and do, but it saves on the 
amount of parentheses or $s. What was the original reasoning for 
disallowing these more complex expressions as the rightmost argument in 
an fexp? Or was this simply not considered?


Twan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Optimization problem

2006-09-18 Thread Ross Paterson
On Sun, Sep 17, 2006 at 01:08:04PM +0200, [EMAIL PROTECTED] wrote:
 Ross Paterson wrote:
  It's interesting that these composed transformations don't seem to cost
  too much.
 
 That the composed transformations are indeed cheap is not necessarily
 disturbing.

I meant the composition of the transformations of the tree (update or
reverse insert) that Bertram does for each list element.  They're cheap
because they're bounded by the depth of the tree, and even cheaper if
you're probing some other part of the tree.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Optimization problem

2006-09-18 Thread Ross Paterson
On Mon, Sep 18, 2006 at 12:23:11AM +0100, Ross Paterson wrote:
 To prove that (even for partial and infinite lists ps)
 
   splitSeq ps = [(a, seconds a ps) | a - nub ps]
 
 [...] we can establish, by induction on the input list,
 
 (1)   fst (splitSeq' s ps) =
   [(a, seconds a ps) | a - nub ps, not (member a s)]
 (2)   member x s  =
   get x s (snd (splitSeq' s ps)) = seconds x ps

Oops, nub ps should be nub (map fst ps) in each case.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Either e Monad

2006-09-18 Thread Deokhwan Kim
Dear all,

Where is the Monad instance declaration of Either e?

From the description of Control.Monad.Error, I deduce that Either e is
an instance of Monad.


http://haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-Error.html

  class Monad m = MonadError e m | m - e where ...

  Error e = MonadError e (Either e)

But, I cannot find the Monad instance declaration of Either anywhere.

Thanks.

-- 
Deokhwan Kim
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe