{ "cells": [ { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "-- Write a function to compute the value of a word. \n", "-- The value of a word is defined as the sum of the \n", "-- values of the individual letters of the word with \n", "-- value of \"a\" being 1, \"b\" being 2 etc. Upper and \n", "-- lower case letters have the same value. You can \n", "-- assume that the word consists of just letters.\n", "\n", "import Data.Char\n", "wordValue :: String -> Int\n", "wordValue \"\" = 0\n", "wordValue (c:cs) = 1 + ord (toLower c) - ord 'a' + wordValue cs" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "wordValue \"AttiTude\"" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "wordValue1 :: String -> Maybe Int\n", "wordValue1 \"\" = Just 0\n", "wordValue1 (c:cs) \n", " | isLetter c = let res = wordValue1 cs in\n", " case res of\n", " Nothing -> Nothing\n", " Just n -> Just (1 + ord (toLower c) - ord 'a' + n) \n", " | otherwise = Nothing" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Just 100" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "Nothing" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "wordValue1 \"AttiTude\"\n", "wordValue1 \"Att1Tude\"" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "wordValue2 :: String -> Maybe Int\n", "wordValue2 \"\" = Just 0\n", "wordValue2 (c:cs) \n", " | isLetter c = wordValue2 cs >>= \\n -> Just (1 + ord (toLower c) - ord 'a' + n)\n", " | otherwise = Nothing" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Just 100" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "Nothing" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "wordValue2 \"AttiTude\"\n", "wordValue2 \"Att1Tude\"" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "wordValue3 :: String -> Maybe Int\n", "wordValue3 \"\" = Just 0\n", "wordValue3 (c:cs) \n", " | isLetter c = do\n", " n <- wordValue3 cs\n", " Just (1 + ord (toLower c) - ord 'a' + n)\n", " | otherwise = Nothing" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Just 100" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "Nothing" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "wordValue3 \"AttiTude\"\n", "wordValue3 \"Att1Tude\"" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "-- given f and a positive integer n, repeated returns a function that applies f \n", "-- to its input n times. i.e. f(f(...(f(x))...))\n", "repeated :: (Int -> Int) -> Int -> (Int -> Int)\n", "repeated f 0 = f\n", "repeated f n = f . repeated f (n-1)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "g = repeated (2*) 3" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "64" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "g 4" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "-- def convertNum2Binary(num: Int): String =\n", "convertNum2Binary Int -> String\n" ] } ], "metadata": { "kernelspec": { "display_name": "Haskell", "language": "haskell", "name": "haskell" }, "language_info": { "codemirror_mode": "ihaskell", "file_extension": ".hs", "name": "haskell", "pygments_lexer": "Haskell", "version": "8.6.5" } }, "nbformat": 4, "nbformat_minor": 4 }