Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  if statements (Marc Weber)
   2. Re:  if statements (Daniel Fischer)
   3. Re:  if statements (Roel van Dijk)
   4. Re:  Re: Re: testing and the culture of Haskell (Adrian Adshead)
   5. Re:  Re: Re: testing and the culture of Haskell
      (Brandon S. Allbery KF8NH)


----------------------------------------------------------------------

Message: 1
Date: Sat, 23 Jan 2010 19:31:48 +0100
From: Marc Weber <[email protected]>
Subject: Re: [Haskell-beginners] if statements
To: beginners <[email protected]>
Message-ID: <1264271305-sup-9...@nixos>
Content-Type: text/plain; charset=UTF-8

Sorry I missed something:

> evalStep (Add x y) =
>   let (Val xEvaled) = evalStep x
>       (Val yEvaled) = evalStep y
>   in Val (xEvaled + yEvaled)

should work. So you either have to use this let binding or you have to
add a getVal function. And this is why I love Haskell. By thinking about
the code you notice that you really want

evalStep :: Expression -> result

where result is Float, Int or Ratianal or such. Then you can use the
code I wrote in my previous mail.

Marc Weber


------------------------------

Message: 2
Date: Sat, 23 Jan 2010 19:33:10 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] if statements
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Samstag 23 Januar 2010 18:40:10 schrieb John Moore:
> Hi
>    I'm trying to get a if statement to work but not sure if this is the
> right approach,

I think pattern matching and case .. of would be better suited.

> what I want to do in simple english is to evaluate  one
> expression at a time for example. (3+2)+(4+5) I want it to return 5 +
> (4+5) then 5+9.
>
> data Expression = Val Integer
>
>                 | Add Expression Expression
>                 | Subtract Expression Expression
>                 | Multiply Expression Expression
>                 | Divide Expression Expression
>
>          deriving Show
> evalStep :: Expression ->  Expression
> evalStep (Val x)=  (Val x)
>
> evalStep (Add x y) = do
>
>   if x = isDigit
>            then if y isDigit
>              else evalStep x,y
>

-- evaluate one step only
evalStep (Add x y)
    = case x of
        (Val v) -> case y of
                     (Val w) -> Val (v+w)
                     other -> Add x (evalStep y)
        other -> Add (evalStep x) y
evalStep (Subtract x y) ... -- analogously, also Multiply and Divide

evalStep e = e -- only Val left.

Voilà, the leftmost innermost compound expression is reduced if the 
expression isn't fully evaluated. To completely evaluate:

eval :: Expression -> Expression
eval e@(Val _) = e
eval e = eval (evalStep e)

> I thinking about using recursion down a tree left then right but that
> seems very complicated.
> Any thoughts welcome!
> John



------------------------------

Message: 3
Date: Sat, 23 Jan 2010 20:13:09 +0100
From: Roel van Dijk <[email protected]>
Subject: Re: [Haskell-beginners] if statements
To: John Moore <[email protected]>
Cc: Beginners Haskell <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi John,

The @ in a pattern is called an 'as-pattern'. They are explained in
this tutorial: http://www.haskell.org/tutorial/patterns.html

Very briefly they assign a name to a pattern.

I'm not sure why you get a type error. I could load the code into GHCI
without problems. When the compiler complains about missing instanced
for Num than it usually means you did something like (x + y) where x
and y are of type E without E having an instance for Num. For example
(Val 3) + (Val 4) would result in such an error.

On Sat, Jan 23, 2010 at 7:32 PM, John Moore <[email protected]> wrote:
> Hi Roel,
>              This isn't working, what does @ stand for in
> evalStep (Add x@(Val _) y) = Add x (evalStep y)
> Maybe this is a mistype
> It's asking me for a instance declaration for (num expression)
>
> John


------------------------------

Message: 4
Date: Sat, 23 Jan 2010 20:31:47 +0000 (GMT)
From: Adrian Adshead <[email protected]>
Subject: Re: [Haskell-beginners] Re: Re: testing and the culture of
        Haskell
To: Alexander Dunlap <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi Alex,

That does help, but raises a further question.

If  (>>=) :: m a -> (a -> m b) -> m b

Then whenever the IO monad is entered it can not be escaped,
so in the example you gave:-

f :: IO a -> Int
f _ = 10


while f (putStrLn "foo") is a pure function evaluating to 10,
neither the IO Action, nor the a (from 'IO a') can be used without
using the (>>=) or (>>). Therefore since the parameter can not
be used in the function f without entering the IO monad, your
function would be equivalent to :-

f :: Int
f = 10

I have two questions to try and clarify my understanding.

1) Is it possible to have a function with an IO Action as an input
parameter (and not just ignore the parameter) and have a non-IO
return value?
(ie is     f :: IO a -> b    possible without ignoring the IO a)

2) Is it possible to have a function which evaluates any IO Action
within the body and have a non-IO return value?
(ie is    f :: a -> b     possible while evaluating any IO Action
within the body.)



My understanding of Haskell was that both of these situations
would not be possible, and the return value would always be
of the form IO b. (For these questions I am excluding use of
unsafePerformIO)

Many thanks for your time.

Adrian.


--- On Sat, 23/1/10, Alexander Dunlap <[email protected]> wrote:

From: Alexander Dunlap <[email protected]>
Subject: Re: [Haskell-beginners] Re: Re: testing and the culture of Haskell
To: "Adrian Adshead" <[email protected]>
Cc: [email protected]
Date: Saturday, 23 January, 2010, 5:22

When you put actions in a "do" block, what you are really doing under
the hood is using the ">>=" function to string actions together. (>>=)
:: m a -> (a -> m b) -> m b, so, when m ~ IO, (>>=) :: IO a -> (a ->
IO b) -> IO b. Thus, a do block of IO actions will itself be an IO
action. However, consider the function:

f :: IO a -> Int
f _ = 10

Now the value f (putStrLn "foo") is totally pure, and has the value of
10. A similar thing is going on with the greetAdrian function.

Hope that helps.

Alex

On Fri, Jan 22, 2010 at 7:34 PM, Adrian Adshead
<[email protected]> wrote:
>
> OK, so I get the idea that all functions are pure because they
> just return actions.
>
> So as I understand it now...
>
> All functions are pure.
> The evaluation of all functions not declared as IO results in no side effects.
> The evaluation of all functions declared as IO may or may not have side 
> effects.
>
> But how can the function greetAdrian from the example below not be an IO
> operation?
>
>
>
>
> From: Maciej Piechotka <uzytkownik2 <at> gmail.com>
> Subject: Re: Re: testing and the culture of Haskell
> Newsgroups: gmane.comp.lang.haskell.beginners
> Date: 2010-01-23 00:31:42 GMT (2 hours and 52 minutes ago)
>
> On Fri, 2010-01-22 at 20:59 +0000, Adrian Adshead wrote:
> > >All Haskell functions are pure without exception.  For example:
> > >
> > >greet :: String -> IO ()
> > >greet name = putStrLn $ "Hello, "++name
> > >
> > >This is a pure function from String to IO ().  This function (like all
> > >Haskell functions) has no side effects.  Its return value of type IO ()
> > >merely _represents_ an IO action.  The runtime system knows how to act
> > >on this representation.
> > >
> > >This also means that there is no such thing in Haskell as marking a
> > >function as side-effecting.
> > >
> > >This distinction may be subtle, but it's important.
> > >
> > >
> > >Steve
> >
> > Steve,
> >
> > Please could you clarify this for me since you are making exactly
> > the opposite assertion than I have
>  understood.
> >
> > I am confused by you stating "All Haskell functions are pure
> > without
> >  exception.".
> >
> > Pure functions have no impact on 'anything'. They take input
> > parameters (which they don't change) and return exactly the
> > same result whenever the same input parameters are given.
> >
> > >greet :: String -> IO ()
> > >greet name = putStrLn $ "Hello, "++name
> >
> > This example you gave is not a pure function since it does have
> > the side effect that the screen is changed by outputting the string
> > "Hello, " and the name passed in.
> >
> >
>
> greatAdrian :: String
> greetAdrian = let x = greet "Adrian"
>               in x `seq` f x
>
> greet can be consider a pure function and value IO () is evaluated by
> seq. IO () represents an action(s) not execution of action(s). If f of x
> does not use any tricks nothing will be
>  printed.
>
> IO a value it can be:
> - cast unsafely into a. However I guess we omit this shame for a moment
> - binded with other action. But the resultant type is now again IO b. So
> we still get a something
> - returned as main. Then we might consider whole Haskell program as
> metalanguage which returns single thing - other program. In similar way
> as:
>
> type PythonProgram = String
> main :: PythonProgram
> main = "print \"Hello World\""
>
> is pure add_impure is pure. What we do with the result is other thing.
>
> Regards
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100123/15c3d4f6/attachment-0001.html

------------------------------

Message: 5
Date: Sat, 23 Jan 2010 16:08:26 -0500
From: "Brandon S. Allbery KF8NH" <[email protected]>
Subject: Re: [Haskell-beginners] Re: Re: testing and the culture of
        Haskell
To: Adrian Adshead <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Skipped content of type multipart/alternative-------------- next part 
--------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20100123/28534890/PGP.bin

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 19, Issue 22
*****************************************

Reply via email to