Skip to content

Instantly share code, notes, and snippets.

@yasar11732
Last active December 11, 2019 14:43
Show Gist options
  • Select an option

  • Save yasar11732/78e04bec0a722596ec7fb83677836a1d to your computer and use it in GitHub Desktop.

Select an option

Save yasar11732/78e04bec0a722596ec7fb83677836a1d to your computer and use it in GitHub Desktop.
{-
Exercise 1 Hopscotch
Your first task is to write a function
skips :: [a] -> [[a]]
The output of skips is a list of lists. The first list in the output should
be the same as the input list. The second list in the output should
contain every second element from the input list. . . and the nth list in
the output should contain every nth element from the input list.
For example:
skips "ABCD" == ["ABCD", "BD", "C", "D"]
skips "hello!" == ["hello!", "el!", "l!", "l", "o", "!"]
skips [1] == [[1]]
skips [True,False] == [[True,False], [False]]
skips [] == []
Note that the output should be the same length as the input.
-}
localMaxima :: [Integer] -> [Integer]
localMaxima [] = []
localMaxima [x] = []
localMaxima [x,y] = []
localMaxima (x:y:z:xs)
| y > x && y > z = [y] ++ localMaxima (y:z:xs)
| otherwise = localMaxima (y:z:xs)
histogram :: [Integer] -> String
histogram numbers =
let initialcounts = take 10 (repeat 0)
calculatedcounts = histogramcount numbers initialcounts
maxheight = maximum calculatedcounts
linefunctions = map drawline [maxheight..0]
in unlines (map ($ calculatedcounts) linefunctions)
histogramcount :: [Integer] -> [Int] -> [Int]
histogramcount [] counts = counts
histogramcount (x:xs) counts =
let
xint = fromIntegral x
newcount = (counts !! xint) + 1
in histogramcount xs (take xint counts ++ [newcount] ++ drop (xint+1) counts)
drawline :: Int -> [Int] -> String
drawline level = map (\x -> if x >= level then '#' else ' ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment