ocaml

Handling incremental Data Modeling Changes in Functional Programming

安稳与你 提交于 2019-12-17 10:32:43
问题 Most of the problems I have to solve in my job as a developer have to do with data modeling. For example in a OOP Web Application world I often have to change the data properties that are in a object to meet new requirements. If I'm lucky I don't even need to programmatically add new "behavior" code (functions,methods). Instead I can declarative add validation and even UI options by annotating the property (Java). In Functional Programming it seems that adding new data properties requires

Handling incremental Data Modeling Changes in Functional Programming

大兔子大兔子 提交于 2019-12-17 10:32:14
问题 Most of the problems I have to solve in my job as a developer have to do with data modeling. For example in a OOP Web Application world I often have to change the data properties that are in a object to meet new requirements. If I'm lucky I don't even need to programmatically add new "behavior" code (functions,methods). Instead I can declarative add validation and even UI options by annotating the property (Java). In Functional Programming it seems that adding new data properties requires

Is it possible to define rest argument in OCaml?

▼魔方 西西 提交于 2019-12-14 03:54:07
问题 Relate thread in reddit. In racket, we could define a function with rest argument as: (define (avg . l) (/ (apply + l) (length l))) We could call this function by: > (avg 1 2 3) 2 There are plenty of ways to solve this particular avg mentioned by the replies from reddit. But if I want to do something more complicated like: (define *memoize-tbl* (make-hasheq)) (define (bind fn . args) (let ([res (apply fn args)]) (hash-set! *memoize-tbl* (equal-hash-code (cons fn args)) res) res)) (define (f1

What exactly is the syntax error here?

╄→гoц情女王★ 提交于 2019-12-14 03:22:42
问题 I'm trying to write a function that will return the second smallest number in a list. I keep getting a syntax error but I can't really pinpoint what the issue is. Can I please get help on this? Deleted code 回答1: You forget to close local let bindings using in . The correct (and indented) code should be: let second_smallest_helper1 lst= let second_smallest_helper2 currentMinimum currentNumber = if currentMinimum < currentNumber then currentMinimum else currentNumber in List.fold_left second

Type to capture either integer, float or a string value and then do pattern matching in Scala

不打扰是莪最后的温柔 提交于 2019-12-14 02:11:16
问题 In Ocaml it is possible to define something like this: type univ = I of int | F of float | S of string ;; In order to create objects with like this: let pol_list = [I 3; F 4.3; S "potato"; I 4];; And then do pattern matching to extract a certain property (Either value or length depending on the case) like this: let get_value val = match val with | I v -> v | F v -> (int_of_float v) | S s -> (String.length s) How would this be done in Scala? If not possible, is there another similar

Ocaml - polymorphic print and type losing

匆匆过客 提交于 2019-12-14 00:29:55
问题 There is series of functions like print_int, print_endline and Printf in OCaml. I can't do something like: let n = 10 in print n;; (* And I haven't to change `print` in case type of `n` changed *) That is polymorphic print like in Java, C#, Python and others. Instead, we have C-like with type explicitly defined by programmer. So I think that OCaml losing type information during compilation and doesn't have it at runtime, right? And this is the reason why we need mli files also? EDIT: I'm

How do you append a char to a string in OCaml?

心已入冬 提交于 2019-12-14 00:18:40
问题 It seems as if there is no function in the standard library of type char -> string -> string , which insert a char in front of (or at the end of) a string . There are workarounds, e.g. by using String.make or String.blit . Is there an elegant way to do this? 回答1: String.make and String.blit is a good way to do so, but they seem to be imperative. Personally I prefer to make infix functions using Char.escaped and string concatenation: let (^$) c s = s ^ Char.escaped c (* append *) let ($^) c s

Should OCaml compilation with custom linking work in Windows (via MinGW)?

青春壹個敷衍的年華 提交于 2019-12-13 14:26:53
问题 I want to compile an OCaml program interfacing with C code, using a MinGW-based GCC, and using separate compilation (GCC produces the .o , then ocamlopt produces the final executable). It's not clear to me if (1) this should work on Windows and, if so, (2) which command-line arguments are necessary. I'm using Jonathan Protzenko's OCaml on Windows installer to install OCaml 4.02.1 along with a Cygwin shell (note that it uses a native windows OCaml compiler, not a Cygwin-based one). I installed

OCaml: assert with the message

做~自己de王妃 提交于 2019-12-13 14:23:37
问题 Another question again :P I'm not too sure whether I should post it here or on the OCaml mailing list, but I try SO first. I like assert statements. However, I find the error messages close to useless without an additional message (assertion violation at line XXX --- well great, but what actually went wrong?). I think the good example of an assertion is a pythonic assert x > 0, "X must be greater than zero for the algorithm X to work" and a bad example is C-like assert(x>0) . I was quite

Ocaml, replace all specified elements with a given element in a list

会有一股神秘感。 提交于 2019-12-13 13:27:24
问题 I am writing a ocaml project, in which I have a function that replace all '' in a char-list with 'E' . Here's my code for this propose: let rec string_lst_change_E lst = match lst with [] -> let a ='E'; a::[] |(h::t) if (h = '') -> 'E'::(string_lst_change_E t) |(h::t) -> h::(string_lst_change_E t) ;; It says I have a syntax error... But I cannot figure out by myself. I tried to modify it like this: let rec string_lst_change_E lst = match lst with [] -> 'E'::[] |(h::t) ->if (h = '') then 'E'::