Re: [Haskell-cafe] Parameters and patterns

2011-10-01 Thread Wolfgang Braun
Hello,

I think you have to remember that is x in
 f x = 2 * x + 1
is just a name for the parameter and not the parameter itself.

If you look at
 g (_:xs) = xs
(_:xs) is something similar as a name. You might say '(_:xs)' stands for the
parameter (you can't say it is the name of the parameter...).  And xs is
just the name of the part of the parameter.

In the case of
 g y@(_:xs) = xs
you might probably say y is the parameter of g - but y is just a name for
that parameter.

There is a difference between names, entities and bindings. Can be confusing
if you take a look at this quote:

“The name of the song is called ‘Haddocks’ Eyes’.”
 “Oh, that’s the name of the song, is it?” Alice said, trying to feel
 interested.
 “No, you don’t understand,” the Knight said, looking a little vexed.
 “That’s what the name is called.
 The name really is ‘The Aged Aged Man’.”
 “Then I ought to have said ‘That’s what the song is called’?” Alice
 corrected herself.
 “No, you oughtn’t: that’s quite another thing! The song is called ‘Ways
 and Means’; but that’s only
 what it’s called, you know!”
 “Well, what is the song, then?” said Alice, who was by this time
 completely bewildered.
 “I was coming to that,” the Knight said. “The song really is ‘A-sitting
 On A Gate’; and the tune’s
 my own invention.”
 L. Caroll, Through the Looking Glass


2011/10/1 José Romildo Malaquias j.romi...@gmail.com

 Hello.

 When studing programming languages I have learned that parameter is a
 variable (name) that appears in a function definition and denotes the
 value to which the function is applied when the function is called.

 Argument is the value to which the function is applied.

 The parameter allows the manipulation of the argument in the body of the
 funtion definition in order to produce the result.

 Now I am not sure how to apply these concepts to Haskell, as Haskell
 uses pattern matching to deal with argument passing to functions.

 For instance, in the definition

  f x = 2 * x + 1

 x is a parameter, and in the application

  f 34

 34 is an argument.

 But in the definition

  g (_:xs) = xs

 what is the parameter of the function g? Is it the pattern (_:xs)? If so
 then a parameter is not necessarily a variable anymore, and that seems
 very strange. And what is xs? Is it a parameter, although it does not
 denote the value to which the function is aplied, but just part of it?

 I am writing some slides to use in my functional programming classes,
 but I am not sure how to deal with these terms.

 Any comments?

 Romildo
 --
 DECOM - ICEB - UFOP

 ___
 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] overloading show function

2011-06-30 Thread Wolfgang Braun
An environment contains local variable bindings, so no subcomputation will 
ever need to return its environment.
  - That is not true.  A subcomputation can possible modify an environment 
except the language forbids such a case.


On 06/30/2011 02:36 PM, Holger Siegel wrote:
 Am 29.06.2011 um 23:50 schrieb Philipp Schneider:

 Hi cafe,

 in my program i use a monad of the following type

 newtype M a = M (State - (a, State))

 i use the monad in two different ways. The type variable a can be a
 pair as in

 interp :: Term - Environment - M (Value,Environment)

 and it can be just a value as in

 type Environment = [(Name, Either Value (M Value))]
 Simple rule: Never return an environment!

 An environment contains local variable bindings, so no subcomputation will 
 ever need to return its environment. I don't know anything about the language 
 your program interprets, but I'm sure that you can rewrite function interp as

   interp :: Term - Environment - M Value

 The structure of the interpreter will become clearer and your problem will 
 vanish.


 now in any case when i print the monad, i just want to print the value
 and never the environment.

 More specific i want to use somthing like the following

 instance (Show a,Show b) = Show (M (a,b)) where
   show (M f) = let ((v,_), s) = f 0 in
 Value:  ++ show v ++   Count:  ++ show s

 instance Show a = Show (M a) where
   show (M f) = let (v, s) = f 0 in
 Value:  ++ show v ++   Count:  ++ show s

 however this gives me the following error message:

Overlapping instances for Show (M (Value, Environment))
  arising from a use of `print'
Matching instances:
  instance (Show a, Show b) = Show (M (a, b))
-- Defined at
 /home/phil/code/haskell/vorlesung/ue09/ue09-3c3.hs:78:10-42
  instance Show a = Show (M a)
-- Defined at
 /home/phil/code/haskell/vorlesung/ue09/ue09-3c3.hs:82:10-29
In a stmt of an interactive GHCi command: print it

 Any ideas how to fix it? Thanks!
 Philipp

 ___
 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 mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe