From leavens@larch.cs.iastate.edu Tue Aug 31 14:59:57 2004 Date: Tue, 31 Aug 2004 14:59:57 -0500 (CDT) From: Gary T. Leavens To: Computer Science 541 , Ricky A. Kendall Subject: Com S 541 questions and answers, from class and homework Hi all, Here's a list of questions and answers I have from today in class and after. Precendence of ++ vs. : The precedence of Haskell operators is given by the table in section 4.4.2 of the Haskell report, see the URL: http://haskell.org/onlinereport/decls.html#fixity Here we see that : and ++ are both right associative and at the same precedence. This means that x : xs ++ ys is parsed as we intended without the parentheses: x : (xs ++ ys) I never remember all of these details between languages, and find it best to parenthesize whenever it seems likely to make a difference. But in Haskell I find it useful to remember the function application has higher precedence than all infix operators. Differentiation formula (homework 1, problem 7) Yasser and others asked how to approximate the derivitive for problem 7. For this, use the formula in probelm 6 (the easydiff function). That is,use the answer for probelm 6 to solve problem 7. What to read for the part of the homework due on Thursday? In the Thompson book (Haskell The Craft ...) read Chapters 1-7, and sections 9.1-9.2, 10.1-10.4, and 17.6. Other parts of chapters 9, 10, and 17 may also be helpful to read if you have time. Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 -------------------------------------------------------- From leavens@larch.cs.iastate.edu Tue Sep 14 07:41:18 2004 Date: Tue, 14 Sep 2004 07:41:18 -0500 (CDT) From: Gary T. Leavens To: Daniel Patanroi Subject: Re: hw1 part 2 Hi Dan, On Tue, 14 Sep 2004, Daniel Patanroi wrote: > I have some questions. > > on number 18c, how can we test this fmap function. I tried to type > fmap (\x -> x+2) (Node 5 []) > It gives error : cannot find "show" function This is because you are running Haskell in a mmode where you need to have show functions (instances of Show) for all data types. The easy way to fix this is to use the Hugs command :set -u or to invoke hugs so that it doesn't require that, as in our shell script: Main> fmap (\x -> x+2) (Node 5 []) Tree_Node 7 [] :: Tree Integer Note the setting for the u option below. Main> :set TOGGLES: groups begin with +/- to turn options on/off resp. s Print no. reductions/cells after eval t Print type after evaluation g Print no. cells recovered after gc l Literate modules as default . Print dots to show progress q Print nothing to show progress Q Qualify names when printing w Always show which modules are loaded k Show kind errors in full u Use "show" to display results I Display results of IO programs T Apply 'defaulting' when printing types R Enable root optimisation OTHER OPTIONS: (leading + or - makes no difference) hnum Set heap size (cannot be changed within Hugs) pstr Set prompt string to str rstr Set repeat last expression string to str Pstr Set search path for modules to str Sstr Set list of source file suffixes to str Estr Use editor setting given by str cnum Set constraint cutoff limit Fstr Set preprocessor filter to str Current settings: +tlqR -sg.QwkuIT -h250000 -p"%s> " -r$$ -c40 Search path : -P.:{Hugs}/libraries:{Hugs}/oldlib:{Hugs}/libraries/win32 Source suffixes : -S.hs:.lhs Editor setting : -Eemacs Preprocessor : -F Compatibility : Haskell 98 (+98) And if I switch to use +u, I get your problem: Main> :set +u Main> fmap (\x -> x+2) (Node 5 []) ERROR - Cannot find "show" function for: *** Expression : fmap (\x -> x + 2) (Node 5 []) *** Of type : Tree Integer The other way to fix this is to define a instance of the class Show (see the Prelude). You can also do this automatically by writing: data Tree a = Node a [Tree a] deriving Show instead of the definition given in the homework. > on number 18d, can we use auxiliary functions ? Yes, you can always do that. Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ------------------------------------------- From leavens@larch.cs.iastate.edu Wed Sep 15 08:02:57 2004 Date: Wed, 15 Sep 2004 08:02:57 -0500 (CDT) From: Gary T. Leavens To: Neeraj Khanolkar Subject: Re: hw1 question 20 Hi Neeraj, On Wed, 15 Sep 2004, Neeraj Khanolkar wrote: > Also in problem 20 (Points and regions) how abstract are we supposed to > treat Point, do we never try to extract x or y values using the pair (x, > y) pattern matching? I was thinking of primitives for area but that seems > difficult without getting the actual ints out of the Point. > Or do we have to create our own ADT for Point? The definition of Point is given to you in the problem. What I meant is that your primitives for Regions shouldn't access the coordinates of a Point, except in so far as the Regions they manipulate access Points. That is, leave all the access of Point values to the Region values. Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 --------------------------------------- From leavens@larch.cs.iastate.edu Wed Sep 15 08:08:48 2004 Date: Wed, 15 Sep 2004 08:08:47 -0500 (CDT) From: Gary T. Leavens To: Daniel Patanroi Subject: Re: 541 hw1, problem 20 Hi Daniel, On Wed, 15 Sep 2004, Daniel Patanroi wrote: > on #20 > > here we're asked to keep track a potentially infinite geometric regions. > So put it into a code, we need to maintain a list of Region. > So I would guess we have to create an ADT > > ---------------- > module Geometry (initial) where > > initial :: [Region] > > type Point (Int, Int) > type Region = Point -> Bool > > ------------------- > > Now, I can think of some function inside this module, such as put a region, > pop a region, check if a region member of the list. No, I didn't you to create an ADT of containers of regions. > But I still don't get it why Region has to be a boolean function. Because that way a Region can contain an infinite number of Points. > Do I miss the whole big picture for this problem? I guess I didn't explain it very well, as I wanted you to think about the design issues. As in problem 19, try to create some operations on regions (and points) that manipulates the regions as sets of points. For example, consider ways of forming Regions from other Regions or for asking questions about Points in Regions. Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 -------------------------------------------- From leavens@larch.cs.iastate.edu Wed Sep 15 14:26:52 2004 Date: Wed, 15 Sep 2004 14:26:52 -0500 (CDT) From: Gary T. Leavens To: Neeraj Khanolkar Subject: Re: problem 21 Hi Neeraj, On Wed, 15 Sep 2004, Neeraj Khanolkar wrote: > Hi Gary, > > Is it OK if I make OType an instance of Show class as well? Otherwise it > is unable to print on the interpreter command line. Yes, if you're using the (default) +u option on hugs, you'll need to do that. Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ------------------------------------------