Bob Ippolito | 18 Apr 21:54 2014

Re: Learn You a Haskell! I have a few questions!




On Fri, Apr 18, 2014 at 12:42 PM, Bob Ippolito <bob <at> redivi.com> wrote:



On Fri, Apr 18, 2014 at 8:12 AM, Gilberto Melfe <gilbertomelfe <at> gmail.com> wrote:
Hi there to you all!

I've been reading through the first chapter of "Learn You a Haskell" and I'd like to ask the community a few questions.

Any help would be appreciated...

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

All the standard Prelude functions that throw out an error when fed the []!

head
maximum
...

Are there safe versions anywhere, or do we have to define them ourselves?

Not so many that ship with GHC or Haskell Platform, but you can install safe:

Some of these you can work around, for example you can get a safe version of `head` just by using Data.Maybe.listToMaybe
 
-- ---------- ---------- ---------- ---------- ----------

This one is really important!

I understand that for the definition of product to work as it is:
  product [] must be equal to 1

But what if we want to add a product to something else???
Shouldn't the result be Nothing?
(I guess we would have to guard against this! But we must guard against the parameter of product being the empty list, anyway. Otherwise we risk adding 1 when there is nothing do multiply)

(The same question arises with the functions and and or, and their boolean results, I think! Right?)

There's a precedent in mathematics for behaving like this. 0! and n^0 are both equal to 1 for example. It sounds like perhaps you're trying to do something strange with products of lists, and there might be a better way but it's hard to suggest something without a concrete example.
 

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

-- ---------- Start Quote

Names can't be enumerated. What comes after "John"? I don't know.
 
-- ---------- End Quote

"a" to "z" then "aa" to "zz" then "aaa" to "zzz" and so on! Is it to difficult or impossible to create a function that enumerates all possible strings?

This is not hard to implement, but you don't know which of those strings are names.
 
-- ---------- ---------- ---------- ---------- ----------

-- ---------- Start Quote

To make a list with all the numbers from 20 to 1, you can't just do [20..1], you have to do [20,19..1].

-- ---------- End Quote

Why is this? If the first was greater than the second it would just subtract! Right?

[a..b] is syntax sugar for enumFromTo and the definition of that function just doesn't behave in that way. [a, b .. c] is syntax sugar for enumFromThenTo which does. A reason for it to behave like this would be that it's often desired to have the behavior that it does. Consider enumerating every index in a list `xs` except for the first, you could write this as `[1 .. length xs - 1]` with the current syntax, but that sort of thing would yield surprising results if it sometimes went backwards.
 
-- ---------- ---------- ---------- ---------- ----------

-- ---------- Start Quote

Watch out when using floating point numbers in ranges! Because they are not completely precise (by definition), their use in ranges can yield some pretty funky results.

ghci> [0.10.3 .. 1]
[0.1,0.3,0.5,0.7,0.8999999999999999,1.0999999999999999]

-- ---------- End Quote

Can anyone explain me why it works for the first few values, and not "completely"?

It doesn't "work" for any of the values, it's just an artifact of how they're rendered. 0.1 can't be exactly represented in binary floating point, so the error compounds. Double probably shouldn't be enumerable in the first place, but that's a decision we have to live with. The reason that the end result is so surprising is that 1.0999999999999999 is less than 1 + 0.1 and for whatever reason the way Enum is defined for Double checks to see if the result is > to+(then-from) rather than <= to.

To clarify, this is what is happening:

    λ> takeWhile (< 1 + 0.1) $ iterate (+0.1) 0.1
    [0.1,0.2,0.30000000000000004,0.4,0.5,0.6,0.7,0.7999999999999999,0.8999999999999999,0.9999999999999999,1.0999999999999999]
<div><div dir="ltr">
<br><div class="gmail_extra">
<br><br><div class="gmail_quote">On Fri, Apr 18, 2014 at 12:42 PM, Bob Ippolito <span dir="ltr">&lt;<a href="mailto:bob <at> redivi.com" target="_blank">bob <at> redivi.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote"><div dir="ltr">
<br><div class="gmail_extra">
<br><br><div class="gmail_quote">
<div class="">On Fri, Apr 18, 2014 at 8:12 AM, Gilberto Melfe <span dir="ltr">&lt;<a href="mailto:gilbertomelfe <at> gmail.com" target="_blank">gilbertomelfe <at> gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote"><div dir="ltr">
<div>Hi there to you all!<br><br>
</div>
<div>

I've been reading through the first chapter of "Learn You a Haskell" and I'd like to ask the community a few questions.<br><br>
</div>
<div>Any help would be appreciated...<br><br>
</div>
<div>-- ----------  ----------  ----------  ----------  ----------  <br><br>
</div>
<div>All the standard Prelude functions that throw out an error when fed the []!<br><br>
</div>
<div>head<br>
</div>
<div>maximum<br>
</div>
<div>...<br><br>
</div>
<div>Are there safe versions anywhere, or do we have to define them ourselves?</div>
</div></blockquote>
<div><br></div>
</div>
<div>Not so many that ship with GHC or Haskell Platform, but you can install safe:</div>

<div>
<a href="https://hackage.haskell.org/package/safe" target="_blank">https://hackage.haskell.org/package/safe</a><br>
</div>
<div><br></div>
<div>Some of these you can work around, for example you can get a safe version of `head` just by using Data.Maybe.listToMaybe</div>
<div class="">
<div>&nbsp;</div>
<blockquote class="gmail_quote"><div dir="ltr">
<div>-- ----------  ----------  ----------  ----------  ----------  <br><br>This one is really important!<br><br>
</div>
<div>I understand that for the definition of product to work as it is:<br>&nbsp; product [] must be equal to 1<br>
</div>
<div><br></div>
<div>But what if we want to add a product to something else???<br>
</div>
<div>Shouldn't the result be Nothing?<br>
</div>
<div>(I guess we would have to guard against this! But we must guard against the parameter of product being the empty list, anyway. Otherwise we risk adding 1 when there is nothing do multiply)<br><br>(The same question arises with the functions and and or, and their boolean results, I think! Right?)<br>
</div>
</div></blockquote>
<div><br></div>
</div>
<div>There's a precedent in mathematics for behaving like this. 0! and n^0 are both equal to 1 for example. It sounds like perhaps you're trying to do something strange with products of lists, and there might be a better way but it's hard to suggest something without a concrete example.</div>
<div class="">
<div>&nbsp;</div>
<blockquote class="gmail_quote"><div dir="ltr">
<div>
<br>-- ----------  ----------  ----------  ----------  ----------  <br><br>-- ----------  Start Quote<br><br>Names can't be enumerated. What comes after "John"? I don't know.<br>&nbsp;<br>--  ---------- End Quote<br><br>
</div>
<div>"a" to "z" then "aa" to "zz" then "aaa" to "zzz" and so on! Is it to difficult or impossible to create a function that enumerates all possible strings?<br>
</div>
</div></blockquote>
<div><br></div>
</div>
<div>This is not hard to implement, but you don't know which of those strings are names.</div>
<div class="">
<div>&nbsp;</div>
<blockquote class="gmail_quote">

<div dir="ltr">
<div>-- ----------  ----------  ----------  ----------  ----------  <br><br>-- ----------  Start Quote<br><br>To make a list with all the numbers from 20 to 1, you can't just do <span>[20..1]</span>, you have to do <span>[20,19..1]</span>.<br><br>-- ----------  End Quote<br><br>
</div>
<div>Why is this? If the first was greater than the second it would just subtract! Right?<br>
</div>
</div>
</blockquote>
<div><br></div>
</div>
<div>[a..b] is syntax sugar for enumFromTo and the definition of that function just doesn't behave in that way. [a, b .. c] is syntax sugar for enumFromThenTo which does. A reason for it to behave like this would be that it's often desired to have the behavior that it does. Consider enumerating every index in a list `xs` except for the first, you could write this as `[1 .. length xs - 1]` with the current syntax, but that sort of thing would yield surprising results if it sometimes went backwards.</div>
<div class="">
<div>&nbsp;</div>
<blockquote class="gmail_quote"><div dir="ltr">
<div>-- ----------  ----------  ----------  ----------  ----------  <br><br>-- ---------- Start Quote<br><br>Watch out when using floating point numbers in ranges! Because they are 
not completely precise (by definition), their use in ranges can yield 
some pretty funky results.<br><br><span><span>ghci&gt;</span><span>&nbsp;[</span><span>0.1</span><span>,&nbsp;</span><span>0.3</span><span>&nbsp;..&nbsp;</span><span>1</span><span>]</span></span><br><span>[<span>0.1</span><span>,</span><span>0.3</span><span>,</span><span>0.5</span><span>,</span><span>0.7</span><span>,</span><span>0.8999999999999999</span><span>,</span><span>1.0999999999999999</span><span>]</span></span><br><br>
</div>-- ---------- End Quote<br><br><div>Can anyone explain me why it works for the first few values, and not "completely"?<br>
</div>
</div></blockquote>
<div><br></div>
</div>
<div>It doesn't "work" for any of the values, it's just an artifact of how they're rendered. 0.1 can't be exactly represented in binary floating point, so the error compounds. Double probably shouldn't be enumerable in the first place, but that's a decision we have to live with. The reason that the end result is so surprising is that&nbsp;1.0999999999999999&nbsp;is less than 1 + 0.1 and for whatever reason the way Enum is defined for Double checks to see if the result is &gt; to+(then-from) rather than &lt;= to.</div>
</div>
</div>
</div></blockquote>
<div><br></div>
<div>To clarify, this is what is happening:</div>
<div><br></div>
<div>
<div>&nbsp; &nbsp; &lambda;&gt; takeWhile (&lt; 1 + 0.1) $ iterate (+0.1) 0.1</div>
<div>&nbsp; &nbsp; [0.1,0.2,0.30000000000000004,0.4,0.5,0.6,0.7,0.7999999999999999,0.8999999999999999,0.9999999999999999,1.0999999999999999]</div>
</div>
</div>
</div>
</div></div>
Gilberto Melfe | 18 Apr 17:12 2014
Picon

Learn You a Haskell! I have a few questions!

Hi there to you all!

I've been reading through the first chapter of "Learn You a Haskell" and I'd like to ask the community a few questions.

Any help would be appreciated...

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

All the standard Prelude functions that throw out an error when fed the []!

head
maximum
...

Are there safe versions anywhere, or do we have to define them ourselves?

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

This one is really important!

I understand that for the definition of product to work as it is:
  product [] must be equal to 1

But what if we want to add a product to something else???
Shouldn't the result be Nothing?
(I guess we would have to guard against this! But we must guard against the parameter of product being the empty list, anyway. Otherwise we risk adding 1 when there is nothing do multiply)

(The same question arises with the functions and and or, and their boolean results, I think! Right?)

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

-- ---------- Start Quote

Names can't be enumerated. What comes after "John"? I don't know.
 
-- ---------- End Quote

"a" to "z" then "aa" to "zz" then "aaa" to "zzz" and so on! Is it to difficult or impossible to create a function that enumerates all possible strings?

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

-- ---------- Start Quote

To make a list with all the numbers from 20 to 1, you can't just do [20..1], you have to do [20,19..1].

-- ---------- End Quote

Why is this? If the first was greater than the second it would just subtract! Right?

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

-- ---------- Start Quote

Watch out when using floating point numbers in ranges! Because they are not completely precise (by definition), their use in ranges can yield some pretty funky results.

ghci> [0.10.3 .. 1]
[0.1,0.3,0.5,0.7,0.8999999999999999,1.0999999999999999]

-- ---------- End Quote

Can anyone explain me why it works for the first few values, and not "completely"?

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

Any thoughts?

Thank You Very Much in advance!

