ocaml

Building an OCaml cross-compiler - configure part

三世轮回 提交于 2019-12-10 18:20:04
问题 I need to build an OCaml cross-compiler. Sadly, it seems this is not supported out of the box and needs a little work, as described for an older version of the OCaml compiler. My first question is: What is a nice way to generate the files config/m.h , config/s.h and config/Makefile ? 回答1: I've been building OCaml cross compilers for a few years now. (See my profile for a link to my website.) What I do is build the compiler 1 1/2 times. The first time is for the host (with some settings for

Determining type on the fly in OCaml's OOP construct

本秂侑毒 提交于 2019-12-10 18:07:58
问题 I am learning about OCaml's OOP constructs and partially implemented this today until I realized I have no idea how to represent a polymorphic match statement without using the type keyword outside of the object. class bar (param:string) = object (code) end;; class foo param = object (code) initializer match param with string -> Printf.printf "param is a string" | bar -> Printf.printf "param is a bar" end;; let b = new bar "a string";; let f1 = new foo "test";; let f2 = new foo b;; Is it

Save image with ocaml graphics

倾然丶 夕夏残阳落幕 提交于 2019-12-10 17:48:18
问题 i want to save the picture generated by ocaml graphics in a file (png or jpeg). Thank you. 回答1: I'm going to assume that you are talking about the Graphics module in ocaml. You should notice that the Graphics module isn't for creating and processing images. You can of course call Graphics.dump_image if you've already gone ahead and wrote something that produces the Graphics.image type. This will produce a color array array , where color is a packed integer in the format, 0xRRGGBB. After some

Writing an interpreter with OCaml GADTs

梦想的初衷 提交于 2019-12-10 17:37:00
问题 I am writing a small interpreter in OCaml and am using GADTs to type my expressions: type _ value = | Bool : bool -> bool value | Int : int -> int value | Symbol : string -> string value | Nil : unit value | Pair : 'a value * 'b value -> ('a * 'b) value and _ exp = | Literal : 'a value -> 'a exp | Var : name -> 'a exp | If : bool exp * 'a exp * 'a exp -> 'a exp and name = string exception NotFound of string type 'a env = (name * 'a) list let bind (n, v, e) = (n, v)::e let rec lookup =

OCaml function with data type tree

好久不见. 提交于 2019-12-10 17:29:49
问题 We are give a tree that contains two types of elements. It is defined with the following data-structure. type ( 'a , 'b ) tree = Empty | Vertexa of 'a * ( 'a , 'b ) tree list | Vertexb of 'b * ( 'a , 'b ) tree list Write a function split: ('a,'b) tree -> 'a list * 'b list, that saves all elements of type 'a to the first list and all elements of type 'b in the second list. I had an idea of doing this recursively but I am kind of stuck on this. I will attach my attempt even though it does not

Initialize Array to Blank custom type OCAML

坚强是说给别人听的谎言 提交于 2019-12-10 17:29:47
问题 ive set up a custom data type type vector = {a:float;b:float}; and i want to Initialize an array of type vector but containing nothing, just an empty array of length x. the following let vecarr = Array.create !max_seq_length {a=0.0;b=0.0} makes the array init to {a=0;b=0} , and leaving that as blank gives me errors. Is what im trying to do even possible? 回答1: You can not have an uninitialized array in OCaml. But look at it this way: you will never have a hard-to-reproduce bug in your program

Passing a string to a C library from OCaml using Ctypes and Foreign

…衆ロ難τιáo~ 提交于 2019-12-10 17:24:23
问题 I'm really new to OCaml, and wanted to try and do some work with pcap as a way of getting started, only, there doesn't seem to be a maintained library for it. After looking at the awesome Real World OCaml book, I figured I'd give writing a binding a go. Here's the (poor) code snippet: open Ctypes open Foreign open PosixTypes let char_ptr = " " let pcap_lookupdev = foreign "pcap_lookupdev" (string @-> returning string_opt) let result = pcap_lookupdev char_ptr let test2 = match result with |

Tail recursive List.map

自作多情 提交于 2019-12-10 17:19:45
问题 The typical List.map function in OCaml is pretty simple, it takes a function and a list, and applies the function to each items of the list recursively. I now need to convert List.map into a tail recursive function, how can this be done? What should the accumulator accumulate? 回答1: Arguably the simplest approach is to implement map in terms of an tail-recursive auxiliary function map_aux that traverses the list while accumulating an already mapped prefix: let map f l = let rec map_aux acc =

Simple OCaml exercise

穿精又带淫゛_ 提交于 2019-12-10 17:06:25
问题 I'm trying to teach myself OCaml through Jason Hickey notes and the following exercise got me stumped. Question: Write a function sum that given two integer bounds m,n and a function f computes a summation. I'm trying this: let rec sum m n f= if m>n then 0 else if m=n then f n else f m + sum n m+1 f but it doesn't work, producing a type error. 回答1: You need some parentheses. let rec sum m n f= if m>n then 0 else if m=n then f n else f m + sum n (m+1) f (Although for readability, I would

How to realize error traceback in Emacs?

元气小坏坏 提交于 2019-12-10 16:25:42
问题 I am writing a compiler in Ocaml. The tracback works well when I compile and test it with make in a terminal, for instance: export OCAMLRUNPARAM=b ./Simpler-Basic test.sib Fatal error: exception Match_failure("interp.ml", 45, 21) Called from file "interp.ml", line 97, characters 72-86 Called from file "list.ml", line 74, characters 24-34 Called from file "interp.ml", line 108, characters 9-35 Called from file "main.ml", line 54, characters 4-17 make: *** [all] Error 2 But when I compile and