{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "{-# LANGUAGE FlexibleContexts #-}\n", "{-# LANGUAGE MultiWayIf #-}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import Data.List (tails)\n", "import qualified Data.HashMap.Strict as M\n", "import Data.HashMap.Strict ((!))\n", "import Control.Monad.State.Lazy" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "offsets2 = repeat 8" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[8,8,8]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "take 3 offsets2" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[8,16,24]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "take 3 $ scanl (+) 8 offsets2" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "diagonal n = scanl (+) 1 $ scanl (+) n $ repeat 8" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1,9,25,49,81]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "dr = diagonal 8\n", "take 5 dr" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1,5,17,37,65]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ul = diagonal 4\n", "take 5 ul" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1,3,13,31,57]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ur = diagonal 2\n", "take 5 ur" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1,7,21,43,73]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "dl = diagonal 6\n", "take 5 dl" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "73" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "dl!!4" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4,73)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "head $ dropWhile ((< 70) . snd) $ zip [0..] dl" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4,81)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "head $ dropWhile ((< 70) . snd) $ zip [0..] dr" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0,1,2]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "take 3 [0..]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "interleave4 (p:ps) (q:qs) (r:rs) (s:ss) = p:q:r:s:interleave4 ps qs rs ss" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "interleave ([]:_) = []\n", "interleave xss = map head xss ++ interleave (map tail xss)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0,1),(1,3),(1,5),(1,7),(1,9),(2,13),(2,17),(2,21),(2,25),(3,31),(3,37),(3,43),(3,49),(4,57),(4,65),(4,73),(4,81),(5,91),(5,101),(5,111)]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "take 20 $ drop 3 $ interleave4 (zip [0..] ur) (zip [0..] ul) (zip [0..] dl) (zip [0..] dr)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1,1,1,1]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "map head [ur, ul, dl, dr]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[3,13],[5,17],[7,21],[9,25]]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "map (take 2 . tail) [ur, ul, dl, dr]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "