Git Product home page Git Product logo

haskell-exercises's Introduction

Opqdonut's Haskell Exercises

Note! You probably want to check out my Haskell MOOC (course page, GitHub) instead! It's completely free and open and is based on these exercises, but also contains study material and other improvements. These exercises still work, but this repository isn't maintained.

Quick Start

If these instructions don't work for you, see the instructions for using cabal instead of stack

  1. Install Stack

  2. Download dependencies by running

     $ stack build
    
  3. Then check you can actually run the tests with:

     $ stack runhaskell W0Test.hs
    

    This should print Everything seems to be OK!. If you see any errors, you might not have a problem with your Haskell installation.

  4. Generate the exercise templates (files W*.hs):

     $ make
    
  5. Now you can edit W1.hs and see how well you did by running

     $ stack runhaskell W1Test.hs
    
  6. You can also play around with your solutions interactively by running

     $ stack ghci W1.hs
    

Introduction

This is a collection of small and simple Haskell exercises with unit tests. This means your answers to the exercises are checked by the computer – making these exercises great for self-study.

The exercises don't really include any reading material, so you should study some Haskell tutorial, e.g. Learn You A Haskell For Great Good! while working on the exercises.

If bump into an exercise that talks about terms you don't understand, look them up in a tutorial or google them! The exercises are meant to encourage you to learn, not check that you already know stuff :)

I created this set of exercises for a Haskell course I held at Helsinki University because I couldn't find a set of good Haskell exercises anywhere. I hope somebody else finds them useful as well.

Contents

The exercises are split into sets called "weeks", each containing about 20 exercises. You are meant to work through the weeks in order, but you don't need to get every exercise in a week right to move on.

The topics of the weeks are

  • W1: basics, syntax, defining functions, pattern matching, recursion
  • W2: lists, strings, higher-order functions, polymorphism
  • W3: data types
  • W4: IO
  • W5: type classes
  • W6: Monads
  • W7: recap of weeks 1-6

Working on the Excercises

  • Read and edit the Wn.hs files according to the instructions

  • Don't remove or change any type signatures (things like foo :: String -> String) that are already in the files

  • Check your answers for week n with stack runhaskell WnTest.hs (or alternatively cabal exec runhaskell WnTest.hs if you're not using stack)

  • A typical test failure looks like this:

      Testing 11
      *** Failed! Falsifiable (after 1 test):
      0
      0
      Expected 1, got 3
      FAIL
    

    This means that the function from exercise 11 failed the test when the two arguments were 0 and 0. The result should have been 1, but it was 3.

    I'm sorry if the test failures aren't always understandable :/

  • You can also play around with your solutions interactively by running stack ghci Wn.hs (or cabal exec ghci Wn.hs). This is a good idea for instance when you don't understand the test failures.

Solutions

This repository also contains solutions to the exercises. You can get the solutions for week n by running

$ make WnSol.hs

The solutions are also visibile under the directory templ/. Don't look there if you don't want spoilers!

haskell-exercises's People

Contributors

aleksejkozin avatar basicacid avatar joamaki avatar opqdonut avatar soniah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

haskell-exercises's Issues

Incorrect definition of `primes`

Problem

The definition of primes in file W1Test.hs, i.e.

primes = nubBy (\x y -> mod x y == 0) [2..]

is incorrect; for instance, if you load W1Test.hs in GHCi and evaluate

take 5 primes

you get [2,3,4,5,6]. Because of this incorrect definition, tests for problems 17-19 fail, even on correct solutions.

Solution

Replacing the incorrect definition by a correct one, e.g.

primes = 2 : filter isPrime' [3, 5 ..]
  where
    isPrime' n
        | n < 2     = False
        | otherwise = all (\p -> n `mod` p /= 0) $
                          takeWhile (\p -> p ^ 2 <= n) primes

solves the problem.

Ambigious reference running W0test.hs

After installing QuickCheck calling runhaskell W0test.hs returns

mpl/Test.hs:34:10:
Ambiguous occurrence ===' It could refer to eitherImpl.Test.===',
defined at Impl/Test.hs:35:8
or Test.QuickCheck.===', imported fromTest.QuickCheck' at Impl/Test.hs:4:1-45
(and originally defined in `Test.QuickCheck.Property')

Off a fresh install of haskell-platform.

Type errors in W3Test.hs with GHC 7.10.1

After cloning the repo and running make,

runhaskell W3Test.hs

results in the following type errors (in GHC 7.10.1):

W3Test.hs:26:11:
    No instance for (Show (t0 Int)) arising from a use of ‘property’
    The type variable ‘t0’ is ambiguous
    Note: there are several potential instances:
      instance Show a => Show (Control.Applicative.Const a b)
        -- Defined in ‘Control.Applicative’
      instance Show a => Show (Control.Applicative.ZipList a)
        -- Defined in ‘Control.Applicative’
      instance Show a => Show (Data.Complex.Complex a)
        -- Defined in ‘Data.Complex’
      ...plus 23 others
    In the expression: property
    In the expression: property $ ex14_mysum
    In the expression:
      [property $ ex14_mysum, property $ ex14_mylength]

W3Test.hs:26:22:
    No instance for (Foldable t0) arising from a use of ‘ex14_mysum’
    The type variable ‘t0’ is ambiguous
    Note: there are several potential instances:
      instance Foldable (Control.Applicative.Const m)
        -- Defined in ‘Control.Applicative’
      instance Foldable (Either a) -- Defined in ‘Data.Foldable’
      instance Foldable Data.Proxy.Proxy -- Defined in ‘Data.Foldable’
      ...plus five others
    In the second argument of ‘($)’, namely ‘ex14_mysum’
    In the expression: property $ ex14_mysum
    In the expression:
      [property $ ex14_mysum, property $ ex14_mylength]

LICENSE for this repository

Hello,

Thank you very much for your exercises. I would like to solve them and push to my repository for practising and employment purpose in the future. Can you specify a license for this project so that I will know if I have a right to do that or not?

Best regards,
TRINH Quoc Anh

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.