Send Beginners mailing list submissions to beginners@haskell.org 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 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. Re: Haskell triangular loop (correct use of (++)) (AntC) 2. I have simple question about Haskell (Eunsu Kim) 3. Re: i have questions about Haskell (Simon Jakobi) 4. Re: Applying a function to two lists (Matt Williams) ---------------------------------------------------------------------- Message: 1 Date: Sat, 23 Apr 2016 00:20:58 +0000 (UTC) From: AntC <anthony_clay...@clear.net.nz> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Haskell triangular loop (correct use of (++)) Message-ID: <loom.20160423t020252-...@post.gmane.org> Content-Type: text/plain; charset=utf-8 > rohan sumant <r.s.sumant <at> gmail.com> writes: > > ?The approach I am following is this :- > mkpairs [] = [] > > mkpairs (x:xs) = (map (fn x) xs) ++ (mkpairs xs) > fn x y = (x,y) > > It is generating the desired output ... Good! You've got the right approach for triangular loops. That is a form mkpairs (x:xs) = {- stuff -} $ mkpairs xs A couple of things look non-idiomatic. So before I get on to your main question: As well as an empty list being a special case, neither can you make any pairs from a singleton ;-). I suggest: mkpairs [x] = [] The auxiliary `fn` is messy. I would put explicit lambda: mkpairs (x:xs) = map (\x' -> (x, x') ) xs ++ mkpairs xs Or even switch on tuple sections https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ syntax-extns.html#tuple-sections mkpairs (x:xs) = map (x, ) xs ++ mkpairs xs > but I am a bit unsure about the time complexity of the function mkpairs. ... > I am particularly worried about the (++) operator. ... You are spot-on! (++) has terrible time complexity. Use the showS trick for constant-time concatenation. Look in the Prelude for class Show a/method showsPrec, explained in the Haskell 2010 report section 6.3.3. To get that to work, you need a 'worker' function to hand across the successor for each list. It's conventional to call that `go`, and because everyone uses `go`, wrap it inside mkpairs using a `where`. Complete code: mkPairs [] = [] mkPairs [x] = [] mkPairs (x:xs) = go xs $ mkPairs xs where go [] s = s go (x':xs') s = (x, x') : go xs' s Now is that any faster in practice? Probably not until you get to a list with thousands of elements. HTH ------------------------------ Message: 2 Date: Fri, 22 Apr 2016 22:55:48 -0500 From: Eunsu Kim <wntuw...@gmail.com> To: beginners@haskell.org Subject: [Haskell-beginners] I have simple question about Haskell Message-ID: <e77e0bc9-3483-4f71-adeb-d20d09432...@gmail.com> Content-Type: text/plain; charset="utf-8" Hi THIS IS MY CODE: evalpoly = do putStr "What is the degree of polynomial: " degree <- getLine coeffs <- (funcOfCoeff ((read degree::Int)+1) [] ) putStr "What value do you want to evaluate at: " value <- getLine putStr "the value of " putStr (printChar coeffs) putStr (" evaluated at "++ value ++" is ") putStrLn (show (getResult (coeffs) (read value :: Float) )) printChar coeffs =parser([x | x <-reverse (zip coeffs (iterate (+1) 0)), fst x /= 0]) parser []="" parser (a:as)=show(fst a) ++ "x^" ++show(snd a) ++ " " ++(parser as) ?PROBLEM IS HERE!!! ---function loop to get coefficient-- funcOfCoeff 0 coeffs = do --to check the degree of 0 return coeffs --return list of coefficient funcOfCoeff degree coeffs = do putStr ("What is the x^" ++ show(degree-1)) putStr " coefficient: " coeff <- getLine loop <- funcOfCoeff (degree-1) ((read coeff :: Float) : coeffs) return loop getResult (coeffs) x = sum(map(\(a,b) -> a*x^b).zip coeffs.iterate (+1)$0) --evaluate polynomial with value HERE IS MY OUTPUT: *Main> evalpoly What is the degree of polynomial: 2 What is the x^2 coefficient: 3 What is the x^1 coefficient: 2 What is the x^0 coefficient: 1 What value do you want to evaluate at: 7 the value of 3.0x^2 2.0x^1 1.0x^0 evaluated at 7 is 162.0 in output, on the very bottom part, there should be + or - sign there like this: 3.0x^2 + 2.0x^1 - 1.0x^0 what should I do? I have no idea now?.. thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160422/c9c63af1/attachment-0001.html> ------------------------------ Message: 3 Date: Sat, 23 Apr 2016 05:58:30 +0200 From: Simon Jakobi <simon.jak...@googlemail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] i have questions about Haskell Message-ID: <cagtp2sgmf_qktb-sjh2g-opd6rbd8s03fmha8frjqb3o2q9...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 Hi! > when outputting the polynomial value, actually write out the polynomial, > but: > - skipping any missing monomials > - not including any extraneous signs > -not showing the constant term > > for the above example, the final line would be: > > The value of 1.0 x^3 - 2.0 x^2 + 10.0 evaluated at -1.0 is 7.0 Because this is apparently a homework problem that you're supposed to solve yourself, I'll only give you a few pointers: What the problem description hints at is a function with type Polynomial -> String. So in a first step you should define that data type Polynomial (or whatever you want to call it). So far you seem to have been using a list of coefficients to represent polynomials, so you might just build on that existing representation, either by creating a type alias or creating a separate "real" type with a constructor. In a second step you need to write a function Polynomial -> String with an appropriate definition. I'd recommend that you start by generating a simpler string representation like "1.0 x^3 + -2.0 x^2 + 0.0 x^1 + 10.0" and refine your definition iteratively. Cheers, Simon ------------------------------ Message: 4 Date: Sat, 23 Apr 2016 08:50:32 +0000 From: Matt Williams <matt.williams45...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Applying a function to two lists Message-ID: <CAFTVGQbe-cCEPpts1EFgumfsA21NY+H_=zompsq8otix9jm...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Thanks a lot for this. Just to clarify (and ignoring the flip, which I can solve by rewriting the checkNum function) - is this an example of currying? M On Fri, 22 Apr 2016 22:30 Francesco Ariis, <fa...@ariis.it> wrote: > On Fri, Apr 22, 2016 at 10:20:19PM +0100, Matt Williams wrote: > > I am looking for something like: > > > > list1 = [1,2,3,4,5,6] > > list2 = [1,2,3,4,5,6] > > > > map checkNum list1 list2 > > > > to return: > > > > [(1,[1]),(2[3,4,5]),(6,[3]) > > > > (I have tried to simplify this a little, so my apologies if it looks > > pointless - the real function is useful) > > > > Any help would be appreciated. > > > > Matt > > Hey Matt, > if what you want is > > [checkNum 1 list2, checkNum 2 list2, etc.] > > then > > map (flip checknum list2) list1 > > is what you want (flip signature being :: (a -> b -> c) -> b -> a -> c) > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160423/4c14237d/attachment-0001.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 94, Issue 23 *****************************************