This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Require Import Basics. | |
| Require Import BinInt. | |
| Require Import Zbool. | |
| Require Import List. | |
| Require Import ssreflect. | |
| Open Scope Z_scope. | |
| Open Scope list_scope. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object QuadraticEquationSolver { | |
| def solve(a: Double, b: Double, c: Double) = { | |
| val discriminant = b*b - 4*a*c | |
| if (discriminant < 0) | |
| List() | |
| else { | |
| val sqrtOfDisr = math.sqrt(discriminant) | |
| List((-b - sqrtOfDisr)/2, (-b + sqrtOfDisr)/2) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Control.Monad.State | |
| import qualified Data.Map as Map | |
| -- Мини-фреймворк для автоматической мемоизации рекурсивных функций. | |
| -- Построен с помощью монады State. | |
| -- Чтобы воспользоваться фреймворком, нужно описать требуемое вычисление | |
| -- в монадической форме и с открытой рекурсией. Последнее означает, | |
| -- что пользовательская функция должна в качестве первого дополнительного |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Control.Monad.State | |
| import qualified Data.Map as Map | |
| recall :: Ord a => a -> State (Map.Map a r) (Maybe r) | |
| recall n = do | |
| memory <- get | |
| return (Map.lookup n memory) | |
| memorize :: Ord a => a -> r -> State (Map.Map a r) () | |
| memorize n result = do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Control.Exception.Base (assert) | |
| import Data.Function (on) | |
| import Data.List (intercalate) | |
| import qualified Data.Map as Map | |
| type ErrorHandler a = String -> a | |
| languageLError message = error ("[Language L] " ++ message ++ ".") | |
| internalError message = languageLError ("Internal Error: " ++ message) | |
| expressionError message = languageLError ("Expression Evaluation: " ++ message) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (* ('a, 'v) expr GADT: | |
| * 'a stands for type of expression constants. | |
| * 'v stands for type of result of expression evaluation. | |
| * | |
| * For most cases an expression evaluates to value of the same | |
| * type as it's constants, but if the last operator of expression | |
| * is a comparison (Less constructor) or negation (Not constructor) | |
| * the whole expression has a boolean type. Note that in that case | |
| * 'a parameter stays the same, probably non-boolean type. What actually | |
| * changes is 'v. So, the following statement is true: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Make_expr (Args : | |
| sig | |
| type a | |
| end) | |
| = | |
| struct | |
| include Args | |
| type _ t = | |
| | Const : a -> a t |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type _ t = | |
| | Const : int -> int t | |
| | Add : int t * int t -> int t | |
| | Less : int t * int t -> bool t | |
| | IfThenElse : bool t * int t * int t -> int t | |
| let rec eval : type a. a t -> a = function | |
| | Const x -> x | |
| | Add (l, r) -> (eval l) + (eval r) | |
| | Less (l, r) -> (eval l) < (eval r) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| run_tests: irregular_tree.mli irregular_tree.ml run_tests.ml | |
| ocamlopt irregular_tree.mli irregular_tree.ml run_tests.ml -o $@ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cassert> | |
| #include <functional> | |
| std::function<int()> create_counter(int const delta) { | |
| int counter = 0; | |
| return [delta, counter]() mutable { | |
| return counter += delta; | |
| }; | |
| } |