Gilberto
<div><div dir="ltr">
<div>Hi there to you all!<br><br>
</div>
<div>I've been reading through the first chapter of "Learn You a Haskell" and I'd like to ask the community a few questions.<br><br>
</div>
<div>Any help would be appreciated...<br><br>
</div>
<div>-- ----------  ----------  ----------  ----------  ----------  <br><br>
</div>
<div>All the standard Prelude functions that throw out an error when fed the []!<br><br>
</div>
<div>head<br>
</div>
<div>maximum<br>
</div>
<div>...<br><br>
</div>
<div>Are there safe versions anywhere, or do we have to define them ourselves?<br>
</div>
<div>
<br>-- ----------  ----------  ----------  ----------  ----------  <br><br>This one is really important!<br><br>
</div>
<div>I understand that for the definition of product to work as it is:<br>&nbsp; product [] must be equal to 1<br>
</div>
<div><br></div>
<div>But what if we want to add a product to something else???<br>
</div>
<div>Shouldn't the result be Nothing?<br>
</div>
<div>(I guess we would have to guard against this! But we must guard against the parameter of product being the empty list, anyway. Otherwise we risk adding 1 when there is nothing do multiply)<br><br>(The same question arises with the functions and and or, and their boolean results, I think! Right?)<br><br>-- ----------  ----------  ----------  ----------  ----------  <br><br>-- ----------  Start Quote<br><br>Names can't be enumerated. What comes after "John"? I don't know.<br>&nbsp;<br>--  ---------- End Quote<br><br>
</div>
<div>"a" to "z" then "aa" to "zz" then "aaa" to "zzz" and so on! Is it to difficult or impossible to create a function that enumerates all possible strings?<br><br>-- ----------  ----------  ----------  ----------  ----------  <br><br>-- ----------  Start Quote<br><br>To make a list with all the numbers from 20 to 1, you can't just do <span class="">[20..1]</span>, you have to do <span class="">[20,19..1]</span>.<br><br>-- ----------  End Quote<br><br>
</div>
<div>Why is this? If the first was greater than the second it would just subtract! Right?<br><br>-- ----------  ----------  ----------  ----------  ----------  <br><br>-- ---------- Start Quote<br><br>Watch out when using floating point numbers in ranges! Because they are 
not completely precise (by definition), their use in ranges can yield 
some pretty funky results.<br><br><span><span class="">ghci&gt;</span><span>&nbsp;[</span><span class="">0.1</span><span>,&nbsp;</span><span class="">0.3</span><span>&nbsp;..&nbsp;</span><span class="">1</span><span>]</span></span><br><span>[<span class="">0.1</span><span>,</span><span class="">0.3</span><span>,</span><span class="">0.5</span><span>,</span><span class="">0.7</span><span>,</span><span class="">0.8999999999999999</span><span>,</span><span class="">1.0999999999999999</span><span>]</span></span><br><br>
</div>-- ---------- End Quote<br><br><div>Can anyone explain me why it works for the first few values, and not "completely"?<br><br>-- ----------  ----------  ----------  ----------  ----------  <br><br>
</div>
<div>Any thoughts?<br><br>
</div>
<div>Thank You Very Much in advance!<br><br>
</div>
<div>Gilberto<br>
</div>
</div></div>
Ari King | 17 Apr 03:33 2014
Picon

Learning Haskell: Web Project

Hi,

I'm just starting out with Haskell and I thought a good way to learn/practice would be to re-implement a small web app. The web app I plan on re-implementing is made up of a restful (java) api with a angularjs client. The api and client is served by jetty. I'd appreciate suggestions on frameworks and/or tools that make the most sense as I start out.

I was thinking of using scotty + warp + persistence for the api/serving of static content. As for tooling, I was thinking of vim with ghc-mod & syntastic.

Thanks.

-Ari
<div><div dir="ltr">
<div>
<div>Hi,<br><br>
</div>I'm just starting out with Haskell and I thought a good way to learn/practice would be to re-implement a small web app. The web app I plan on re-implementing is made up of a restful (java) api with a angularjs client. The api and client is served by jetty. I'd appreciate suggestions on frameworks and/or tools that make the most sense as I start out. <br><br>I was thinking of using scotty + warp + persistence for the api/serving of static content. As for tooling, I was thinking of vim with ghc-mod &amp; syntastic. <br><br>Thanks.<br><br>
</div>-Ari<br>
</div></div>
Mike Meyer | 14 Apr 21:20 2014

Why getArgs is in the IO monad


From: "John M. Dlugosz" <ngnr63q02 <at> sneakemail.com>
(And that begs the question of why getArgs needs to be monadic in the first place.  It
doesn't change its value; it's a strict constant at run-time, and not knowing it at
compile time is my problem how?)

Actually, the value of the arguments can be changed - at least on some platforms. They are writable from C, if nothing else. What should getArgs do if some ffi changes the arguments before it's called?

Mike

<div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote">
<br>
From: "John M. Dlugosz" &lt;<a href="mailto:ngnr63q02 <at> sneakemail.com">ngnr63q02 <at> sneakemail.com</a>&gt;<br>(And that begs the question of why getArgs needs to be monadic in the first place. &nbsp;It<br>
doesn't change its value; it's a strict constant at run-time, and not knowing it at<br>
compile time is my problem how?)<br>
</blockquote>
<div><br></div>
<div class="gmail_default">Actually, the value of the arguments can be changed - at least on some platforms. They are writable from C, if nothing else. What should getArgs do if some ffi changes the arguments before it's called?</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">Mike</div>
<div class="gmail_default">
<br>
</div>
</div></div></div></div>
Dimitri DeFigueiredo | 14 Apr 08:03 2014
Picon

