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.  Choosing a function by (string) name... (Stuart Hungerford)
   2.  Choosing a function randomly... (Stuart Hungerford)
   3. Re:  Choosing a function by (string) name... (Magnus Therning)
   4. Re:  Choosing a function by (string) name... (Stuart Hungerford)
   5. Re:  Choosing a function randomly... (Ertugrul S?ylemez)


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

Message: 1
Date: Sun, 20 May 2012 16:28:05 +1000
From: Stuart Hungerford <[email protected]>
Subject: [Haskell-beginners] Choosing a function by (string) name...
To: [email protected]
Message-ID:
        <cag+kmrf1jgzu9dy9fsj8yrx7uw5pusgibozypo9zgy9y4pc...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

Suppose I have a module of functions with identical signatures:

module Functions where

a :: Int -> [Int]

a x = [x + 1, x + 2, x + 3]

b :: Int -> [Int]

b x = [x + 4, x + 5, x + 6]

and in my main function I receive a command line arguments (a string)
naming one of the functions in MyModule. I'd like to lookup and use of
the functions by name:

lookup :: String -> (Int -> [Int])

lookup "a" = MyModule.a

lookup "b" = MyModule.b

...

Is there a more idiomatic Haskell way of going about this?  I can see
there's a fundamental tension between a runtime choice of function
name expressed as a string and the need for compile-time known
function choice.

Thanks,

Stu



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

Message: 2
Date: Sun, 20 May 2012 16:42:07 +1000
From: Stuart Hungerford <[email protected]>
Subject: [Haskell-beginners] Choosing a function randomly...
To: [email protected]
Message-ID:
        <CAG+kMrEOgz=p7=5k_-O981ZCBHXb75SRABn+hC1kG=yomnk...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

This is kind-of related to my earlier question on looking up functions
by name.  Suppose I have a module with a number of functions with the
same signature:

scale :: Int -> Int -> Int

scale s x = s * x

add :: Int -> Int -> Int

add a x = a + x

...

I'd like to choose and run one of these functions randomly at run
time. I can see I could use some kind of case expression:

op :: Int -> Int -> Int

op p x = case random(1,2) of
   1 -> scale p x
   2 -> add p x

Or some kind of pattern guards:

op p x
  | random(1,2) == 1 = scale p x
  | otherwise  = add p x

Although that method won't work as is for more than two choices.  Are
these methods the most idiomatic way of randomly choosing a function?
 How hard would it be to use the machinery of the QuickCheck library
for this, given it must be doing something similar in test suites?

Thanks,

Stu



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

Message: 3
Date: Sun, 20 May 2012 09:51:15 +0200
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] Choosing a function by (string)
        name...
To: Stuart Hungerford <[email protected]>
Cc: [email protected]
Message-ID:
        <caaexw5uvf+r8yph-hknvq8xia5ms0bzo7lshwbnfvxpa8h7...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Sun, May 20, 2012 at 8:28 AM, Stuart Hungerford
<[email protected]> wrote:
> Hi,
>
> Suppose I have a module of functions with identical signatures:
>
> module Functions where
>
> a :: Int -> [Int]
>
> a x = [x + 1, x + 2, x + 3]
>
> b :: Int -> [Int]
>
> b x = [x + 4, x + 5, x + 6]
>
> and in my main function I receive a command line arguments (a string)
> naming one of the functions in MyModule. I'd like to lookup and use of
> the functions by name:
>
> lookup :: String -> (Int -> [Int])
>
> lookup "a" = MyModule.a
>
> lookup "b" = MyModule.b
>
> ...
>
> Is there a more idiomatic Haskell way of going about this? ?I can see
> there's a fundamental tension between a runtime choice of function
> name expressed as a string and the need for compile-time known
> function choice.

Since the two functions have the same type there is no problem to do
this, and in fact you have already hinted at a reasonable solution
yourself: create an assoctiation list and use lookup
(http://is.gd/wZPlNJ).

/M

-- 
Magnus Therning ? ? ? ? ? ? ? ? ? ? ?OpenPGP: 0xAB4DFBA4
email: [email protected] ? jabber: [email protected]
twitter: magthe ? ? ? ? ? ? ? http://therning.org/magnus



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

Message: 4
Date: Sun, 20 May 2012 18:54:46 +1000
From: Stuart Hungerford <[email protected]>
Subject: Re: [Haskell-beginners] Choosing a function by (string)
        name...
To: Magnus Therning <[email protected]>
Cc: [email protected]
Message-ID:
        <CAG+kMrFv0P0_aHmup02hk=h1bl0p_wmuetndui7id-1nzf3...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

On Sun, May 20, 2012 at 5:51 PM, Magnus Therning <[email protected]> wrote:

> [...]
> Since the two functions have the same type there is no problem to do
> this, and in fact you have already hinted at a reasonable solution
> yourself: create an assoctiation list and use lookup
> (http://is.gd/wZPlNJ).

Thanks Magnus, that's a clear and simple approach.

Stu



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

Message: 5
Date: Sun, 20 May 2012 11:13:22 +0200
From: Ertugrul S?ylemez <[email protected]>
Subject: Re: [Haskell-beginners] Choosing a function randomly...
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Stuart Hungerford <[email protected]> wrote:

> This is kind-of related to my earlier question on looking up functions
> by name.  Suppose I have a module with a number of functions with the
> same signature:
>
> [...]
>
> I'd like to choose and run one of these functions randomly at run
> time. [...]

Again the lookup approach seems most reasonable.  The cleanest way is to
define a simple name type for your functions:

    data FuncIx = FuncA | FuncB deriving (Ord)

    instance Random FuncIx where
        ...

    funcA :: A -> B
    funcB :: A -> B

    funcs :: Map FuncIx (A -> B)
    funcs = M.fromList (zip [FuncA, FuncB] [funcA, funcB])

If you want to go for maximum speed instead:

    import qualified Data.Vector as V

    type FuncIx = Int

    ...

    funcs :: V.Vector (A -> B)
    funcs = V.fromList [funcA, funcB]

    randFunc :: (RandomGen g) => g -> (A -> B, g)
    randFunc = first (funcs V.!) . randomR (0, 1)


Greets,
Ertugrul

-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120520/0fef612d/attachment-0001.pgp>

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

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


End of Beginners Digest, Vol 47, Issue 13
*****************************************

Reply via email to