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: Recursion in monad (Adrian May)
2. small expression evaluator (Petr Novotnik)
3. Re: small expression evaluator (Henk-Jan van Tuyl)
----------------------------------------------------------------------
Message: 1
Date: Tue, 22 Mar 2011 12:46:54 +0800
From: Adrian May <[email protected]>
Subject: Re: [Haskell-beginners] Recursion in monad
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
OK I did this:
import System.Random
walk :: Int -> IO Int
walk i = randomRIO (0,1) >>= \r -> return (i+r*2-1)
say :: Int -> IO ()
say i = putStrLn $ show i
rep :: Int -> a -> (a -> IO a) -> (a -> IO ()) -> IO ()
rep n i w s
| n<=0 = return ()
| otherwise = s i >> w i >>= \ii -> rep (n-1) ii w s
main :: IO ()
main = rep 10 50 walk say
Is that the easiest way?
Adrian.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110322/72654dd7/attachment-0001.htm>
------------------------------
Message: 2
Date: Tue, 22 Mar 2011 08:56:45 +0100
From: Petr Novotnik <[email protected]>
Subject: [Haskell-beginners] small expression evaluator
To: Haskell Beginners List <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Hello,
on my journey through Haskell I've hit a problem where I don't know any
further and would greatly appreciate your help.
I've got this code so far:
data Value a e = VConst a
| VFunc (e -> a)
evalV :: Value a e -> e -> a
evalV (VConst x) = const x
evalV (VFunc f) = f
liftV :: (a -> a -> t) -> (Value a e) -> (Value a e) -> e -> t
liftV f x y e = (evalV x e) `f` (evalV y e)
(.==.), (./=.) :: (Eq a) => (Value a e) -> (Value a e) -> e -> Bool
(.==.) = liftV (==)
(./=.) = liftV (/=)
(.&&.), (.||.) .... some more operators
This allows me to write client code like this:
data Person = Person {
personName :: String
, personAge :: Int
}
deriving (Show)
exampleExpr :: Bool
exampleExpr = (VConst 99) .==. (VFunc personAge) $ Person "pete" 99
I was wondering, whether it'd be possible to enable defining expression
without the Value data constructors, i.e.
99 .==. personAge $ Person "pete" 99
I've tried using a type class to have a function for lifting different
types into Values.
class ToValue a e | a -> e where
valueLift :: a -> Value a e
then I tried defining an instance for functions:
instance ToValue ((->) Person a) Person where
valueLift f = undefined
in ghci:
> :t valueLift personName
valueLift personName :: Value (Person -> String) Person
and suddenly I realized this will not work because I would need the type
`Value String Person'.
Now, I'm stuck and don't know which path to take in order to come closer
to being able writing "99 .==. personAge" or "personAge .==. 99". Maybe
I'm approaching it in a completely wrong way or it's simply not
possible. Do you have any ideas, hints?
Many thanks in advance for any kind of feedback,
pete.
------------------------------
Message: 3
Date: Tue, 22 Mar 2011 10:14:57 +0100
From: "Henk-Jan van Tuyl" <[email protected]>
Subject: Re: [Haskell-beginners] small expression evaluator
To: "Haskell Beginners List" <[email protected]>, "Petr Novotnik"
<[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-15; format=flowed;
delsp=yes
On Tue, 22 Mar 2011 08:56:45 +0100, Petr Novotnik
<[email protected]> wrote:
> data Person = Person {
> personName :: String
> , personAge :: Int
> }
> deriving (Show)
>
> exampleExpr :: Bool
> exampleExpr = (VConst 99) .==. (VFunc personAge) $ Person "pete" 99
>
>
> I was wondering, whether it'd be possible to enable defining expression
> without the Value data constructors, i.e.
>
>
> 99 .==. personAge $ Person "pete" 99
You can write:
99 == personAge (Person "pete" 99)
Regards,
Henk-Jan van Tuyl
--
http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
--
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 33, Issue 30
*****************************************