{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Cours 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Premierement, faisons un petit test.\n", "\n", "Question\n", "\n", "Ecrire une fonction qui calcul la moyenne d'une liste" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#'user/moyenner" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(defn moyenner [lst]\n", " (/ (apply + lst) (count lst)))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(moyenner [ 10 11 12])" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#'user/double-all" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(defn double-number[n]\n", " (* 2 n))\n", "\"\"\n", "(defn double-all [ lst]\n", " (map #(* 2 %) lst))\n", " " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(90 132 24 90)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(double-all [45 66 12 45])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", ";; 3 < 4 < 10 < 100\\\n", ";; (< 3 4 10 100)\n", "(defn larger-than [n]\n", " (< 3 n))\n", " \n", " \n", "\n", "(defn new-sum [lst]\n", " \"Add all the numbers in lst that are larger than 3.\"\n", " (apply + lst) \n", " (filter #(< 3 %) lst)\n", " (apply + (filter #(< 3 %) lst)))\n", " \n", "(new-sum [1 5 2 4 4 0])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(#(- %1 %2) 5 6)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1 2 34] \"hello\" :toto]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(let [x [ 1 2 34]\n", " y \"hello\"\n", " z :toto]\n", " [x y z])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#'user/new-sum" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(defn new-sum [lst]\n", " \"Add all the numbers in lst that are larger than 3.\"\n", " (let [sub-lst (filter #(< 3 %) lst)]\n", " (apply + sub-lst)))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#'user/f" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ ";; (reduce f initial-value lst)\n", "(defn f [acc new-el]\n", " ;; update acc\n", " acc)\n", " " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "; var initial-value = [];\n", ";for(var e : lst){\n", "; initial-value.push(f(e));\n", ";}" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2 5 9)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(reductions + [2 3 4])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3 2 4 6)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(conj (map #(* 2 %)[ 1 2 3])3)\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(reduce + [2 3 4])" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[12 14 16]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(defn foo [g lst e] \n", " (conj lst (g e)))\n", "\n", "(defn my-map [f lst]\n", " (let [poartialled-foo (partial foo f)]\n", " (reduce poartialled-foo [] lst)))\n", "\n", "(my-map #(* 2 %) [6 7 8]) ;; 12 14 16\n", "\n", ";;#(conj %1 (f %2))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#'user/toto" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(defn toto [ x y]\n", " (+ x y))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "190" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "((partial toto 90) 100)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(51 24 21 324 156)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ ";; 3 * x + 7\n", "\n", ";; 17 * 3 <- 17 <- 10\n", "(let [f1 (map #(* 3 %))\n", " f2 (map #(+ % 7))]\n", " (sequence (comp f2 f1) [10 1 0 101 45]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$ f(g(x)) = f \\circ g (x) $$" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"8544\"" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(-> 16 \n", " (* 3)\n", " double-number\n", " (* 89)\n", " str)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"FOO\"" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(-> :foo name clojure.string/upper-case )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Lein-Clojure", "language": "clojure", "name": "lein-clojure" }, "language_info": { "file_extension": ".clj", "mimetype": "text/x-clojure", "name": "clojure", "version": "1.9.0" } }, "nbformat": 4, "nbformat_minor": 2 }