Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Fundamentals of List Comprehension and Recursion (Cody Haberkorn)
   2.  ghc-pkg (Gregory Guthrie)
   3. Re:  ghc-pkg (Brent Yorgey)
   4. Re:  Fundamentals of List Comprehension and Recursion
      (Brent Yorgey)
   5. Re:  suggestions for optimizing sudoku solver (KC)


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

Message: 1
Date: Mon, 2 Jan 2012 21:07:35 -0600
From: Cody Haberkorn <cody.haberk...@gmail.com>
Subject: [Haskell-beginners] Fundamentals of List Comprehension and
        Recursion
To: beginners@haskell.org
Message-ID:
        <cabsdo8ewspzymuwil5ov77vd3v6qvq8hfjyr+qn0gpotweg...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi all,

I high school programmer and just started learning Haskell. Haskell so far
seems to be an awesome language. I began reading "Learn you Haskell for a
Great Good!" I am confused with list comprehension, as I am coming from an
imperative programming language (Java). From my understanding,
list comprehension is synonymous to Java's for-loop and what not.

For example:

let xs = [1,2,3,4,5]
[x | x <- xs]

I believe it is saying that "bound x to xs, and do (print) x."

Another example:

let rightTriangles' = [ (a,b,c) | c <- [1..10] , b <- [1.. c], a <- [1..
b], a^2 + b^2 == c^2, a+b+c == 24]
ghci > rightTriangles'
[(6 ,8 ,10)]

I believe it is saying "store c with the numbers 1-10, b is length of c,
and a is the length of b. Print the answer (a,b,c) which satisfies a^2 +
b^2 == c^2, a+b+c == 24.
It may seem that I am answering my own question, but when it becomes more
complicated or I am ask to do specific tasks that involves list
comprehensions, I do not know how to read or compose one.

Second, can someone do a line by line breakdown of this code? The book does
a sub-par job explaining it.
-- Creating our own sum function
sum ' :: (Num a) => [a] -> a
sum ' [] = 0
sum ' (x:xs) = x + sum ' xs

Summed up: How did you learn Haskell and how long did it take before you
became competent? This is my second day and I am already running into
problems. I have to re-read things where as Java it came easy to me. Any
book or tutorials? Can someone explain list comprehension and my recursion
code?

Thanks,
- Cody

