I get indenting errors with the following attempts to use let:
----- import Data.List (sortBy) mySort xs = sortBy myCompare xs let lx = length x ly = length y in where myCompare x y | lx < ly = LT | lx == ly = EQ | lx > ly = GT ------------------ import Data.List (sortBy) mySort xs = sortBy myCompare xs where myCompare x y let lx = length x ly = length y in | lx < ly = LT | lx == ly = EQ | lx > ly = GT ------------- import Data.List (sortBy) mySort xs = sortBy myCompare xs where myCompare x y let lx = length x ly = length y in | lx < ly = LT | lx == ly = EQ | lx > ly = GT ------------- This is what I ended up with: ------- import Data.List (sortBy) mySort xs = sortBy myCompare xs where myCompare x y | lx < ly = LT | lx == ly = EQ | lx > ly = GT where lx = length x ly = length y ------- But I would like to know how to use let. |
What about...
mySort xs = let myCompare x y | lx < ly = LT | lx == ly = EQ | lx > ly = GT where lx = length x ly = length y in sortBy myCompare xs M; 7stud wrote: > I get indenting errors with the following attempts to use let: > > ----- > import Data.List (sortBy) > > mySort xs = sortBy myCompare xs > let lx = length x > ly = length y > in where myCompare x y > | lx < ly = LT > | lx == ly = EQ > | lx > ly = GT > > ------------------ > > import Data.List (sortBy) > > mySort xs = sortBy myCompare xs > where myCompare x y > let lx = length x > ly = length y > in > > | lx < ly = LT > | lx == ly = EQ > | lx > ly = GT > > ------------- > > import Data.List (sortBy) > > mySort xs = sortBy myCompare xs > where myCompare x y > let lx = length x > ly = length y > in | lx < ly = LT > | lx == ly = EQ > | lx > ly = GT > > ------------- > > > This is what I ended up with: > > ------- > import Data.List (sortBy) > > mySort xs = sortBy myCompare xs > where myCompare x y > | lx < ly = LT > | lx == ly = EQ > | lx > ly = GT > where lx = length x > ly = length y > ------- > > But I would like to know how to use let. > > > > > > _______________________________________________ > Beginners mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners > |
In reply to this post by 7stud-2
7stud wrote:
> > import Data.List (sortBy) > > mySort xs = sortBy myCompare xs > where myCompare x y > | lx < ly = LT > | lx == ly = EQ > | lx > ly = GT > where lx = length x > ly = length y How about import Data.Ord (comparing) mySort = sortBy (comparing length) or at least mySort = sortBy myCompare where myCompare x y = compare (length x) (length y) Regards, apfelmus -- http://apfelmus.nfshost.com |
In reply to this post by Miguel Pignatelli
Miguel Pignatelli <miguel.pignatelli <at> uv.es> writes:
> > What about... > > mySort xs = let myCompare x y > | lx < ly = LT > | lx == ly = EQ > | lx > ly = GT > where > lx = length x > ly = length y > in sortBy myCompare xs > That doesn't work for me: -- bhask.hs mySort xs = let myCompare x y | lx < ly = LT | lx == ly = EQ | lx > ly = GT where lx = length x ly = length y in sortBy myCompare xs Prelude> :load bhask.hs [1 of 1] Compiling Main ( bhask.hs, interpreted ) bhask.hs:4:16: parse error (possibly incorrect indentation) Failed, modules loaded: none. Prelude> Line 4 is the first guard. |
Am Montag, 2. M?rz 2009 20:42 schrieb 7stud:
> > mySort xs = let myCompare x y > > | lx < ly = LT > | lx == ly = EQ > | lx > ly = GT > > where > lx = length x > ly = length y > in sortBy myCompare xs > > > > > Prelude> :load bhask.hs > [1 of 1] Compiling Main ( bhask.hs, interpreted ) > > bhask.hs:4:16: parse error (possibly incorrect indentation) > Failed, modules loaded: none. > Prelude> > > > Line 4 is the first guard. > That line must be indented further than the first letter of myCompare in the line above. After the keyword 'let', the position of the start of the next significant token (not whitespace or comments), sets a new indentation level. The definiton begun there extends until - a line indented less or equally far is encountered - the keyword 'in' appears - an explicit semicolon ends the definition If a line indented less appears before the keyword 'in', a parse error results. Details can be found in http://haskell.org/onlinereport/syntax-iso.html#sect9.3 |
In reply to this post by Heinrich Apfelmus
Heinrich Apfelmus <apfelmus <at> quantentunnel.de> writes:
> How about > > import Data.Ord (comparing) > > mySort = sortBy (comparing length) > I just finished chap. 3 of Real World Haskell, which doesn't even imports. It took me hours to figure out how to access sortBy. The book hasn't introduced "comparing", yet. > or at least > > mySort = sortBy myCompare > where > myCompare x y = compare (length x) (length y) > Very nice. The book mentioned the compare function in chap. 2. I have a question about that code: how come you don't have to specify a parameter for mySort, for example: mySort xs = ... And doesn't sortBy require two arguments? sortBy :: (a -> a -> Ordering) -> [a] -> [a] (1) (2) How come you can write it with only one argument? Finally, l'm wondering if anyone can explain why my let examples failed? |
In reply to this post by 7stud-2
7stud wrote: > Miguel Pignatelli <miguel.pignatelli <at> uv.es> writes: > >> What about... >> >> mySort xs = let myCompare x y >> | lx < ly = LT >> | lx == ly = EQ >> | lx > ly = GT >> where >> lx = length x >> ly = length y >> in sortBy myCompare xs >> > > That doesn't work for me: > Yes, sorry about that, it is an issue with indentation while copy/pasting the code M; > -- bhask.hs > > mySort xs = let myCompare x y > | lx < ly = LT > | lx == ly = EQ > | lx > ly = GT > where > lx = length x > ly = length y > in sortBy myCompare xs > > > > > Prelude> :load bhask.hs > [1 of 1] Compiling Main ( bhask.hs, interpreted ) > > bhask.hs:4:16: parse error (possibly incorrect indentation) > Failed, modules loaded: none. > Prelude> > > > Line 4 is the first guard. > > > > _______________________________________________ > Beginners mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners > |
In reply to this post by 7stud-2
2009/3/2 7stud <[hidden email]>
> I get indenting errors with the following attempts to use let: > ... three failed exaopmles and a good one ... But I would like to know how to use let. From beginner to beginner, they look to me more as syntax errors than indentation problem. The syntax for let is let { decl ; ... decl } in expression or in layout format let decl ... decl in expression and your attempts do not respect this syntax. In all three cases, after the keyword 'in' you need a full expression, and neither a where clause nor a set of matches are a full expression by themselves. An expression is either a function apllication or a 'statement' like if or case, or maybe something more I can't recall. In my haskell exercises I really appreciate the fact that the haskell mode of emacs has syntax hints (also function hints for functions in the prelude). Even now that I know haskell basic syntax, I find it helpful. Don't know of any other editors with the same capability for haskell, though, and if you are not an emacs user, learning the not-so-standard ways of emacs might offset the benefits of having syntax hints. Ciao --------- FB -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090303/63defd80/attachment-0001.htm |
In reply to this post by 7stud-2
7stud wrote:
> Heinrich Apfelmus <apfelmus <at> quantentunnel.de> writes: >> How about >> >> import Data.Ord (comparing) >> >> mySort = sortBy (comparing length) >> > > I just finished chap. 3 of Real World Haskell, which doesn't > even imports. It took me hours to figure out how to > access sortBy. The book hasn't introduced "comparing", yet. It's a handy little function for exactly this sort of thing. It's defined as comparing p x y = compare (p x) (p y) See also http://haskell.org/hoogle/?q=comparing >> or at least >> >> mySort = sortBy myCompare >> where >> myCompare x y = compare (length x) (length y) >> > > Very nice. The book mentioned the compare function in > chap. 2. > > I have a question about that code: how come you > don't have to specify a parameter for mySort, for example: > > mySort xs = ... > > And doesn't sortBy require two arguments? > > sortBy :: (a -> a -> Ordering) -> [a] -> [a] > (1) (2) > > How come you can write it with only one argument? You can supply arguments one at a time, this is called "currying". In other words, the expression sortBy myCompare is created by supplying one argument to sortBy and thus has one argument remaining. More specifically, it has the type sortBy myCompare :: [[a]] -> [[a]] Furthermore, we can simply set mySort = sortBy myCompare and the left hand side will be a function with one argument just like the right hand side is. > Finally, I'm wondering if anyone can explain why my > let examples failed? Your use of let in conjunction with where was not syntactically correct. While both are used for declaring new things, let is an expression while where is a declaration, these two don't mix. In particular, let must always have the form let x = ... y = ... ... in expr while where is used like this foo x y | ... = expr1 | ... = expr2 where a = ... b = ... ... Construction like let ... in where ... let ... in | ... do not exist, there is no expression after the in . I suggest consulting a syntax reference for Haskell or using where only for the time being. Regards, apfelmus -- http://apfelmus.nfshost.com |
Heinrich Apfelmus <apfelmus <at> quantentunnel.de> writes:
> Thanks for the explanations. |
In reply to this post by Francesco Bochicchio
Francesco Bochicchio <bieffe62 <at> gmail.com> writes:
> In all three cases, after? the keyword 'in' > you need a **full** expression, > and neither a where clause nor a set of matches are a full expression > by themselves. An expression is either a function apllication or a 'statement' > like if or case, or maybe something more I can't recall. > Thanks for the help. > Don't know of any other editors with the same capability for haskell, though, > and if you are not an emacs user, learning > the not-so-standard ways of emacs might offset the benefits of having > syntax hints.? I don't know emacs. I use vim. I may have to give emacs a try. |
Free forum by Nabble | Edit this page |