Am Freitag 29 Januar 2010 22:38:11 schrieb Han Joosten:
> knyttr wrote:
> fun x   = ...
> fun x y  = ...
> fun x y z = ...
> would make more sence.
> however, there still is overlap. (ghc could generate a warning for
> now the where part: there are several options:
> fun x y z
> | z ==  = ...
> | y ==  = ...
> | otherwise = ...
fun x y z
| null z = ...
| null y = ...
| otherwise = ....
> or using a case expression...
> fun x y z = case (x,y,z) of
> (_ , _ , ) -> ...
> (_ ,  , [_:_]) -> ...
> (_ , [_:_], [_:_]) -> ...
> pattern matches do not overlap this way. you could use this pattern
> matches also in the previous solution.
> Then your second question:
> > 2. how can I change a field in a list in the most easy way? the only
> > solution I found is below. I am not very happy with it - it does too
> > many operations just to change one field.
> > changeIn array index value = (take (index-1) array)++[value]++(drop
> > index array)
> Remember, in haskell you do not 'change' values in variables. Thers is
> no such thing as a variable. You probably want a function that takes a
> list, an index and a value. It produces a list with all elements from
> the first list, only the n-th element is replaced with the given value.
> I would specify this as follows:
> f x:xs i v
> | i == 1 = v:xs
> | i > 1 = x : (f xs (i-1) v)
> but there are probably dozens of other ways to do this. While you are
> new to Haskell, do not worry too much about performance.
True, but also
*** be aware that lists are NOT arrays ***
trying to use them as such is sure to land you in a deep hole of horrible
performance sooner or later.
If what you need is an array, use arrays (immutable or mutable, depending
on the problem at hand). If you need a set, use sets (Data.Set e.g.), if a
list is appropriate for what you want to do, use the versatile .
> (ghc will surly
> surprise you =^D) The main thing is to think functions. If you do that,
> you will appreciate Haskell for sure.
Haskell-Cafe mailing list