{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "{-# LANGUAGE NegativeLiterals #-}\n", "{-# LANGUAGE FlexibleContexts #-}" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [], "source": [ "import Data.List.Split (splitOn, chunksOf)\n", "import Data.Char (ord, chr)\n", "import Data.Bits\n", "import Numeric (showHex)\n", "import Text.Printf" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "extract items from len = take len $ drop from $ items ++ items" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "-- replace original replacement from \n", "-- | from + length replacement <= length original = replacement ++ drop (length replacement) original\n", "-- | otherwise = drop (length original) extended ++ \n", "-- where extended = take from original ++ replacement ++ drop suflen original\n", "-- suflen = from + length replacement - length original " ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "replace original replacement from = take (length original) (start ++ replacement ++ remainder)\n", " where excess = drop (length original - from) replacement\n", " stub = drop (length excess) original\n", " start = take from (excess ++ stub)\n", " remainder = drop (length $ start ++ replacement) original " ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0,1,2]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "extract [0, 1, 2, 3, 4] 0 3" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2,1,0]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "reverse [0,1,2]" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "l0 = [0,1,2,3,4]" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2,1,0,3,4]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "replace l0 (reverse $ extract l0 0 3) 0" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4,0,1]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "extract l0 4 3" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0,4,2,3,1]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "replace l0 (reverse $ extract l0 4 3) 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "0 1 2 3 4\n", "0 1 2 3 [4]\n", "0 1) 2 3 ([4]\n", "0 4) 2 3 (1\n", "\n", "0 1 2 3 4 | 0 1 2 3 4\n", "0 1 2 3 [4] | 0 1 2 3 4\n", "0 1 2 3 ([4] | 0 1) 2 3 4\n", "0 1 2 3 ( 1 | 0 4) 2 3 4\n", "\n", "\n", " 0 1 2 3 4 | 0 1 2 3 4\n", " [0] 1 2 3 4 | 0 1 2 3 4\n", "([0] 1 2) 3 4 | 0 1 2 3 4\n", "( 2 1 0) 3 4 | 0 1 2 3 4" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "tie original start len = replace original replacement start\n", " where replacement = reverse $ extract original start len" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/html": [ "