
Hi, how can I change the value of a variable. let x = 1 x = x + 2 First I set the value of x to 1. Then I want to increase it by 2. This way doesn't work, because I think it is a infinite expression. Is there a way to change the value? -- View this message in context: http://www.nabble.com/Change-value-of-a-variable-tp23913404p23913404.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Hello ptrash, Sunday, June 7, 2009, 9:41:56 PM, you wrote:
Hi, how can I change the value of a variable.
there are no variables in haskell :))) x, like any other identifier, is a value. when you translate to Haskell some algo that needs to update variable contents, you may either 1) use recursion: length (x:xs) = 1 + length xs length [] = 0 2) use references (IORef). like in C, references by itself are non-mutable, but they point to values that can be mutated -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

I guess the short answer is that it is not possible. 'x' is immutable
and if you want a different value than 'x' that expression has to be
given a different name like:
let x=1
y=x+2
...
But I'm not sure if that helps you. Haskell does things very
differently than the imperative languages and forces you to think
differently about how to solve problems. When I started learning
haskell I found that I had to think more about composing/decomposing
expressions and less about sequencing actions and side effects like
you do in most of the more popular languages (I really have come to
prefer the Haskell way). I think we may be able to give a more helpful
answer if give a more high level algorithm/use case... why do you want
to change the value of x
-Keith
On Sun, Jun 7, 2009 at 1:41 PM, ptrash
Hi, how can I change the value of a variable.
let x = 1 x = x + 2
First I set the value of x to 1. Then I want to increase it by 2. This way doesn't work, because I think it is a infinite expression.
Is there a way to change the value?
-- View this message in context: http://www.nabble.com/Change-value-of-a-variable-tp23913404p23913404.html 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
-- keithsheppard.name

Hi, thanks for the answers. I want to make something like a counter. I have written a recursive method which for example runs x times and counts how many times it runs, and also count some other thinks. Add the end I want a statistic about certain thinks returned by the method. -- View this message in context: http://www.nabble.com/Change-value-of-a-variable-tp23913404p23914185.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Hello ptrash, Sunday, June 7, 2009, 11:03:55 PM, you wrote:
Hi, thanks for the answers.
I want to make something like a counter. I have written a recursive method which for example runs x times and counts how many times it runs, and also count some other thinks. Add the end I want a statistic about certain thinks returned by the method.
then the best way is to add this counter as one more param of recursive procedure -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

2009/6/7 ptrash
Hi, thanks for the answers.
I want to make something like a counter. I have written a recursive method which for example runs x times and counts how many times it runs, and also count some other thinks. Add the end I want a statistic about certain thinks returned by the method.
Depending on exactly what you want, you may or may not want to look into monads, specifically the State or Writer monad. Could you give some more specific details on what you are trying to accomplish? -- Deniz Dogan

ptrash
I want to make something like a counter. I have written a recursive method which for example runs x times and counts how many times it runs, and also count some other thinks. Add the end I want a statistic about certain thinks returned by the method.
Keep in mind that a function's result depends only on its parameters, so any state you want to retain must be part of the parameters. So you might be looking for something like: iterateN x f y = if x == 0 then y else iterateN (x-1) f (f y) -k -- If I haven't seen further, it is by standing in the footprints of giants

What i am exactly to do is this: I have a list of pupils (type Pupil = (Name, Grade)) where I store the name of the pupil and which grade he has. No I want to get the number (and average number) of each grade. Something like 10 Pupils have a A (23%), 2 Pupils have a B ( 4 %) etc -- View this message in context: http://www.nabble.com/Change-value-of-a-variable-tp23913404p23914558.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Sounds like a fold to me. Try looking at the doc of either foldl/r/l'
or mapAccum depending on what you want.. Then write a function for
one iteration that returns the value from that iteration combined with
the value from the last iteration
-- Jeff
On Sun, Jun 7, 2009 at 3:44 PM, ptrash
What i am exactly to do is this:
I have a list of pupils (type Pupil = (Name, Grade)) where I store the name of the pupil and which grade he has. No I want to get the number (and average number) of each grade. Something like 10 Pupils have a A (23%), 2 Pupils have a B ( 4 %) etc -- View this message in context: http://www.nabble.com/Change-value-of-a-variable-tp23913404p23914558.html 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

Hello ptrash, Sunday, June 7, 2009, 11:44:18 PM, you wrote:
I have a list of pupils (type Pupil = (Name, Grade)) where I store the name of the pupil and which grade he has. No I want to get the number (and average number) of each grade. Something like 10 Pupils have a A (23%), 2 Pupils have a B ( 4 %) etc
it doesn't need variables, you just going to change your mind, Neo :) the way it may be calculated in FP is 1) sort list by grades 2) group it by grades 3) count number of elements in each group just look into Data.List functions. or, alternatively, read A Tour of the Haskell Prelude http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html http://undergraduate.csse.uwa.edu.au/units/CITS3211/lectureNotes/tourofprelu... -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
participants (6)
-
Bulat Ziganshin
-
Deniz Dogan
-
Jeff Heard
-
Keith Sheppard
-
Ketil Malde
-
ptrash