ocaml

Higher-order type constructors and functors in Ocaml

馋奶兔 提交于 2019-11-30 11:51:17
问题 Can the following polymorphic functions let id x = x;; let compose f g x = f (g x);; let rec fix f = f (fix f);; (*laziness aside*) be written for types/type constructors or modules/functors? I tried type 'x id = Id of 'x;; type 'f 'g 'x compose = Compose of ('f ('g 'x));; type 'f fix = Fix of ('f (Fix 'f));; for types but it doesn't work. Here's a Haskell version for types: data Id x = Id x data Compose f g x = Compose (f (g x)) data Fix f = Fix (f (Fix f)) -- examples: l = Compose [Just 'a'

How do I do automatic data serialization of data objects?

假装没事ソ 提交于 2019-11-30 11:48:49
问题 One of the huge benefits in languages that have some sort of reflection/introspecition is that objects can be automatically constructed from a variety of sources. For example, in Java I can use the same objects for persisting to a db (with Hibernate), serializing to XML (with JAXB), and serializing to JSON (json-lib). You can do the same in Ruby and Python also usually following some simple rules for properties or annotations for Java. Thus I don't need lots "Domain Transfer Objects". I can

If SML.NET had functors why can't F#?

感情迁移 提交于 2019-11-30 10:35:29
问题 This question started out from My translating of "ML for the Working Programmer" (WorldCat) by L. C. PAULSON to F# which uses functors for the examples. Eventual desire to translate "Purely Functional Data Structures" (WorldCat) by Chris Okasaki which uses functors. Reading "CATEGORIES TYPES AND STRUCTURES - An Introduction to Category Theory for the working computer scientist" (WorldCat) by Andrea Asperti and Giuseppe Longo. Not understanding it all, mostly the category theory. SML.NET can

Explaining pattern matching vs switch

醉酒当歌 提交于 2019-11-30 10:21:35
问题 I have been trying to explain the difference between switch statements and pattern matching(F#) to a couple of people but I haven't really been able to explain it well..most of the time they just look at me and say "so why don't you just use if..then..else". How would you explain it to them? EDIT! Thanks everyone for the great answers, I really wish I could mark multiple right answers. 回答1: Having formerly been one of "those people", I don't know that there's a succinct way to sum up why

type level integers in ocaml

混江龙づ霸主 提交于 2019-11-30 09:46:35
Could anyone give me suggestions/advice on making type level integers in OCaml (3.12) supporting addition and subtraction operations on them? For example, if I have numbers represented like this: type zero type 'a succ type pos1 = zero succ type pos2 = zero succ succ ... I need a way to define function on types like this: val add: pos2 -> pos1 -> pos3 Little background: I'm trying to port some haskell code for operations on physical dimensions and i need the ability to define operations on dimension types (record of 7 type level ints representing exponents of 7 basic SI units). I need to do it

Why OCaml's threading is considered as `not enough`?

馋奶兔 提交于 2019-11-30 08:32:25
It seems many people are saying OCaml does not have a good capacity for concurrency and it is also not good for web server applications. I am currently learning ocaml's manual . It seems that OCaml provide concurrency now. Can I know why OCaml's concurrency/threading is considered as bad? Can I develop server application in OCaml? What problems may I meet? Chuck See Concurrency vs. parallelism — What's the difference? . OCaml's threads offer concurrency, as you can have the next function start before the previous one is finished. But OCaml does not offer parallelism, so when that second

How do I read in lines from a text file in OCaml?

痴心易碎 提交于 2019-11-30 08:20:09
This is what I have so far. Isn't this all that you need? I keep getting the error "Error: Unbound module Std" let r file = let chan = open_in file in Std.input_list (chan) If you don't have Extlib installed (and apparently you don't based on the error message above), then generally it's done something like this: let read_file filename = let lines = ref [] in let chan = open_in filename in try while true; do lines := input_line chan :: !lines done; !lines with End_of_file -> close_in chan; List.rev !lines ;; If you do have Extlib: let read_file filename = let chan = open_in filename in Std

gcc ld: symbol(s) not found for architecture x86_64

安稳与你 提交于 2019-11-30 08:16:56
问题 Alright so I'm making a lexer and a parser using Ocamlyacc. I've done my research and I think it's something to do with my makefile not picking the right bit version for my compiler or something like it? I don't know much about makefiles which is why I'm asking. I've run my program on another computer where it works without trouble so it gotta be something to do with my machine. It's a MacBook Pro 64 bit. I'm using Xcode 4.2.1. Here's the makefile: SHELL = /bin/sh C_C = gcc CPP_C = g++ ifdef

automata in ocaml

懵懂的女人 提交于 2019-11-30 06:46:04
I am a bit new to OCaml. I want to implement product construction algorithm for automata in ocaml. I am confused how to represent automata in ocaml. Can someone help me? Victor Nicollet A clean representation for a finite deterministic automaton would be: type ('state,'letter) automaton = { initial : 'state ; final : 'state -> bool ; transition : 'letter -> 'state -> 'state ; } For instance, an automaton which determines whether a word contains an odd number of 'a' could be represented as such: let odd = { initial = `even ; final = (function `odd -> true | _ -> false) ; transition = (function

Converting F# pipeline operators ( <|, >>, << ) to OCaml

谁都会走 提交于 2019-11-30 04:22:01
问题 I'm converting some F# code to OCaml and I see a lot of uses of this pipeline operator <| , for example: let printPeg expr = printfn "%s" <| pegToString expr The <| operator is apparently defined as just: # let ( <| ) a b = a b ;; val ( <| ) : ('a -> 'b) -> 'a -> 'b = <fun> I'm wondering why they bother to define and use this operator in F#, is it just so they can avoid putting in parens like this?: let printPeg expr = Printf.printf "%s" ( pegToString expr ) As far as I can tell, that would