Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/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:  How to "add column" to a Table? (Michael Orlitzky)
   2. Re:  IO and purity (Ertugrul S?ylemez)
   3. Re:  :  GCJ 2015 - Round 1A Problem - Haircut (Gregory Guthrie)
   4.  Partition a number or optimize (Animesh Saxena)
   5. Re:  How to "add column" to a Table? (martin)
   6.  fmap Maybe (Shishir Srivastava)


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

Message: 1
Date: Sun, 26 Apr 2015 15:46:23 -0400
From: Michael Orlitzky <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] How to "add column" to a Table?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

On 04/26/2015 06:13 AM, martin wrote:
> Hello all,
> 
> I suppose the natural representation for a Table is a List of Tuples. Or more 
> generally I'll probably end up with a type
> "Table a" where a stands for the Columns.
> 
> I cannot figure out how to add a new Column to a Table. What would be the 
> type of such an operation? If I just want to
> add an empty Column (which what an RDBMS would do), would I do something like 
> this?
> 
> addColumn :: ColumnDef -> Table a -> Table b
> 
> But what is this ColumnDef? It would need to contain the name and type of the 
> new column. To implement this operation I
> would have to somewehre add a column to something. But what is the type of 
> this something. I don't see how I can add a
> component to a Tuple in a generic way, or can I?
> 

type Person = ( Int,    -- ID
                String, -- Name
                Int,    -- Age
                Bool    -- Evil bit
              )

type Place = ( Int,    -- ID
               String, -- Name
               Double, -- Longitude
               Double, -- Latitude
               Double  -- Elevation
             )

data Column = Person | Place

type Table = [Column]

addColumn :: Table -> Column -> Table
addColumn table col = table ++ [col]



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

Message: 2
Date: Mon, 27 Apr 2015 03:25:36 +0200
From: Ertugrul S?ylemez <[email protected]>
To: Shishir Srivastava <[email protected]>, beginners
        <[email protected]>
Subject: Re: [Haskell-beginners] IO and purity
Message-ID: <[email protected]_W_723V_1_36_000>
Content-Type: text/plain; charset="us-ascii"

> Can someone please explain how IO operations do not fit in the pure
> category of mathematical function in that they have to be implemented
> via Monads.

Let's not talk about monads at all.  Instead allow me to eliminate a
major misconception right away: I/O does in fact fit into the model of
purity.

The first major insight occurs when you stop thinking about getLine as a
function or an "impure value".  What it really is is just a program.
Every value of type `IO A` is in fact a program that produces a value of
type `A`.

The second major insight happens when you realise that the equal sign in
Haskell introduces an actual equation:

    line = getLine

Here `line` is not a string either.  You have just expressed that `line`
is the exact same thing as `getLine`, so it is a program that, when run,
produces a string.  Now realise that `main` is also a program, the one
that is the actual program you are writing.  When

    putStrLn "Hello!"

is a program that prints a string and results in the dummy value `()`
and you say that

    main = putStrLn "Hello!"

then the program you are writing is that same program, because you have
just defined it to be (remember: equation, not assignment!).

Now you have a bunch of programs.  The final ingredient to writing
side-effecting programs in Haskell is a way to combine those programs.
If `p1` and `p2` are two programs, then `p1 >> p2` is the program that
first executes `p1` and then executes `p2`.  Its result is the result of
`p2`, the latter argument of `(>>)`:

    main = putStrLn "Hello" >> putStrLn "world!"

You have just defined `main` (i.e. your program) to be the program that
first prints the "Hello" line, then prints the "world!" line.  A more
powerful operator is `(>>=)`, which is best explained in terms of an
example:

    putStrLn :: String -> IO ()

Now this is an actual function.  It takes a string and returns a
program, the one that prints that string.  Remember that functions are
the things that can be applied.  Not everything is a function, and in
particular programs are *not* functions, a common and dangerous
misconception.  You could just pass it a string, but what if you want to
print whatever `getLine` resulted in?

    (>>=) :: IO a -> (a -> IO b) -> IO b

Verify that the first argument fits the type of `getLine`, the second
one fits the type of `putStrLn`.  Here is the specialised version, where
`a = String` and `b = ()`:

    (>>=) :: IO String -> (String -> IO ()) -> IO ()

Simply apply it to the desired program (`getLine`) and the desired
program-producing function (`putStrLn`) to get a composite program:

    main = getLine >>= putStrLn

Now let's talk about monads.  It is a nice incident that the `(>>=)`
operator follows a nice algebraic structure (monads), just like addition
follows a nice algebraic structure (monoids).  And it is not the only
operator that follows this structure.  What we can do now is to write
extremely generic functions that work for all monads with a single
implementation.

Welcome to the world of composable polymorphism.  This is the main
reason why Haskell code tends to be much shorter than code in other
languages.  Functions like `mapM` and `forever` are not IO-specific.
They work for all monads, yet they have only a single implementation.

So yes, you are right.  Monads are not at all necessary.  But they are
useful, because we get so much for free.  Basically when you implement a
control functions, chances are that you can actually apply it to a wide
variety of structures, including `IO`.

Exercise:  Can you think of an operation that would work for all
monoids?  If you can, you can implement it *in its general form* in
Haskell.  In other words, once you have established that some type
denotes a monoid, that operation will be available for it for free.
Same thing with monads.


> For e.g. the getLine function has the type IOString and it reads the input
> from the user. Now as I see it the output of getLine will always be same if
> the input remain same (i.e. for input "X" getLine will always return "X" )
> which is the constraint on mathematical functions.
>
> Therefore I don't see why monads are necessary for implementing IO in pure
> languages.
>
> I can understand why Date and Random functions have be implemented via
> monads because their output will always change.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 472 bytes
Desc: not available
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150427/a3824780/attachment-0001.sig>

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

Message: 3
Date: Sun, 26 Apr 2015 21:38:37 -0500
From: Gregory Guthrie <[email protected]>
To: "[email protected]" <[email protected]>
Subject: Re: [Haskell-beginners] :  GCJ 2015 - Round 1A Problem -
        Haircut
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Seems to me that an estimate won't help, one has to know exactly how much to 
skip over.

I did this:
   Find the lowest common multiple (lcm), and then from that the shortest cycle 
of services,
   And then skip any multiple of those from the customer queue.

sim :: Problem -> [Result]
sim (n, ts) = serve tSkip (servers ts)
    --  optimize; skip ahead over server cycles...
    where tsLcm  = (foldl lcm $ head ts) $ tail ts
          tCycle = sum $ map (div tsLcm) ts
          tSkip  = n - (div n tCycle)*tCycle

(Some fixup is needed for tSkip=0, one easy one is to force an extra cycle.)
> ----------------------------------------------------------------------
> Message: 1
> Date: Sat, 18 Apr 2015 10:29:11 -0400
> From: Doug McIlroy <[email protected]>
> To: [email protected]
> Cc: [email protected]
> Subject: Re: [Haskell-beginners] GCJ 2015 - Round 1A Problem - Haircut
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset=us-ascii
> 
> > Can someone tell me how I could have avoided or fixed this?
> 
> The trouble manifested as stack overflow on solving
> 
> > https://code.google.com/codejam/contest/4224486/dashboard#s=p1
> 
> For efficiency, you'll probably need more cleverness in math than in Haskell. 
> You can predict
> an approximate start time for the Nth customer's service from the average 
> per-customer
> service time M, where 1/M = sum 1/M_k.
> Starting from that estimate, one can skip over almost the entire service 
> simulation.
> 
> Doug McIlroy
> 
> 


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

Message: 4
Date: Mon, 27 Apr 2015 13:18:34 +0800
From: Animesh Saxena <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Partition a number or optimize
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

I have the following function to optimize

myfunction = max(p `div` c + p `mod` c)
Here p is an array of numbers
and c is also an array of numbers

I need to find c?s which minimize the value of my function. for example if p = 
[1,10000]. c = 2,8 or c = 1,9 or c=5,5
number of c is length of p which is 2 (in this case). Sum of c is given as a 
constraint, so for example if sum is 10, then c?s can be 2,8 or 1,9 or 5,5 or 
4,6 or?..etc etc. Length of c will be equal to length of p array. Hope the 
problem statement is clear

In haskell terms it will be,

let my_min  p c = foldl1 max $ zipWith (\p c -> (p `div` c) + (p `mod` c)) p c
One way is to use various combinations of c using integer partitioning.

Now i wrote a crude parts function to do this

nparts 0 = []
nparts n = [n] : [x:xs | x <- [1..n`div`2], xs <- nparts(n - x)]

which is a bad bad example of recursion coz it?s slow!

*Main> nparts 5
[[5],[1,4],[1,1,3],[1,1,1,2],[1,1,1,1,1],[1,2,2],[1,2,1,1],[2,3],[2,1,2],[2,1,1,1]]

I can then filter this with length of p and get the required options of c

I can then call the function ? my_min? with this list (simple map application)

Problem is if i do 
*Main> filter (\x->length x == 2) $ nparts 100
[[1,99]

and it hangs?for a lot of time?.so time constraint?

Any other approaches to this problem to make the partitions speedier. I know 
it?s an optimization problem?but all non linear optizers will use floats?and 
fail to converge?this is an integer optimizing problem.

Any ideas??



-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150427/53f157cd/attachment-0001.html>

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

Message: 5
Date: Mon, 27 Apr 2015 08:46:56 +0200
From: martin <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How to "add column" to a Table?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252

Am 04/26/2015 um 09:46 PM schrieb Michael Orlitzky:
> On 04/26/2015 06:13 AM, martin wrote:
>> Hello all,
>>
>> I suppose the natural representation for a Table is a List of Tuples. Or 
>> more generally I'll probably end up with a type
>> "Table a" where a stands for the Columns.
>>
>> I cannot figure out how to add a new Column to a Table. What would be the 
>> type of such an operation? If I just want to
>> add an empty Column (which what an RDBMS would do), would I do something 
>> like this?
>>
>> addColumn :: ColumnDef -> Table a -> Table b
>>
>> But what is this ColumnDef? It would need to contain the name and type of 
>> the new column. To implement this operation I
>> would have to somewehre add a column to something. But what is the type of 
>> this something. I don't see how I can add a
>> component to a Tuple in a generic way, or can I?
>>
> 
> type Person = ( Int,    -- ID
>                 String, -- Name
>                 Int,    -- Age
>                 Bool    -- Evil bit
>               )
> 
> type Place = ( Int,    -- ID
>                String, -- Name
>                Double, -- Longitude
>                Double, -- Latitude
>                Double  -- Elevation
>              )
> 
> data Column = Person | Place
> 
> type Table = [Column]
> 
> addColumn :: Table -> Column -> Table
> addColumn table col = table ++ [col]

Aren't you adding a *row* to a Table which allows rows of multiple shapes? What 
I am looking for is an operation which
adds a *column* to a regular table, i.e. one where all rows have the same shape.

But I like your idea of adding an "Evil bit" to Persons.



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

Message: 6
Date: Mon, 27 Apr 2015 11:02:01 +0100
From: Shishir Srivastava <[email protected]>
To: beginners <[email protected]>
Subject: [Haskell-beginners] fmap Maybe
Message-ID:
        <CALe5RTssXwpoJ=o1=g-sdfmyadzps-cuthnfayhtba6z1ej...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi,

Please can someone explain why these two expression behave differently -

----
fmap (\x -> x) Just 2
Just 2

-----
fmap (\x -> x+2) Just 2

*No instance for (Num (Maybe a0)) arising from a use of `it'*
*In a stmt of an interactive GHCi command: print it*

----
The first expression evaluates fine whereas the second one fails. However
if I do -
----

fmap (\x -> x+2) $ Just 2
Just 4
----

Then the second expression also returns the Maybe value. Why is $ needed in
second expression but not in the first one ?

Thanks,
Shishir
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150427/d79e46f6/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 82, Issue 39
*****************************************

Reply via email to