ocaml

Abusing the algebra of algebraic data types - why does this work?

爱⌒轻易说出口 提交于 2019-11-26 08:38:36
问题 The \'algebraic\' expression for algebraic data types looks very suggestive to someone with a background in mathematics. Let me try to explain what I mean. Having defined the basic types Product • Union + Singleton X Unit 1 and using the shorthand X² for X•X and 2X for X+X et cetera, we can then define algebraic expressions for e.g. linked lists data List a = Nil | Cons a (List a) ↔ L = 1 + X • L and binary trees: data Tree a = Nil | Branch a (Tree a) (Tree a) ↔ T = 1 + X • T² Now, my first

The value restriction

匆匆过客 提交于 2019-11-26 08:29:50
问题 In OCaml you can\'t generalize a partially-applied curried function (the \"value restriction\"). What is the purpose of the value restriction? What unpleasant would happen if it did not exist? 回答1: Without the value restriction or other mechanisms to restrict generalization, this program would be accepted by the type system: let r = (fun x -> ref x) [];; (* this is the line where the value restriction would trigger *) > r : 'a list ref r := [ 1 ];; let cond = (!r = [ "foo" ]);; The variable r

Using regular expressions to validate a numeric range

霸气de小男生 提交于 2019-11-25 23:58:11
问题 My input number is an int. But the input number must be in a range from -2055 to 2055 and I want to check this by using regular expression. So is there anyway to write a regular expression to check whether a number is in (-2055, 2055) or not ? It is easier to use if statement to check whether the number is in range or not. But I\'m writing an interpreter so I should use regex to check the input number 回答1: Using regular expressions to validate a numeric range To be clear: When a simple if