p.s The questions so far have been over my head (is this a " true
beginner's email list"?), I'll try to answer what I can.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120102/5dba6db5/attachment-0001.htm>

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

Message: 2
Date: Mon, 2 Jan 2012 21:36:39 -0600
From: Gregory Guthrie <guth...@mum.edu>
Subject: [Haskell-beginners] ghc-pkg
To: "beginners@haskell.org" <beginners@haskell.org>
Message-ID:
        <08ef9da445c4b5439c4733e1f35705ba019bb8b4c...@mail.cs.mum.edu>
Content-Type: text/plain; charset="us-ascii"

What is the remedy (is there a simple remedy?)  if "ghc-pkg check" reports 
something amiss?

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

C:\Users>ghc-pkg check
There are problems in package hoogle-4.2.6:
  dependency "blaze-builder-0.3.0.1-157e8a92bc5ef27895f938613ecfdcb1" doesn't 
exist
There are problems in package warp-0.4.3:
  dependency "blaze-builder-0.3.0.1-157e8a92bc5ef27895f938613ecfdcb1" doesn't 
exist
There are problems in package wai-0.4.1:
  dependency "blaze-builder-0.3.0.1-157e8a92bc5ef27895f938613ecfdcb1" doesn't 
exist
There are problems in package http-types-0.6.5.1:
  dependency "blaze-builder-0.3.0.1-157e8a92bc5ef27895f938613ecfdcb1" doesn't 
exist
There are problems in package case-insensitive-0.3.0.1:
  dependency "hashable-1.1.2.1-25dfe4e720b096d06d0cf57f76c49caf" doesn't exist
There are problems in package blaze-builder-enumerator-0.2.0.2:
  dependency "blaze-builder-0.3.0.1-157e8a92bc5ef27895f938613ecfdcb1" doesn't 
exist
There are problems in package scion-0.1.0.9:
  dependency "AttoJson-0.5.10-4932e72997280d8c2e35b4aca3328284" doesn't exist

The following packages are broken, either because they have a problem
listed above, or because they depend on a broken package.
hoogle-4.2.6
warp-0.4.3
wai-0.4.1
http-types-0.6.5.1
case-insensitive-0.3.0.1
blaze-builder-enumerator-0.2.0.2
scion-0.1.0.9
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120102/ed30a4b6/attachment-0001.htm>

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

Message: 3
Date: Mon, 2 Jan 2012 22:45:20 -0500
From: Brent Yorgey <byor...@seas.upenn.edu>
Subject: Re: [Haskell-beginners] ghc-pkg
To: beginners@haskell.org
Message-ID: <20120103034520.ga1...@seas.upenn.edu>
Content-Type: text/plain; charset=us-ascii

In this case, the simple remedy is to reinstall the packages which the
error messages say do not exist (blaze-builder, hashable, AttoJson)
and then reinstall any packages you want to use which depend on them.

-Brent

On Mon, Jan 02, 2012 at 09:36:39PM -0600, Gregory Guthrie wrote:
> What is the remedy (is there a simple remedy?)  if "ghc-pkg check" reports 
> something amiss?
> 
> -------------------------------------------
> 
> C:\Users>ghc-pkg check
> There are problems in package hoogle-4.2.6:
>   dependency "blaze-builder-0.3.0.1-157e8a92bc5ef27895f938613ecfdcb1" doesn't 
> exist
> There are problems in package warp-0.4.3:
>   dependency "blaze-builder-0.3.0.1-157e8a92bc5ef27895f938613ecfdcb1" doesn't 
> exist
> There are problems in package wai-0.4.1:
>   dependency "blaze-builder-0.3.0.1-157e8a92bc5ef27895f938613ecfdcb1" doesn't 
> exist
> There are problems in package http-types-0.6.5.1:
>   dependency "blaze-builder-0.3.0.1-157e8a92bc5ef27895f938613ecfdcb1" doesn't 
> exist
> There are problems in package case-insensitive-0.3.0.1:
>   dependency "hashable-1.1.2.1-25dfe4e720b096d06d0cf57f76c49caf" doesn't exist
> There are problems in package blaze-builder-enumerator-0.2.0.2:
>   dependency "blaze-builder-0.3.0.1-157e8a92bc5ef27895f938613ecfdcb1" doesn't 
> exist
> There are problems in package scion-0.1.0.9:
>   dependency "AttoJson-0.5.10-4932e72997280d8c2e35b4aca3328284" doesn't exist
> 
> The following packages are broken, either because they have a problem
> listed above, or because they depend on a broken package.
> hoogle-4.2.6
> warp-0.4.3
> wai-0.4.1
> http-types-0.6.5.1
> case-insensitive-0.3.0.1
> blaze-builder-enumerator-0.2.0.2
> scion-0.1.0.9

> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners




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

Message: 4
Date: Mon, 2 Jan 2012 23:05:17 -0500
From: Brent Yorgey <byor...@seas.upenn.edu>
Subject: Re: [Haskell-beginners] Fundamentals of List Comprehension
        and Recursion
To: beginners@haskell.org
Message-ID: <20120103040517.gb1...@seas.upenn.edu>
Content-Type: text/plain; charset=us-ascii

On Mon, Jan 02, 2012 at 09:07:35PM -0600, Cody Haberkorn wrote:
> Hi all,

Hi Cody, welcome!

> I high school programmer and just started learning Haskell. Haskell so far
> seems to be an awesome language. I began reading "Learn you Haskell for a
> Great Good!" I am confused with list comprehension, as I am coming from an
> imperative programming language (Java). From my understanding,
> list comprehension is synonymous to Java's for-loop and what not.

I can tell you are coming from an imperative background, because all
your explanations below are phrased in terms of what the code *does*.
Forget this way of thinking when learning Haskell!  Haskell code does
not *do* things; it *evaluates to* some value, in the same sense that
a mathematical expression like (3 + 4) * 6 evaluates ("simplifies") to
72.

> 
> For example:
> 
> let xs = [1,2,3,4,5]
> [x | x <- xs]
> 
> I believe it is saying that "bound x to xs, and do (print) x."

No, this code does not do anything, and it certainly does not print.
[x | x <- xs] evaluates to the list made up of the values 'x', where
'x' ranges over all the values in the list 'xs'.  That is, 
[x | x <- xs] is exactly equivalent to xs.

If you type this expression at the ghci command line, it will cause
the value of this list to be printed. But there is nothing inherent in
the above code to do with printing, just as there is nothing inherent
in the number '72' to do with pencil lead, although you may happen to
write down 72 using your pencil. (If you type 72 at the ghci prompt it
will also cause 72 to be printed: I hope you will agree that 72 does
not mean 'print 72', it just means 72!)

> Another example:
> 
> let rightTriangles' = [ (a,b,c) | c <- [1..10] , b <- [1.. c], a <- [1..
> b], a^2 + b^2 == c^2, a+b+c == 24]
> ghci > rightTriangles'
> [(6 ,8 ,10)]
> 
> I believe it is saying "store c with the numbers 1-10, b is length of c,
> and a is the length of b. Print the answer (a,b,c) which satisfies a^2 +
> b^2 == c^2, a+b+c == 24.

Again, there is no storing going on.  Also, b is not the length of c;
instead b will take on each of the numbers from 1 to c. This says 

  * let c refer to each of the numbers from 1 to 10 in turn
  * for each value of c, let b refer to each of the numbers from 1 to
    c in turn
  * for each value of b, let a refer to each of the numbers from 1 to
    b in turn
  * keep only those combinations where a^2 + b^2 == c^2 and a+b+c ==
    24
  * make a list out of triples (a,b,c) of the remaining combinations.

> -- Creating our own sum function
> sum ' :: (Num a) => [a] -> a

  This first line says: sum' is a function which works for any type a,
  as long as a is an instance of the Num type class (that is, it is a
  type which supports arithmetic operations, such as Integer or
  Double).  It takes a single input -- a list whose elements are of
  type a -- and produces a single output -- a value of type a.

  (By the way, you have put a space in between sum and the single quote
  ', but there should be no space, like sum' -- it is just a name;
  unlike Java, Haskell names allow single quotes as long as they are
  not at the beginning.)

> sum ' [] = 0

  If the input to sum' is the empty list, the output of sum' is 0.

> sum ' (x:xs) = x + sum ' xs

  If the input to sum' is a list whose first element is called x and
  whose remainder is called xs, the output of sum' is x plus the
  result of a recursive call to sum' with the input xs.

> Summed up: How did you learn Haskell and how long did it take before you
> became competent? This is my second day and I am already running into
> problems. I have to re-read things where as Java it came easy to me. Any
> book or tutorials? Can someone explain list comprehension and my recursion
> code?

It took me many months to learn Haskell.  I don't remember exactly how
I learned it, but it was a combination of reading tutorials, reading
and trying to understand lots of other people's code, and working on
my own projects.  If you are only on your second day, don't despair!
Your confusion and problems are quite natural.  Keep pushing through
them (and keep asking questions) and you will learn a lot.  The best
things in life do not come easily.

-Brent



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

Message: 5
Date: Mon, 2 Jan 2012 21:41:00 -0800
From: KC <kc1...@gmail.com>
Subject: Re: [Haskell-beginners] suggestions for optimizing sudoku
        solver
To: peter.h...@memorphic.com, beginners@haskell.org
Message-ID:
        <CAMLKXymFnkN-25YxTZwuGM1FDyMqQzyEH9W-mzVe3b=uejx...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

>From "Pearls of Functional Algorithm Design", Richard Bird, 2010

"Assuming about a half of the 81 entries are fixed initially (a
generous estimate), there are about 9^40 grids to check."

The main value of the book is that Bird shows how to design by
calculation (how equational reasoning) can be used to solve problems
and then the efficiency of the solution can be improved.



On Sun, Jan 1, 2012 at 7:27 PM, Peter Hall <peter.h...@memorphic.com> wrote:
> I set myself a learning task of writing a sudoku solver.
> (https://github.com/peterjoel/sudoku/blob/master/src/Sudoku.hs)
> It works pretty well, but struggles with some of the harder grids.
> e.g. data/hard4.txt and data/hard5.txt take around 18 seconds to
> solve. Obviously there's a limit, but I feel like I should be able to
> make this faster.
>
> I think the main issue is reading/writing to the cells in the grid,
> since (!!) is O(n). Even though it never has to go beyond index 8, it
> will add up over the millions of times it has to do it. I imagine it
> could be a lot faster if I use a static, non-list data structure, but
> I was hoping to keep it a bit more flexible.
>
> Also, I'm struggling to get started with performance profiling. Can
> someone point me to some good resources?
>
> Peter
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



-- 
--
Regards,
KC



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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 43, Issue 4
****************************************

Reply via email to