How do I use Guards in record syntax?


I'm having some trouble understanding where I can or cannot use guards 
inside record syntax. I'm writing a simple conversion routine, but I am 
not able to write it without inserting an extra let. Do I need a let 
expression here? Am I missing something?

--------------
data OldTrade = OldTrade {
     oldprice   :: Double   ,
     oldamount  :: Double   ,
     oldbuysell :: String  -- "buy", "sell" or ""
     } deriving( Eq, Show)

data BuyOrSell = Buy | Sell | Unknown deriving(Eq, Show)

data Trade = Trade {
     price   :: Double   ,
     amount  :: Double   ,
     buysell :: BuyOrSell
     } deriving( Eq, Show)

convert :: OldTrade -> Trade

convert ot  = Trade { price   = oldprice  ot,
                       amount  = oldamount ot,
                       buysell = let x | oldbuysell ot == "buy"  = Buy
                                       | oldbuysell ot == "sell" = Sell
                                       | otherwise               = Unknown
                                 in x
                     }

-- how do I eliminate the 'let' expression here?
-- I wanted to write something like:
--
--                      buysell | oldbuysell ot == "buy"  = Buy
--                              | oldbuysell ot == "sell" = Sell
--                              | otherwise               = Unknown

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

Thanks!

Dimitri

Emmanuel Touzery | 13 Apr 10:21 2014
Picon

parsing exif file: binary, binary-strict, or cereal?

Hello,

 I could not find a pure haskell library to parse EXIF so I wrote one (pretty basic so far):
https://github.com/emmanueltouzery/hsexif

 I wrote it with binary-strict. I also considered binary, but I would like the library not to throw an exception if the file that it's given is not a JPEG or is a JPEG without EXIF, but rather return an Either. I didn't manage to do that with binary (I mean I could offer an IO monad wrapper with try* but to do that even if you give me a lazy bytestring, when it could be done only with pure code, is annoying).

 It annoys me to load the full JPEG in memory just to parse its EXIF though. Then again with binary I'd have to be pretty careful with strictness annotations and so on, although parsing the EXIF is going to be fast and it won't contain that much data. Still, I want the lazy bytestring holding the JPEG file to be released ASAP. It'd be a shame to have it lying in memory, together with holding the JPEG file open until the library caller actually makes use of the EXIF contents...

 And then I realized that binary-strict was last updated in 2010, it seems cereal is recommended for strict binary file parsing nowadays.

 So, what should I do? Leave it as it is? Port to cereal? Port to binary?

 Otherwise I did write this because I'd like to make a program to parse EXIF file which will have to run also on windows. The haskell exif parsing library that I could find uses the C library libexif, which is going to get complicated to get running on windows. Plus it was fun :-)

 Thanks,

Emmanuel
<div><div dir="ltr">
<div>
<div>
<div>
<div>
<div>
<div>Hello,<br><br>
</div>&nbsp;I could not find a pure haskell library to parse EXIF so I wrote one (pretty basic so far):<br><a href="https://github.com/emmanueltouzery/hsexif">https://github.com/emmanueltouzery/hsexif</a><br><br>
</div>&nbsp;I wrote it with binary-strict. I also considered binary, but I would like the library not to throw an exception if the file that it's given is not a JPEG or is a JPEG without EXIF, but rather return an Either. I didn't manage to do that with binary (I mean I could offer an IO monad wrapper with try* but to do that even if you give me a lazy bytestring, when it could be done only with pure code, is annoying).<br><br>
</div>&nbsp;It annoys me to load the full JPEG in memory just to parse its EXIF though. Then again with binary I'd have to be pretty careful with strictness annotations and so on, although parsing the EXIF is going to be fast and it won't contain that much data. Still, I want the lazy bytestring holding the JPEG file to be released ASAP. It'd be a shame to have it lying in memory, together with holding the JPEG file open until the library caller actually makes use of the EXIF contents...<br><br>
</div>&nbsp;And then I realized that binary-strict was last updated in 2010, it seems cereal is recommended for strict binary file parsing nowadays.<br><br>&nbsp;So, what should I do? Leave it as it is? Port to cereal? Port to binary?<br><br>
</div>&nbsp;Otherwise I did write this because I'd like to make a program to parse EXIF file which will have to run also on windows. The haskell exif parsing library that I could find uses the C library libexif, which is going to get complicated to get running on windows. Plus it was fun :-)<br><br>
</div>
<div>&nbsp;Thanks,<br>
</div>
<div><br></div>Emmanuel<br>
</div></div>
John M. Dlugosz | 12 Apr 15:29 2014
Picon

