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: Re: question about show -- RWH chapter 5 (Daniel Fischer)
2. Re: question about styles of recursion (Brent Yorgey)
3. Re: question about styles of recursion (7stud)
4. Re: question about styles of recursion (7stud)
5. Re: Re: question about styles of recursion (Daniel Fischer)
6. Re: Re: question about styles of recursion (Michael Mossey)
7. Re: question about styles of recursion (7stud)
8. Re: Re: question about styles of recursion (Daniel Fischer)
9. Simple laplace solver (Edward Z. Yang)
----------------------------------------------------------------------
Message: 1
Date: Thu, 26 Mar 2009 17:52:48 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Re: question about show -- RWH
chapter 5
To: [email protected]
Cc: 7stud <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Donnerstag 26 März 2009 17:20:46 schrieb 7stud:
> Daniel Fischer <daniel.is.fischer <at> web.de> writes:
> > > $ ghc -o simple Main.hs PutJSON.hs SimpleJSON.hs
> > > /usr/libexec/gcc/i686-apple-darwin8/4.0.1/ld: warning -F:
directory
> >
> > name
> >
> > > (/Users/me/Library/Frameworks) does not exist
> > >
> > > $ simple
> > > hello
> > >
> > > Also can anyone tell me why I always get that warning?
> >
> > Does the directory exist? If it exists, is in in the linker path?
>
> No it doesn't exist. There is a directory:
>
> /Library/Frameworks
That explains the warning, though not why ld is looking for that
directory. Since I don't know Macs, I can only guess.
Maybe the fact that you untarred ghc to
/Users/me/my_tar_extractions/ghc-6.8.2 confused it as it expected to
be untarred to /usr/local/ghc-6.8.2 and find the frameworks in
../../Library/Frameworks.
You can try symlinking /Library to /Users/me and see if that gets rid of
the warning, though for a real fix, a mac-user's answer would be
required.
>
> This is what I did to install ghc:
>
> 1) I downloaded GMP.framework and GNUreadline.framework, which
my mac
> automatically unzipped and placed on my desktop. I then dragged
the
> resulting two folders into /Library/Frameworks as per the instructions
at:
>
> http://www.haskell.org/ghc/download_ghc_682.html#macosxintel
>
> 2) I downloaded ghc-6.8.2
>
> 3) I unzipped an untared into /Users/me/my_tar_extractions
>
> 4) I cd'ed into the newly created ghc-6.8.2 folder.
>
> 5) I read the INSTALL document in the ghc-6.8.2 folder.
>
> 6) I ran the command:
>
> $ ./configure
>
> 7) Then I ran the command:
>
> $ sudo make install
>
> 8) At the end of the install output, I got a message that said:
> -------------
> Installation of ghc-6.8.2 was successful.
>
> To use, add /usr/local/bin to your PATH.
>
> Warning: this binary distribution does NOT contain documentation!
> --------------
>
> 9) I appended /usr/local/bin onto the PATH in ~/.bash_profile.
>
>
> After unzipping and untaring ghc, should I have put the
> ghc-6.8.2 folder in /Library/Frameworks like I did with
> GMP.framework and GNUreadline.framework? Can I still do that?
>
------------------------------
Message: 2
Date: Thu, 26 Mar 2009 14:09:00 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] question about styles of recursion
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Thu, Mar 26, 2009 at 09:19:18AM -0700, Michael Mossey wrote:
>
> myAvg' :: Int -> [Int] -> [ Double ] -> Double
> myAvg' sum count [] = sum / fromIntegral count
> myAvg' sum count (x:xs) = myAvg' (x + sum) (n + 1) xs
The definition of myAvg' looks fine, but I think you have the wrong
type signature there.
>
> myAvg :: [Double] -> Double
> myAvg xs = myAvg' 0 0 xs
You could also do it without accumulating parameters, like this:
myAvg xs = sum / count
where (sum, count) = sumCount xs
sumCount :: [Double] -> (Double, Int)
sumCount [] = (0,0)
sumCount (x:xs) = (s+x, c+1)
where (s,c) = sumCount xs
-Brent
------------------------------
Message: 3
Date: Thu, 26 Mar 2009 20:43:24 +0000 (UTC)
From: 7stud <[email protected]>
Subject: [Haskell-beginners] Re: question about styles of recursion
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Michael Mossey <mpm <at> alumni.caltech.edu> writes:
>
> In RWH, in the exercises at the end of the book,
There are no exercises at the end of the book.
> I was told to write a
> function that averages the integer values in a list. I wanted to do this
> using on the tools we had been presented, which did not include
> 'length'.
Ok. That sounds like you are working on the exercises for chapter 3
on p.69, specifically exercise 3. The length function was introduced
before the sum function, which I see you are using in your
function definition. See p. 15. The length function was also
mentioned in exercise 1, and the instructions for exercise 3 mention
using the length of the list.
> So I thought of writing a recursive function in which each
> case passes an accumulator of the "sum so far" as well as a count of
> node "up to the point", and the base case does the actual division. I
> was wondering if there is a better way of doing it (using the just ideas
> up to chapter 3, so no length, no higher order functions, no foldr/foldl
> or map).
>
> myAvg' :: Int -> [Int] -> [ Double ] -> Double
> myAvg' sum count [] = sum / fromIntegral count
> myAvg' sum count (x:xs) = myAvg' (x + sum) (n + 1) xs
>
Use variables that are defined?
Prelude Data.List> :load ehask.hs
[1 of 1] Compiling Main ( ehask.hs, interpreted )
ehask.hs:3:44: Not in scope: `n'
Failed, modules loaded: none.
------------------------------
Message: 4
Date: Thu, 26 Mar 2009 20:53:47 +0000 (UTC)
From: 7stud <[email protected]>
Subject: [Haskell-beginners] Re: question about styles of recursion
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
7stud <bbxx789_05ss <at> yahoo.com> writes:
> > myAvg' :: Int -> [Int] -> [ Double ] -> Double
> > myAvg' sum count [] = sum / fromIntegral count
> > myAvg' sum count (x:xs) = myAvg' (x + sum) (n + 1) xs
> The length function was introduced
> before the sum function, which I see you are using in your
> function definition.
Hmm...I guess you aren't using the sum function--that's one
of your variable names. I wonder how Prelude knows sum
is a variable name and not the sum function? I changed the
name sum to s, and I get this error:
Prelude Data.List> :load ehask.hs
[1 of 1] Compiling Main ( ehask.hs, interpreted )
ehask.hs:2:24:
Couldn't match expected type `Double' against inferred type `Int'
In the expression: s / fromIntegral count
In the definition of `myAvg'':
myAvg' s count [] = s / fromIntegral count
Failed, modules loaded: none.
------------------------------
Message: 5
Date: Thu, 26 Mar 2009 22:10:45 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Re: question about styles of
recursion
To: [email protected]
Cc: 7stud <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Donnerstag 26 März 2009 21:53:47 schrieb 7stud:
> 7stud <bbxx789_05ss <at> yahoo.com> writes:
> > > myAvg' :: Int -> [Int] -> [ Double ] -> Double
> > > myAvg' sum count [] = sum / fromIntegral count
> > > myAvg' sum count (x:xs) = myAvg' (x + sum) (n + 1) xs
> >
> > The length function was introduced
> > before the sum function, which I see you are using in your
> > function definition.
>
> Hmm...I guess you aren't using the sum function--that's one
> of your variable names. I wonder how Prelude knows sum
> is a variable name and not the sum function?
Name shadowing/scoping. By naming one of the parameters sum, a
local variable with that name is declared and Prelude.sum is only
accessible qualified in that scope. It's the same as declaring local
variables with the same name as one in an enclosing scope in other
languages.
> I changed the
> name sum to s, and I get this error:
>
> Prelude Data.List> :load ehask.hs
> [1 of 1] Compiling Main ( ehask.hs, interpreted )
>
> ehask.hs:2:24:
> Couldn't match expected type `Double' against inferred type `Int'
> In the expression: s / fromIntegral count
> In the definition of `myAvg'':
> myAvg' s count [] = s / fromIntegral count
> Failed, modules loaded: none.
>
Yup, the type signature is wrong, it should be
myAvg' :: Double -> Int -> [Double] -> Double
------------------------------
Message: 6
Date: Thu, 26 Mar 2009 15:23:01 -0700
From: Michael Mossey <[email protected]>
Subject: Re: [Haskell-beginners] Re: question about styles of
recursion
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
7stud wrote:
> Michael Mossey <mpm <at> alumni.caltech.edu> writes:
>
>> In RWH, in the exercises at the end of the book,
>
> There are no exercises at the end of the book.
>
Thanks for the help everyone. I wrote this post in the middle of the
night when I had some insomnia, and I had just taken a sleeping
medication, so I was basically "drunk." It's like trying to program
drunk. My apologies for screwing up so many aspects of the post, but the
gist of my question was answered, I think.
Before I go further, let me ask again: can someone show me how to put
the "School of Expression" code "on the library path" so I don't have to
put it in the same directory where I'm working? I'm on Windows. I've
tried the -i option in ghci and ghc, but ghci and ghc don't see the SOE
code. I tried many ways of specifying the directory to -i: with quotes,
without quotes, relative path, absolute path. ghc happily accepts every
form I give it! But then fails to find SOE. I would like either to get
the -i form working, or even better have ghc read an environment
variable so it happens automatically every time it starts.
But back to the gist of my question last night: I am aware that most
examples of recursion presented in the books so far do their processing
"as the recursion unwinds." In other words:
length :: [a] -> Int
length [] = 0
length (x:xs) = 1 + length xs
This function goes deeper and deeper into the recursion before doing any
calculation, then does all the sums "on the way back out."
Being an imperative programmer, I keep trying to write loops that
accumulate "on the way down", like this:
length' :: Int -> [a] -> Int
length' s [] = s
length' s (x:xs) = length' (s+1) xs
length :: [a] -> Int
length xs = length' 0 xs
I suppose both forms are valid, but the first form seems to be most
natural in most situations I've encountered in the exercises. I'm
working with "Real World Haskell", "Haskell School of Expression," and
"Yet Another Haskell Tutorial." My strategy is to work each book's early
chapters before going further in any of the books, so I get multiple
"takes" on the material.
Thanks,
Mike
------------------------------
Message: 7
Date: Fri, 27 Mar 2009 02:37:27 +0000 (UTC)
From: 7stud <[email protected]>
Subject: [Haskell-beginners] Re: question about styles of recursion
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Daniel Fischer <daniel.is.fischer <at> web.de> writes:
>
> > myAvg' :: Int -> [Int] -> [ Double ] -> Double
>>
> > myAvg' sum count [] = sum / fromIntegral count
> > myAvg' sum count (x:xs) = myAvg' (x + sum) (n + 1) xs
>
>
> > ehask.hs:2:24:
> > Couldn't match expected type `Double' against inferred type `Int'
> > In the expression: s / fromIntegral count
> > In the definition of `myAvg'':
> > myAvg' s count [] = s / fromIntegral count
> > Failed, modules loaded: none.
> >
>
> Yup, the type signature is wrong, it should be
>
> myAvg' :: Double -> Int -> [Double] -> Double
>
Can you explain the error message in detail? To me it looks like
this should be the problem:
Prelude> fromIntegral [1, 2, 3]
<interactive>:1:0:
No instance for (Integral [t])
arising from a use of `fromIntegral' at <interactive>:1:0-21
Possible fix: add an instance declaration for (Integral [t])
In the expression: fromIntegral [1, 2, 3]
In the definition of `it': it = fromIntegral [1, 2, 3]
------------------------------
Message: 8
Date: Fri, 27 Mar 2009 04:42:52 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Re: question about styles of
recursion
To: [email protected]
Cc: 7stud <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Freitag 27 März 2009 03:37:27 schrieb 7stud:
> Daniel Fischer <daniel.is.fischer <at> web.de> writes:
> > > myAvg' :: Int -> [Int] -> [ Double ] -> Double
> > >
> > > myAvg' sum count [] = sum / fromIntegral count
> > > myAvg' sum count (x:xs) = myAvg' (x + sum) (n + 1) xs
> > >
> > >
> > > ehask.hs:2:24:
> > > Couldn't match expected type `Double' against inferred type
`Int'
> > > In the expression: s / fromIntegral count
> > > In the definition of `myAvg'':
> > > myAvg' s count [] = s / fromIntegral count
> > > Failed, modules loaded: none.
> >
> > Yup, the type signature is wrong, it should be
> >
> > myAvg' :: Double -> Int -> [Double] -> Double
>
> Can you explain the error message in detail? To me it looks like
> this should be the problem:
>
> Prelude> fromIntegral [1, 2, 3]
>
> <interactive>:1:0:
> No instance for (Integral [t])
> arising from a use of `fromIntegral' at <interactive>:1:0-21
> Possible fix: add an instance declaration for (Integral [t])
> In the expression: fromIntegral [1, 2, 3]
> In the definition of `it': it = fromIntegral [1, 2, 3]
>
That would be the next problem.
By the type signature, the result of myAvg' is a Double, hence the use
of (/) in the first equation is at type Double -> Double -> Double.
Now the first argument of (/) is s(um), which by the type signature is of
type Int, while (/) expects Double.
So, expected type is Double, 'inferred' (from the type signature) type
is Int, doesn't match, boom.
The compiler stops there, if it didn't, it would also report the missing
instance for (Integral [Int]).
------------------------------
Message: 9
Date: Thu, 26 Mar 2009 23:57:19 -0400 (EDT)
From: "Edward Z. Yang" <[email protected]>
Subject: [Haskell-beginners] Simple laplace solver
To: [email protected]
Message-ID: <alpine.deb.2.00.0903262354520.8...@javelin>
Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII
Hello all,
As my first foray into the wonderful world of Haskell programming, I
wrote a simple program that solves Laplace equations. The program is 44
lines, and I would greatly appreciate comments on how I could make it
better, with regards to Haskell idioms, performance and readability.
Cheers,
Edward
import Data.Array
data Node = Node {
isBoundary :: Bool,
value :: Double
}
instance Show Node where
show (Node True value) = show value ++ "*"
show (Node False value) = show value
blankArray :: Ix i => (i, i) -> a -> Array i a
blankArray (a, b) x = array (a, b) [(i, x) | i <- range (a, b)]
fillBox :: Ix i => (i, i) -> a -> Array i a -> Array i a
fillBox (a, b) value m = m // [(i, value) | i <- range(a, b)]
average :: Fractional a => [a] -> a
average list = sum list / fromIntegral (length list)
solve :: Array (Integer, Integer) Node -> Array (Integer, Integer) Node
solve m =
if maximum deltas > 0.0001
then solve n
else n
where
n = array (min, max) [(i, solveNode i) | i <- range(min, max)]
deltas = map delta $ zip (values m) (values n)
delta (x, y) = abs (x - y)
values a = map value $ elems a
(min, max) = bounds m
solveNode (a, b) =
if isBoundary $ node
then node
else averageNode
where
node = m ! (a, b)
averageNode = Node False $
average [value $ m ! i | i <-
[(a+1,b),(a-1,b),(a,b+1),(a,b-1)]]
myArray =
fillBox ((3,3), (5,5)) (Node True 100)
$ fillBox ((1,1), (8,8)) (Node False 0)
$ blankArray ((0,0), (9,9)) (Node True 0)
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 9, Issue 33
****************************************