mirror of
https://github.com/iritmaximus/haskell.git
synced 2026-10-12 19:13:39 +03:00
various fixes & clarifications to exercises, thanks to Leo Varis
This commit is contained in:
@@ -68,6 +68,9 @@ substring i j s = todo
|
||||
-- off the front _and_ back of a string until the result is a
|
||||
-- palindrome.
|
||||
--
|
||||
-- Hint: remember the function palindrome :: String -> Bool from the
|
||||
-- course material!
|
||||
--
|
||||
-- Examples:
|
||||
-- palindromify "ab" ==> ""
|
||||
-- palindromify "aaay" ==> "aa"
|
||||
|
||||
+7
-1
@@ -141,9 +141,15 @@ simplify p = todo
|
||||
-- Ex 10: implement the typeclass Num for RationalNumber. The results
|
||||
-- of addition an multiplication must be simplified.
|
||||
--
|
||||
-- Reminders:
|
||||
-- * negate x is 0-x
|
||||
-- * abs is absolute value
|
||||
-- * signum is -1, +1 or 0 depending on the sign of the input
|
||||
--
|
||||
-- Examples:
|
||||
-- RationalNumber 1 2 - RationalNumber 1 3 ==> RationalNumber 1 6
|
||||
-- RationalNumber 1 3 + RationalNumber 1 6 ==> RationalNumber 1 2
|
||||
-- RationalNumber 1 3 * RationalNumber 3 1 ==> RationalNumber 1 1
|
||||
-- negate (RationalNumber 2 3) ==> RationalNumber (-2) 3
|
||||
-- fromInteger 17 :: RationalNumber ==> RationalNumber 17 1
|
||||
-- abs (RationalNumber (-3) 2) ==> RationalNumber 3 2
|
||||
-- signum (RationalNumber (-3) 2) ==> RationalNumber (-1) 1
|
||||
|
||||
+2
-2
@@ -205,8 +205,8 @@ data PasswordRequirement =
|
||||
MinimumLength Int
|
||||
| ContainsSome String -- contains at least one of given characters
|
||||
| DoesNotContain String -- does not contain any of the given characters
|
||||
| And PasswordRequirement PasswordRequirement -- or'ing two requirements
|
||||
| Or PasswordRequirement PasswordRequirement -- and'ing
|
||||
| And PasswordRequirement PasswordRequirement -- and'ing two requirements
|
||||
| Or PasswordRequirement PasswordRequirement -- or'ing
|
||||
deriving Show
|
||||
|
||||
passwordAllowed :: String -> PasswordRequirement -> Bool
|
||||
|
||||
+5
-8
@@ -76,10 +76,8 @@ examplePicture1 = Picture f
|
||||
--
|
||||
-- In addition to the Picture it takes a width and a height.
|
||||
--
|
||||
-- The return type is IO (), which you don't need to worry about right
|
||||
-- now. A value of type IO () is something that can be executed by
|
||||
-- Haskell to produce side effects (like the creation of a file in
|
||||
-- this case).
|
||||
-- The return type is IO (). Check Lecture 8 for a short introduction
|
||||
-- to IO
|
||||
|
||||
render :: Picture -> Int -> Int -> String -> IO ()
|
||||
render (Picture f) w h name = writePng name (generateImage (\x y -> colorToPixel (f (Coord x y))) w h)
|
||||
@@ -92,7 +90,7 @@ render (Picture f) w h name = writePng name (generateImage (\x y -> colorToPixel
|
||||
--
|
||||
-- This should produce an example1.png file.
|
||||
--
|
||||
-- Remember: You can get open GHCi with `stack ghci Set8Project.hs`
|
||||
-- Remember: You can get open GHCi with `stack ghci Set8.hs`
|
||||
|
||||
-- For testing purposes, let's also define some functions for drawing
|
||||
-- pictures as lists. It's customary to show colours as hexadecimal
|
||||
@@ -108,7 +106,6 @@ colorToHex (Color r g b) = showHex r ++ showHex g ++ showHex b
|
||||
|
||||
getPixel :: Picture -> Int -> Int -> String
|
||||
getPixel (Picture f) x y = colorToHex (f (Coord x y))
|
||||
|
||||
renderList :: Picture -> (Int,Int) -> (Int,Int) -> [[String]]
|
||||
renderList picture (minx,maxx) (miny,maxy) =
|
||||
[[getPixel picture x y | x <- [minx..maxx]] | y <- (reverse [miny..maxy])]
|
||||
@@ -368,10 +365,10 @@ xy = Picture f
|
||||
-- The Zoom transform should scale a picture up just like the zoom function above.
|
||||
--
|
||||
-- The FlipX transform should flip the image along the vertical axis,
|
||||
-- i.e. map (10,15) to (10,-15).
|
||||
-- i.e. map (10,15) to (-10,15).
|
||||
--
|
||||
-- The FlipY transform should flip the image along the horizontal
|
||||
-- axis, i.e. map (10,15) to (-10,15).
|
||||
-- axis, i.e. map (10,15) to (10,-15).
|
||||
--
|
||||
-- The FlipXY transform should switch the x and y coordinates, i.e.
|
||||
-- map (10,15) to (15,10).
|
||||
|
||||
+4
-3
@@ -2167,9 +2167,10 @@ Returns the second element of a list
|
||||
<div class="sourceCode" id="cb250"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb250-1" data-line-number="1"><span class="ot">(+) ::</span> <span class="dt">Num</span> a <span class="ot">=></span> a <span class="ot">-></span> a <span class="ot">-></span> a</a>
|
||||
<a class="sourceLine" id="cb250-2" data-line-number="2"><span class="ot">(-) ::</span> <span class="dt">Num</span> a <span class="ot">=></span> a <span class="ot">-></span> a <span class="ot">-></span> a</a>
|
||||
<a class="sourceLine" id="cb250-3" data-line-number="3"><span class="ot">(*) ::</span> <span class="dt">Num</span> a <span class="ot">=></span> a <span class="ot">-></span> a <span class="ot">-></span> a</a>
|
||||
<a class="sourceLine" id="cb250-4" data-line-number="4">negate<span class="ot"> ::</span> <span class="dt">Num</span> a <span class="ot">=></span> a <span class="ot">-></span> a</a>
|
||||
<a class="sourceLine" id="cb250-5" data-line-number="5">abs<span class="ot"> ::</span> <span class="dt">Num</span> a <span class="ot">=></span> a <span class="ot">-></span> a</a>
|
||||
<a class="sourceLine" id="cb250-6" data-line-number="6">fromInteger<span class="ot"> ::</span> <span class="dt">Num</span> a <span class="ot">=></span> <span class="dt">Integer</span> <span class="ot">-></span> a</a></code></pre></div>
|
||||
<a class="sourceLine" id="cb250-4" data-line-number="4">negate<span class="ot"> ::</span> <span class="dt">Num</span> a <span class="ot">=></span> a <span class="ot">-></span> a <span class="co">-- 0-x</span></a>
|
||||
<a class="sourceLine" id="cb250-5" data-line-number="5">abs<span class="ot"> ::</span> <span class="dt">Num</span> a <span class="ot">=></span> a <span class="ot">-></span> a <span class="co">-- absolute value</span></a>
|
||||
<a class="sourceLine" id="cb250-6" data-line-number="6">signum<span class="ot"> ::</span> <span class="dt">Num</span> a <span class="ot">=></span> a <span class="ot">-></span> a <span class="co">-- -1 for negative values, 0 for 0, +1 for positive values</span></a>
|
||||
<a class="sourceLine" id="cb250-7" data-line-number="7">fromInteger<span class="ot"> ::</span> <span class="dt">Num</span> a <span class="ot">=></span> <span class="dt">Integer</span> <span class="ot">-></span> a</a></code></pre></div>
|
||||
<p><code>Num</code> also shows up in the types of integer literals:</p>
|
||||
<div class="sourceCode" id="cb251"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb251-1" data-line-number="1"><span class="dt">Prelude</span><span class="fu">></span> <span class="fu">:</span>t <span class="dv">12</span></a>
|
||||
<a class="sourceLine" id="cb251-2" data-line-number="2"><span class="dv">12</span><span class="ot"> ::</span> <span class="dt">Num</span> p <span class="ot">=></span> p</a></code></pre></div>
|
||||
|
||||
Reference in New Issue
Block a user