general structuring of "foreach" logic in IO

This works:

main = do
     myargs <- getArgs
     mapM_ (\s -> putStrLn s ) myargs

and imagine that the "body" will be substantial rather than just a putStrLn.  My gut 
instinct is that the code ought to be arranged as:

	<any needed keywords or punctuation> and <the collection of items>
	<body to perform for every element
		...
		...
		>

Meanwhile, there is no need to name the result of getArgs into myargs.

So, getArgs is of type IO [String], and I want to apply that in the manner of a list. 
Without the Monad wrappers, plain
	map ( blah ) strings
could be ( blah ) <$> strings, and in this particular case I don't see a reversed-arg 
version, although there is one for <*> (as <**≥).  But, for monad stuff in general there 
are reversed arrows for (most?) everything, and that's where I'm heading.

So the first question is, how do I do the equivalent map-as-nondeterministic-apply when 
the strings is further wrapped in IO, as is the function being applied.

	getArgs >>= mapM_ (\s -> putStrLn s )

does double-duty of moving the argument from last place to the left, as it makes use of 
eta reduction.  Because I have two things going on (list applicative and IO monad) I'm 
losing the slickness of using applicative syntax.  Is there a nice way to make these work 
together?

And more generally, how would you write such a construct?  I'm naturally biased with my 
knowledge in other languages, so maybe there's a completely different "normal" way of 
approaching this?

Thanks,
-John

ke dou | 11 Apr 18:50 2014

Convert my own types to the Haskell types using typeclass

Hi,

I want to define a typeclass that can convert my own types like MyBool, MyInt, MyOption to according Haskell types -- Bool, Int, Maybe.

Currently I can convert the first two types, but for MyOption, I don't how to do, since it is a polymophic type.

