"Semesters/2023-05F/370/homework/due.php"
This is a course page of
David Casperson
You are here: homeSemestersFall 2023CPSC 370
Associate Professor
Computer Science
University of Northern British Columbia

CPSC 370: Functional and Logic Programming ( Fall 2023)

Questions Pending.

…can be found at ./370/homework/pending.php .

Questions Due.


    1. How many binary Bool functions are there?
    2. How mamy of these are monoids?
    3. How many are semigroups but not monoids?
  1. Write an explicit tail-recursive function to compute the maximum value of a non-empty list (N.B. maximum does this, and is part of the Prelude. Write your own function that avoids using the bultin maximum.)
  2. Redo the previous question using foldl and no explicit tail-recursiion.
  3. Write a function
    isSorted::(Ord a) => ArrayNE a -> Bool
    that returns True iff its argument is sorted in non-decreasing (≤) order.

  4. Hoogle the Down newtype. What is it for?

    Show how to use your previous isSorted function for ArrayNEs, Down, and fmap to determine if an ArrayNE is sorted in non-increasing (≥) order.

  5. With a newtype wrapper

    newtype Cx = Cx { getCs :: Int }
    

    around Int, is

    instance Semigroup Cx where
        x <> y = Cx (1 + getCx x)
    

    a legitimate semigroup definition? Why or why not?

  6. Given
    data OneValueType = ThisIsIt
    instance Semigroup OneValueType where 
        (<>) = const
    instance Monoid OneValueType where
       mempty = ThisIsIt
     
    determine if this is a legitimate mathematical monoid.

    (yes, this example is perverse.)

  7. Given “data Triple a = Tx a a a”, define
    1. Functor,
    2. Foldable, and
    3. Applicative
    instances for Triple.
  8. Given
    data RoseTree a
      = Pure a
      | Nest [RoseTree a]
    
    define Functor and Foldable instances for RoseTree.

  1. Install Prolog on your computer. (Preferably SWI Prolog.) Tell me how you did this, including which Prolog version, and the operating system for your computer.


