Skip to content

Latest commit

 

History

History
106 lines (70 loc) · 4.04 KB

Exercises.mdown

File metadata and controls

106 lines (70 loc) · 4.04 KB

Exercises!

  1. write words
takeWhile :: (a -> Bool) -> [a] -> [a]
dropWhile :: (a -> Bool) -> [a] -> [a]

Prelude> takeWhile (<3) [1..10]
[1,2]
-- Takes the elements that are less than 3

Prelude> takeWhile (<8) (enumFromTo 5 15)
[5,6,7]
-- Takes the elements that are less than 8

Prelude> takeWhile (>6) [1..10]
[]
-- It returns an empty list because it stops
-- taking as soon as the condition isn't met,
-- which in this case is the first element

Prelude> takeWhile (=='a') "abracadabra"
"a"

Prelude> dropWhile (<3) [1..10]
[3,4,5,6,7,8,9,10]

Prelude> dropWhile (<8) (enumFromTo 5 15)
[8,9,10,11,12,13,14,15]

Prelude> dropWhile (>6) [1..10]
[1,2,3,4,5,6,7,8,9,10]

Prelude> dropWhile (=='a') "abracadabra"
"bracadabra"

Using takeWhile and dropWhile, write a function that takes a string and returns a list of strings, using spaces to separate the elements of the string into words, as in the following sample:

*Main> myWords "all i wanna do is have some fun"
["all","i","wanna","do","is","have","some","fun"]
  1. How can we implement map by ourselves?

  2. Write a filtering function to remove the articles ("the," "a," "an") from a sentence. You may use words (either yours or the one in Prelude).

  3. Library boolean functions:

myOr returns Trueif any value in the list isTrue`.

myOr :: [Bool] -> Bool
myOr = undefined

myAny returns True if the function applied to any of the values in the list returns True.

myAny :: (a -> Bool) -> [a] -> Bool
myAny = undefined
  1. At home:

We'll be using Data.Char for this next exercise. We'll later turn these into a small project, complete with user input and testing (!!) so make a stack project for them and keep them around. We'll talk about this next time. -- add the stack instructions here yo

A Caesar cipher is a simple substitution cipher, in which each letter is replaced by the letter that is a fixed number of places down the alphabet from it. You will find variations on this all over the place --- you can shift leftward or rightward, for any number of spaces. A rightward shift of 3 means that 'A' will become 'D' and 'B' will become 'E,' for example. If you did a leftward shift of 5, then 'a' would become 'v' and so forth.

Your goal in this exercise is to write a basic Caesar cipher that shifts rightward. You can start by having the number of spaces to shift fixed, but it's more challenging to write a cipher that allows you to vary the number of shifts so that you can encode your secret messages differently each time.

You can find plenty of Caesar ciphers written in Haskell if you look for them, but to maximize the likelihood that you can write yours without peeking at those, here are a couple of tips. When yours is working the way you want it to, we would encourage you to then look around and compare your solution to others out there.

The first lines of your text file should look like this:

module Cipher where

import Data.Char

Data.Char includes two functions called ord and chr that can be used to associate a Char with its Int representation in the Unicode system and vice versa:

*Cipher> :t chr
chr :: Int -> Char
*Cipher> :t ord
ord :: Char -> Int

Using these functions is optional; there are other ways you can proceed with shifting, but using chr and ord might simplify the process a bit.

You want your shift to wrap back around to the beginning of the alphabet, so that if you have a rightward shift of 3 from 'z,' you end up back at 'c' and not somewhere in the vast Unicode hinterlands. Depending on how you've set things up, this might be a bit tricky. Consider starting from a base character (e.g., 'a') and using mod to ensure you're only shifting over the 26 standard characters of the English alphabet.

You should include an unCaesar function that will decode your text as well, and we will write tests for these in an upcoming session.

  1. At home:

Rewrite myOr and myAny with folds. :)

hint: check out ||

See how they work when given a potentially infinite list, such as [1..]