Here is my toy program:
--------------------------------------------------------------
{-# LANGUAGE TypeFamilies #-}

module Coercible where

import qualified Prelude

data MyBool = MyTrue | MyFalse
data MyInt = Zero | One | Two
data MyOption a =
   Some a
 | None

class Coercible a where
  type Return a :: *
  toHaskell :: a -> (Return a)

instance Coercible MyBool where
  type Return MyBool = Prelude.Bool
  toHaskell MyTrue = Prelude.True
  toHaskell MyFalse = Prelude.False

instance Coercible MyInt where
  type Return MyInt = Prelude.Int
  toHaskell Zero = 0
  toHaskell One = 1
  toHaskell Two = 2

instance Coercible (MyOption a) where
  type Return (MyOption a) = Prelude.Maybe a
  toHaskell (Some a) = Prelude.Just a
  toHaskell None = Prelude.Nothing
--------------------------------------------------------------------------
The problem occurs when I am trying to use "toHaskell (Some MyBool)", the error message is "Not in scope: data constructor `MyBool'".
Any hints will be appreciated !!

Best,
--Ke
<div><div dir="ltr">Hi,<div><br></div>
<div>I want to define a typeclass that can convert&nbsp;my own types like MyBool, MyInt, MyOption to according Haskell types -- Bool, Int, Maybe.</div>
<div><br></div>
<div>Currently I can convert the first two types, but for MyOption, I don't how to do, since it is a polymophic type.</div>
<div><br></div>
<div>Here is my toy program:</div>
<div>--------------------------------------------------------------</div>
<div>{-# LANGUAGE TypeFamilies #-}<div><br></div>
<div>module Coercible where</div>
<div><br></div>
<div>
import qualified Prelude</div>
<div><br></div>
<div>data MyBool = MyTrue | MyFalse</div>
<div>data MyInt = Zero | One | Two</div>
<div>data MyOption a =</div>
<div>&nbsp; &nbsp;Some a</div>
<div>&nbsp;| None</div>
<div><br></div>
<div>class Coercible a where</div>
<div>&nbsp; type Return a :: *</div>
<div>&nbsp; toHaskell :: a -&gt; (Return a)</div>
<div><br></div>
<div>instance Coercible MyBool where</div>
<div>&nbsp; type Return MyBool = Prelude.Bool</div>
<div>&nbsp; toHaskell MyTrue = Prelude.True</div>
<div>&nbsp; toHaskell MyFalse = Prelude.False</div>
<div><br></div>
<div>instance Coercible MyInt where</div>
<div>&nbsp; type Return MyInt = Prelude.Int</div>
<div>&nbsp; toHaskell Zero = 0</div>
<div>&nbsp; toHaskell One = 1</div>
<div>&nbsp; toHaskell Two = 2</div>
<div><br></div>
<div>
<div>instance Coercible (MyOption a) where</div>
<div>&nbsp; type Return (MyOption a) = Prelude.Maybe a</div>
<div>&nbsp; toHaskell (Some a) = Prelude.Just a</div>
<div>&nbsp; toHaskell None = Prelude.Nothing</div>
</div>
<div>--------------------------------------------------------------------------</div>
<div>The problem occurs when I am trying to use "toHaskell (Some MyBool)", the error message is "Not in scope: data constructor `MyBool'".</div>
<div>
<div>Any hints will be appreciated !!</div>
<div><br></div>
<div>
Best,<br clear="all"><div>--Ke</div>
</div>
</div>
</div>
</div></div>
John M. Dlugosz | 11 Apr 18:25 2014
Picon

Windows x64 ?

The Haskell Platform has a single download link for "Windows" which seems to be 32-bit 
only.  But GHC is available for Win x64.  Their site dissuades downloading their 
installation and recommends getting Haskell Platform instead.  So, is there an easy way to 
get 64-bit versions of compiler or compiled executables so produced?

John M. Dlugosz | 11 Apr 02:12 2014
Picon

recursive 'let' ?

I understand that the definitions introduced by 'let' can be recursive, even mutually 
recursive among several names.  Why would you want to do that?  I saw contrived examples, 
and wonder why the authors never show a realistic example.

	let b = f a c
	    c = f a b
	in ...

I see that makes sense in light of lazy evaluation: b is really an alias for a (recursive) 
function, not a value that needs to find fixed points.

Is this used for common idioms and problem-solving approaches in Haskell?

--John

Stijn Muylle | 10 Apr 09:48 2014
Picon

Guards problem


All,

I ran into a small problem using guards, and I don't really understand what I'm doing wrong.

I have this code: 

----------------------------------
import Data.List.Split
type Cell = Bool
data Board = Board [[Cell]] deriving Show

trueList = True : trueList

createBoard :: Int -> Int -> Board
createBoard x y = Board (chunksOf x (take (x * y) trueList))

getCellAt :: Int -> Int -> Board -> Cell
getCellAt x y (Board b)
        | x >= (length b) =  False
        | y >= (length (b !! 0)) =  False
        | otherwise     =  (b !! x) !! y

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

When I try to execute this:

getCellAt (-1) 0 $ createBoard 3 3

I get a negative index exception. However, as -1 is smaller than 3 (length of the board), I would expect False to be returned.

Am I overlooking something?

Thanks!
Stijn
<div><div dir="ltr">
<br><div>All,</div>
<div><br></div>
<div>I ran into a small problem using guards, and I don't really understand what I'm doing wrong.</div>
<div><br></div>
<div>I have this code:&nbsp;</div>
<div><br></div>
<div>----------------------------------</div>
<div>
<div>import Data.List.Split</div>
<div>type Cell = Bool</div>
<div>data Board = Board [[Cell]] deriving Show</div>
<div><br></div>
<div>trueList = True : trueList<br>
</div>
<div><br></div>
<div>createBoard :: Int -&gt; Int -&gt; Board</div>
<div>createBoard x y = Board (chunksOf x (take (x * y) trueList))</div>
<div><br></div>
<div>getCellAt :: Int -&gt; Int -&gt; Board -&gt; Cell</div>
<div>
getCellAt x y (Board b)</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; | x &gt;= (length b) = &nbsp;False</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; | y &gt;= (length (b !! 0)) = &nbsp;False</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; | otherwise &nbsp; &nbsp; = &nbsp;(b !! x) !! y</div>
<div><br></div>
<div>-----------------------------------</div>
<div><br></div>
<div>When I try to execute this:</div>
<div><br></div>
<div>getCellAt (-1) 0 $ createBoard 3 3</div>
<div><br></div>
<div>I get a negative index exception. However, as -1 is smaller than 3 (length of the board), I would expect False to be returned.</div>
<div><br></div>
<div>Am I overlooking something?</div>
<div><br></div>
<div>Thanks!</div>
<div>Stijn</div>
</div>
</div></div>

Gmane