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: Caching evaluation of lazy lists (Philip Scott)
2. nim (John Moore)
3. Built-in/more elegant version of this function? (Jordan Cooper)
4. Re: nim (Kyle Murphy)
5. Re: Built-in/more elegant version of this function?
(Daniel Fischer)
6. Why is this type ambiguous? (Maur??cio CA)
7. Re: Why is this type ambiguous? (Peter Verswyvelen)
8. Re: Why is this type ambiguous? (Daniel Fischer)
9. Re: Why is this type ambiguous? (Maur??cio CA)
----------------------------------------------------------------------
Message: 1
Date: Fri, 23 Oct 2009 22:12:20 +0100
From: Philip Scott <[email protected]>
Subject: Re: [Haskell-beginners] Caching evaluation of lazy lists
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>> Thanks Daniel, that was what I was after. Is there any way of
>> investigating these things without using the profiler?
>>
>
> import Debug.Trace
>
Genius, thank you!
------------------------------
Message: 2
Date: Mon, 26 Oct 2009 11:51:47 +0000
From: John Moore <[email protected]>
Subject: [Haskell-beginners] nim
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi All,
In the game of nim I want to make a move such as take x from
pile A. This will then be subtracted from the starting list.Is this correct
in as much as I want to have a move Take 3 from pile A: input would be A3 I
need it to return a list as I wish to take this result and use zipwith (-)
[starting list][result]. How do I use the result, do I store in a variable?
typeOfMove :: (a,b) -> [Int]
typeOfMove ax
|ax = [x,0,0]
|bx = [0,x,0]
|cx = [0,0,x]
John
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20091026/457f503f/attachment-0001.html
------------------------------
Message: 3
Date: Mon, 26 Oct 2009 10:49:51 -0700
From: Jordan Cooper <[email protected]>
Subject: [Haskell-beginners] Built-in/more elegant version of this
function?
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
I was wondering if there is a built-in way to do what this function I
wrote does (or at least a more elegant way, preferably without
explicit recursion).
f xss = map safeHead xss : f (map safeTail xss)
(where safeHead/Tail are versions of head/tail that don't crash on [] input)
------------------------------
Message: 4
Date: Mon, 26 Oct 2009 14:59:16 -0400
From: Kyle Murphy <[email protected]>
Subject: Re: [Haskell-beginners] nim
To: John Moore <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
The type signature you provided for typeOfMove doesn't match the function
body.
"typeOfMove :: (a,b) -> [Int]" says that typeOfMove takes a tuple of two
different values, and returns a list of Ints, but the definition you
provided for typeOfMove only takes a list. I think perhaps what you want is
something more like:
import Data.Char (toLower)
import Control.Monad (liftM)
typeOfMove :: (Char, Int) -> [Int] -> [Int]
typeOfMove ('a', x) xs = zipWith (-) xs [x,0,0]
typeOfMove ('b', x) xs = zipWith (-) xs [0,x,0]
typeOfMove ('c', x) xs = zipWith (-) xs [0,0,x]
main :: IO ()
main = do
putStrLn "Which pile (A,B,C)?"
x <- liftM toLower $ getChar
putStrLn ""
putStrLn "How many stones?"
y <- readLn
let z = typeOfMove (x,y) [5,6,7]
putStrLn . show $ z
Of course, another way to do much the same thing is the following, which has
the advantage that you don't get an exception inside the typeOfMove function
if the user passes in a value besides A, B, or C, although you do get an
exception when readLn attempts to perarse the value (which is better because
you can trap the exception there and re-ask for the correct value).
Similarly this version still isn't very good because it doesn't do any sort
of bounds checking on the number of stones to subtract, but I leave that as
an exercise to you.
data PileName = A | B | C deriving (Show, Eq, Read)
typeOfMove :: (PileName, Int) -> [Int] -> [Int]
typeOfMove (A, x) xs = zipWith (-) xs [x,0,0]
typeOfMove (B, x) xs = zipWith (-) xs [0,x,0]
typeOfMove (C, x) xs = zipWith (-) xs [0,0,x]
main :: IO ()
main = do
putStrLn "Which pile A, B, or C (case matters)?"
x <- readLn
putStrLn "How many stones?"
y <- readLn
let z = typeOfMove (x,y) [5,6,7]
putStrLn . show $ z
-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.
On Mon, Oct 26, 2009 at 07:51, John Moore <[email protected]> wrote:
> Hi All,
> In the game of nim I want to make a move such as take x from
> pile A. This will then be subtracted from the starting list.Is this correct
> in as much as I want to have a move Take 3 from pile A: input would be A3 I
> need it to return a list as I wish to take this result and use zipwith (-)
> [starting list][result]. How do I use the result, do I store in a variable?
>
> typeOfMove :: (a,b) -> [Int]
> typeOfMove ax
> |ax = [x,0,0]
> |bx = [0,x,0]
> |cx = [0,0,x]
>
> John
>
> _______________________________________________
> 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/20091026/6f164a18/attachment-0001.html
------------------------------
Message: 5
Date: Mon, 26 Oct 2009 19:59:32 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Built-in/more elegant version of this
function?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Montag 26 Oktober 2009 18:49:51 schrieb Jordan Cooper:
> I was wondering if there is a built-in way to do what this function I
> wrote does (or at least a more elegant way, preferably without
> explicit recursion).
>
> f xss = map safeHead xss : f (map safeTail xss)
>
> (where safeHead/Tail are versions of head/tail that don't crash on []
> input)
Not quite the same, but
Prelude> :m +Data.List
Prelude Data.List> transpose [[1,2],[],[3,4,5]]
[[1,3],[2,4],[5]]
could be more or less what you want.
------------------------------
Message: 6
Date: Mon, 26 Oct 2009 20:24:26 -0200
From: Maur??cio CA <[email protected]>
Subject: [Haskell-beginners] Why is this type ambiguous?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
import Foreign
import Foreign.C
genericCast :: (Storable a, Storable b) => a -> IO b
genericCast v = let
dummy = undefined
size = max (sizeOf v) (sizeOf dummy)
in if False
then return dummy
else allocaBytes size $ \p -> poke p v >> peek (castPtr p)
----
Code above gives me this:
Ambiguous type variable `a' in the constraint:
`Storable a'
arising from a use of `sizeOf' at src/Bindings/C.hs:28:27-38
----
It seems to refer to '(sizeOf dummy)'. But isn't the
type of 'dummy' defined by 'return dummy' beeing a
possible return value (and, so, dummy :: b)?
Thanks,
Maurício
------------------------------
Message: 7
Date: Mon, 26 Oct 2009 23:38:50 +0100
From: Peter Verswyvelen <[email protected]>
Subject: Re: [Haskell-beginners] Why is this type ambiguous?
To: Maur??cio CA <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
I have no idea what you're trying to do here (looks as if you just
want to recreate Unsafe.Coerce.unsafeCoerce?)
However, dummy is first used an 'a' and then as a 'b', so that can't work.
Next, in sizeOf undefined, undefined does not provide enough
information about that type undefined should be. Although the compiler
can infer that sizeOf undefined must be of the same type as sizeOf v,
that's not enough, it needs to know the type of undefined (I guess you
want it to be the same type as v?)
Can be fixed like:
genericCast :: (Storable a, Storable b) => a -> IO b
genericCast v = let
size = max (sizeOf v) (sizeOf $ undefined `asTypeOf` v)
in if False
then return undefined
else allocaBytes size $ \p -> poke p v >> peek (castPtr p)
But... feels insanely hacky, and is not something a beginner should
attempt to play with I guess ;)
2009/10/26 Maurício CA <[email protected]>:
> import Foreign
> import Foreign.C
>
> genericCast :: (Storable a, Storable b) => a -> IO b
> genericCast v = let
> dummy = undefined
> size = max (sizeOf v) (sizeOf dummy)
> in if False
> then return dummy
> else allocaBytes size $ \p -> poke p v >> peek (castPtr p)
>
> ----
>
> Code above gives me this:
>
> Ambiguous type variable `a' in the constraint:
> `Storable a'
> arising from a use of `sizeOf' at src/Bindings/C.hs:28:27-38
>
> ----
>
> It seems to refer to '(sizeOf dummy)'. But isn't the
> type of 'dummy' defined by 'return dummy' beeing a
> possible return value (and, so, dummy :: b)?
>
> Thanks,
> Maurício
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 8
Date: Tue, 27 Oct 2009 00:11:20 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Why is this type ambiguous?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Montag 26 Oktober 2009 23:24:26 schrieb Maurício CA:
> import Foreign
> import Foreign.C
>
> genericCast :: (Storable a, Storable b) => a -> IO b
> genericCast v = let
> dummy = undefined
> size = max (sizeOf v) (sizeOf dummy)
> in if False
> then return dummy
> else allocaBytes size $ \p -> poke p v >> peek (castPtr p)
>
> ----
>
> Code above gives me this:
>
> Ambiguous type variable `a' in the constraint:
> `Storable a'
> arising from a use of `sizeOf' at src/Bindings/C.hs:28:27-38
>
> ----
>
> It seems to refer to '(sizeOf dummy)'. But isn't the
> type of 'dummy' defined by 'return dummy' beeing a
> possible return value (and, so, dummy :: b)?
No. let-bindings are polymorphic, so dummy :: forall a. a
{-# LANGUAGE ScopedTypeVariables #-}
-- This is unsafe, don't use
genericCast :: forall a b. (Storable a, Storable b) => a -> IO b
genericCast v = let
dummy :: b
dummy = undefined
...
>
> Thanks,
> Maurício
------------------------------
Message: 9
Date: Mon, 26 Oct 2009 21:12:15 -0200
From: Maur??cio CA <[email protected]>
Subject: [Haskell-beginners] Re: Why is this type ambiguous?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> I have no idea what you're trying to do here (looks as if you
> just want to recreate Unsafe.Coerce.unsafeCoerce?)
Yes, except that it works with Storables only and is safe against
runtime corruption. Evil, but can save you from worst FFI hacks.
> However, dummy is first used an 'a' and then as a 'b', so that
> can't work.
Where is dummy used as 'a'? The only place with a specific type I
used it is in 'return dummy', and it's there exactly to "get" that
'b' type.
> Although the compiler can infer that sizeOf undefined must be of
> the same type as sizeOf v, (...)
>
> size = max (sizeOf v) (sizeOf $ undefined `asTypeOf` v)
No, it's not the same type! The idea is that 'size' value should
be the bigger of 'a' and 'b' sizes, so that I guarantee enough
memory will be allocated for the cast.
> But... feels insanely hacky, and is not something a beginner
> should attempt to play with I guess ;)
I have an unusual experience with Haskell. I used it a lot, but
mainly with FFI. So, I'm an expert in FFI -- I have my own package
of hsc2hs macros :) -- but I have no understanding of the type
system, except for just the basics.
Maurício
>> import Foreign
>> import Foreign.C
>>
>> genericCast :: (Storable a, Storable b) => a -> IO b
>> genericCast v = let
>> dummy = undefined
>> size = max (sizeOf v) (sizeOf dummy)
>> in if False
>> then return dummy
>> else allocaBytes size $ \p -> poke p v >> peek (castPtr p)
>>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 16, Issue 21
*****************************************