Question Numbering below fixed 2023-10-04.

  1. Write a definition for the uncurry function.

    Yes, it is possible to Hoogle the answer.

    The preferred approach is to look hard at your answers to Question 14-(7,8) until you are sure you know what uncurry is doing.

    Next, climb a 10 000' mountain. Ignore the sweat dripping into your eyes, and obtain a state of Zen-like calm. Wait for the solution to arrive.

    When it doesn’t, climb back down the mountain. Be sure to have a pencil and piece of paper with you. When the answer suddenly occurs to you, write it down.

    When you get home, try it out in ghci.

  2. maybe fun?

    The Maybe type gets used a lot, partially because it provides a way to make partial functions total, partially because Maybe is an instance of important classes like Monad and Applicative.

    Consequently there are a lot of small, but useful functions for Maybe. Many of them can be written wusing maybe (that's the function, not the type).

    For each of the following maybe expressions;

    • Determine its type. (See if you can cmopute the type in your head. Check it with :type)
    • Try to figure out what it might be useful for.
    • Determine its standard name (using Hoogle on its type).

    Try these:

    1. maybe Nothing Just
    2. maybe Nothing (Just . f) (Here f must be a function. Try specific functions to see if you can get a pattern.)
    3. flip maybe id
    4. maybe Nothing id
    5. maybe True (const False). Also, what does const do? Why is it necessary here?
    6. maybe [] (:[]).

Let's start with some questions where we mimic lists with the Array2 and ArrayNE from the class notes. There is a module JoinList in the Moodle notes. Download it, and import it into your own code.

Many of these questions can be solved indirectly with code that looks like
fromList . a List function . toList  . Avoid doing so.

Most of the solutions are (mecessarily) recursive. Tail-recursive solutions are not necessary.

  1. Write take and drop functions that work like their list equivalentgs, but on Array2's. Be careful if you write a dropNE function because you may end up with an empty array.
  2. Write a map function that work like its list equivalentgs,
  3. Write a filter :: (a -> Bool) -> (Array2 a -> Array2 a) function that keeps only the elements that satisfy the (a -> Bool) predicate.
  1. Read Chapter 3 of Learn You a Haskell for Great Good. Briefly explain the differences as you see them between “where” and “let … in …”.
  2. Using the notion of guards from Chapter 3, wrote code to compute the sin function using the formulas on this page. Hote that you can ignore everything after the word Bonus.

    The point of this question is to learn how to use guard notation (and possibly let or where). If you are having difficulty reading the math notation, ask me!

Question Numbering below fixed 2023-10-04.

  1. This question is similar to Question 6, but with different kinds of expressions. Download the file cpsc370--2023-05F--exprs-2.txt. Each of the twelve lines (starting after the number, period, and spaces) is possibly a legal Haskell expression.

    Think about exch expression. See if you can work out in your head what the type is (or determine that there is something wrong).

    Use ghci to determine which of these expressions are legal. For the legal values, use “:type” to determine what the type of the expression is.

    Do any of the results surprise you?

  1. The arc hyperbolic cosine is defined by
    arc cosh y = log ( y ± sqrt ( y^2 - 1))
    Use the totalSqrt and totalLog defined in the previous question to write a
    totalArcCosh :: Double -> Maybe Double
    function. The point of this question is to experience using functions like totalSqrt. It is possible to code a function totalArcCosh :: Double -> Maybe Double directly, but that is not the point of this question.

    (The arc cosh is the inverse of the cosh function, defined by
    cosh x = ((exp x) + (exp (-x)))/2 .
    You can use this fact to test your arc cosh function: you should have (arc cosh (cosh x)) = x.)

  2. Write a totalDot function:
    totalDot :: (b -> Maybe c) 
             -> (a -> Maybe b) 
             -> (a -> Maybe c)
    
    that is the analog of standard function compositon (.)
    (.) :: (b -> c) -> (a -> b) -> (a -> c)
    
    With this function you should be able to write formulæ like totalDot totalSqrt totalLog x . Code this reasonably directly. The following questions will show slicker and shorter ways to code this idea.
  3. There is a builtin (e.g., in the Prelude) function maybe function.
    1. What is the type of maybe?
    2. Can you see a way to use maybe to give very consice defintion of totalDot?
  4. Use Hoogle to find an operator that acts like totalDot. (You will need an import to use the operator, but it is part of the standard Haskell install.)
  1. sqrt x is not defined for x < 0; log x is not defined for x ≤ 0;

    Define functions

              totalSqrt :: Double -> Maybe Double
              totalLog  :: Double -> Maybe Double
            

    that have as large a domains as possible.

  1. Read Chapter 2 of Learn You a Haskell for Great Good. Explain as clearly as you can the difference between a type and a type class.
  2. Download the file cpsc370--2023-05F--exprs-1.txt. Each of the twelve lines (starting after the number, period, and spaces) is possibly a legal Haskell expression.

    Use ghci to determine which of these expressions are legal. For the legal values, use “:type” to determine what the type of the expression is.

    Do any of the results surprise you?

  3. What feature of Java most resembles the Haskell Ord class?
  1. Katie Miller mentions a group for women in functional programming that she was a part of. What is it called?
  2. Install Haskell on your computer. Tell me how you did this, including Haskell version, and the operating system for your computer.
  3. Thera are not a lot of IDEs for Haskell. If you find one that you intend to use, tell me what it is. Otherwise, tell me what your favourite generic code-editing application is. (All choices other than emacs are wrong. 😎)
  4. In java block comments (“/* … */”) do not nest.
    public class Oooops {
    /* /* Don’t try this */ at home */
    }
    What is the haskell syntax for block comments? Do they nest?
Home page Semesters Site Map
go back Fall 2023 go forward
2024-11 other links

CPSC 370 [Other years]
Homework
Resources
CPSC 704
David’s Schedule

UNBC Undergraduate Calendar
Dates
Computer Science (BSc)
Regulations

fall-2024