Search Results

Search found 97 results on 4 pages for 'ocaml'.

Page 2/4 | < Previous Page | 1 2 3 4  | Next Page >

  • Sorting a list in OCaml

    - by darkie15
    Hi All, Here is the code on sorting any given list: let rec sort lst = match lst with [] -> [] | head :: tail -> insert head (sort tail) and insert elt lst = match lst with [] -> [elt] | head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail;; [Source: Code However, I am getting an Unbound error: Unbound value tail # let rec sort lst = match lst with [] -> [] | head :: tail -> insert head (sort tail) and insert elt lst = match lst with [] -> [elt] | head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail;; Characters 28-29: | head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail;; ^ Error: Syntax error Can anyone please help me understand the issue here?? I did not find head or tail to be predefined anywhere nor in the code

    Read the article

  • OCaml: Matching with any negative

    - by nicotine
    Is there a way to get pattern matching to match my value with any negative number? It does not matter what the negative number is I just need to match with any negative. I have accomplished what I want with this simple code: let y = if(n < 0) then 0 else n in match y with 0 -> [] | _ -> [x] @ clone x (n - 1) But I want to eliminate that if statement and just get it to check it as another case in the match statement

    Read the article

  • OCaml: Currying without defined values

    - by nicotine
    I have two functions f and g and I am trying to return f(g(x)) but I do not know the value of x and I am not really sure how to go about this. A more concrete example: if I have functions f = x + 1 and g = x * 2 and I am trying to return f(g(x)) I should get a function equal to (x*2) + 1

    Read the article

  • Problem with pattern matching in ocaml

    - by Antony
    I wrote the function used to decompose a Boolean function, the problem is that the compilation I get this : "Warning 5: this function application is partial, maybe some arguments are missing." How can I solve this problem? I've set wrong the patter matching or I can not run this operation with pattern matching The code is the following: let rec decomposition state_init state prec formula = match formula with And form -> (fun () -> let f1 = List.hd form in let f2 = And(List.tl form )in let new_state = Forms (state_init,f1) in decomposition state_init new_state state f1; decomposition state_init new_state state f2; Hashtbl.add graph new_state (("",false,state :: []) , []) ; let x = Hashtbl.find graph state in let succ = state :: snd x in let (desc,last,ptrs) = fst x in Hashtbl.replace graph state ( ("And-node",last,ptrs) , succ))

    Read the article

  • What is the purpose of OCaml's Lazy.lazy_from_val?

    - by Ricardo
    The doc of Lazy.lazy_from_val states that this function is for special cases: val lazy_from_val : 'a -> 'a t lazy_from_val v returns an already-forced suspension of v This is for special purposes only and should not be confused with lazy (v). Which cases are they talking about? If I create a pair of suspended computation from a value like: let l1 = lazy 123 let l2 = Lazy.lazy_from_val 123 What is the difference between these two? Because Lazy.lazy_is_val l1 and Lazy.lazy_is_val l2 both return true saying that the value is already forced!

    Read the article

  • how to get a sub list from a list in ocaml

    - by romerun
    Hi, I'm looking at the List document. It seems the library does not provide a sublist function. I'm trying to get list of elements from i to j. Now I have to write it as: let rec sublist list i j = if i j then [] else (List.nth list i) :: (sublist list (i+1) j) which is quite concise but I'm questioning the efficiency of List.nth, because if it's O(n), I would rather have to write it in a less concise way. I'm wondering why didn't they provide List.sublist func, if List.nth is not O(1), because it's such a quite common operation..

    Read the article

  • Will Algorithm written in OCaml compiled from C be Faster than Algorithm written in Pure C code?

    - by Ole Jak
    So I have some cool Image Processing algorithm. I have written it in OCaml. It performs well. I now I can compile it as C code with such command ocamlc -output-obj -o foo.c foo.ml (I have a situation where I am not alowed to use OCaml compiler to bild my programm for my arcetecture, I can use only specialy modified gcc. so I will compile that programm with sometyhing like gcc -L/usr/lib/ocaml foo.c -lcamlrun -lm -lncurses and Itll run on my archetecture.) I want to know in general case will my OCaml code compiled into C run faster than algorithm implemented in pure C?

    Read the article

  • can a program written in C be faster than one written in OCaml and translated to C?

    - by Ole Jak
    So I have some cool Image Processing algorithm. I have written it in OCaml. It performs well. I now I can compile it as C code with such command ocamlc -output-obj -o foo.c foo.ml (I have a situation where I am not alowed to use OCaml compiler to bild my programm for my arcetecture, I can use only specialy modified gcc. so I will compile that programm with sometyhing like gcc -L/usr/lib/ocaml foo.c -lcamlrun -lm -lncurses and Itll run on my archetecture.) I want to know in general case can a program written in C be faster than one written in OCaml and translated to C?

    Read the article

  • Is it possible to have a Ocaml function that accepts only integer lists?

    - by Sam
    I'm writing a recursive function in Ocaml that's supposed to count the number of items in an integer list (Yes I know there's a List.length function but I'm trying to do it myself). However the Ocaml compiler/interpreter forces me to use alpha list all the time. So is it wrong to say that, when a function accepts a list as a parameter, the type of that list must always be alpha? Thanks EDIT: the reason why it's inconvenient for me to use alpha lists is because i can't compare the head of the alpha list with an integer value due to type-matching complaints

    Read the article

  • Creating GUI desktop applications that call into either OCaml or Haskell -- Is it a fool's errand?

    - by Rob Lachlan
    In both Haskell and OCaml, it's possible to call into the language from C programs. How feasible would it be to create Native applications for either Windows, Mac, or Linux which made extensive use of this technique? (I know that there are GUI libraries like wxHaskell, but suppose one wanted to just have a portion of your application logic in the foreign language.) Or is this a terrible idea?

    Read the article

  • Should I learn Haskell or F# if I already know OCaml?

    - by Unknown
    I am wondering if I should continue to learn OCaml or switch to F# or Haskell. Here are the criteria I am most interested in: Longevity Which language will last longer? I don't want to learn something that might be abandoned in a couple years by users and developers. Will Inria, Microsoft, University of Glasgow continue to support their respective compilers for the long run? Practicality Articles like this make me afraid to use Haskell. A hash table is the best structure for fast retrieval. Haskell proponents in there suggest using Data.Map which is a binary tree. I don't like being tied to a bulky .NET framework unless the benefits are large. I want to be able to develop more than just parsers and math programs. Well Designed I like my languages to be consistent. Please support your opinion with logical arguments and citations from articles. Thank you.

    Read the article

  • How does Ocaml decide precedence for user-defined operators?

    - by forefinger
    I want nice operators for complex arithmetic to make my code more readable. Ocaml has a Complex module, so I just want to add operators that call those functions. The most intuitive way for me is to make a new complex operator from all of the usual operators by appending '&' to the operator symbol. Thus +& and *& will be complex addition and multiplication. I would also like ~& to be complex conjugation. If I'm going to use these operators, I want them to associate the same way that normal arithmetic associates. Based on the following sessions, they are automatically behaving the way I want, but I would like to understand why, so that I don't get horrible bugs when I introduce more operators. My current guess is that their precedence is done by lexically sorting the operator symbols according to an ordering that is consistent with normal arithmetic precedence. But I cannot confirm this. Session one: # open Complex;; # let (+&) a b = add a b;; val ( +& ) : Complex.t -> Complex.t -> Complex.t = <fun> # let ( *&) a b = mul a b;; val ( *& ) : Complex.t -> Complex.t -> Complex.t = <fun> # one +& zero *& one +& zero *& one;; - : Complex.t = {re = 1.; im = 0.} # zero +& one *& zero +& one *& zero;; - : Complex.t = {re = 0.; im = 0.} # i +& i *& i +& i *& i *& i;; - : Complex.t = {re = -1.; im = 0.} Session two: # open Complex;; # let ( *&) a b = mul a b;; val ( *& ) : Complex.t -> Complex.t -> Complex.t = <fun> # let (+&) a b = add a b;; val ( +& ) : Complex.t -> Complex.t -> Complex.t = <fun> # one +& zero *& one +& zero *& one;; - : Complex.t = {re = 1.; im = 0.} # zero +& one *& zero +& one *& zero;; - : Complex.t = {re = 0.; im = 0.} # i +& i *& i +& i *& i *& i;; - : Complex.t = {re = -1.; im = 0.} # let (~&) a = conj a;; val ( ~& ) : Complex.t -> Complex.t = <fun> # (one +& i) *& ~& (one +& i);; - : Complex.t = {re = 2.; im = 0.}

    Read the article

  • In OCaml, how can I create an out_channel which writes to a string/buffer instead of a file on disk

    - by Tianyi Cui
    I have a function of type in_channel -> out_channel -> unit which will output something to an out_channel. Now I'd like to get its output as a string. Creating temporary files to write and read it back seems ugly, so how can I do that? Is there any other methods to create out_channel besides Pervasives.open_out family? Actually, this function implemented a repl. What I really need is to test it programmatically, so I'd like to first wrap it to a function of type string -> string. For creating the in_channel, it seems I can use Scanf.Scanning.from_string, but I don't know how to create the out_channel parameter.

    Read the article

  • Ocamlrun.lib Not Found For Linking

    - by Onorio Catenacci
    Hi all, Trying to build the OCaml Win32 API binaries for OCaml 3.11.0 on Win 7 and I consistently get a message when I try nmake dynamic: 'cannot open input file "ocamlrun.lib"'. My google skills seem to be failing me--is there something I need to do to get this .lib file? It doesn't seem to be part of the libraries which are included in the OCaml binary distribution for Windows. Can anyone give me a pointer in the right direction?

    Read the article

  • A more concise example that illustrates that type inference can be very costly?

    - by mrrusof
    It was brought to my attention that the cost of type inference in a functional language like OCaml can be very high. The claim is that there is a sequence of expressions such that for each expression the length of the corresponding type is exponential on the length of the expression. I devised the sequence below. My question is: do you know of a sequence with more concise expressions that achieves the same types? # fun a -> a;; - : 'a -> 'a = <fun> # fun b a -> b a;; - : ('a -> 'b) -> 'a -> 'b = <fun> # fun c b a -> c b (b a);; - : (('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'a -> 'c = <fun> # fun d c b a -> d c b (c b (b a));; - : ((('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'c -> 'd) -> (('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'a -> 'd = <fun> # fun e d c b a -> e d c b (d c b (c b (b a)));; - : (((('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'c -> 'd) -> (('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'd -> 'e) -> ((('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'c -> 'd) -> (('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'a -> 'e = <fun> # fun f e d c b a -> f e d c b (e d c b (d c b (c b (b a))));; - : ((((('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'c -> 'd) -> (('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'd -> 'e) -> ((('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'c -> 'd) -> (('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'e -> 'f) -> (((('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'c -> 'd) -> (('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'd -> 'e) -> ((('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'c -> 'd) -> (('a -> 'b) -> 'b -> 'c) -> ('a -> 'b) -> 'a -> 'f = <fun>

    Read the article

  • Can Goldberg algorithm in ocamlgraph be used to find Minimum Cost Flow graph?

    - by Tautrimas
    I'm looking for an implementation to the Minimum Cost Flow graph problem in OCaml. OCaml library ocamlgraph has Goldberg algorithm implementation. The paper called Efficient implementation of the Goldberg-Tarjan minimum-cost flow algorithm is noting that Goldberg-Tarjan algorithm can find minimum cost graph. Question is, does ocamlgraph algorithm also find the minimum cost? Library documentation only states, that it's suitable at least for the maximum flow problem. If not, does anybody have a good link to a nice any minimum cost optimization algorithm code? I will manually translate it into OCaml then. Forgive me, if I missed it on Wikipedia: there are too many algos on flow networks for the first day!

    Read the article

  • Syntax error beyond end of program

    - by a_m0d
    I am experimenting with writing a toy compiler in ocaml. Currently, I am trying to implement the offside rule for my lexer. However, I am having some trouble with the ocaml syntax (the compiler errors are extremely un-informative). The code below (33 lines of it) causes an error on line 34, beyond the end of the source code. I am unsure what is causing this error. open Printf let s = (Stack.create():int Stack.t); let rec check x = ( if Stack.is_empty s then Stack.push x s else if Stack.top s < x then ( Stack.push x s; printf "INDENT\n"; ) else if Stack.top s > x then ( printf "DEDENT\n"; Stack.pop s; check x; ) else printf "MATCHED\n"; ); let main () = ( check 0; check 4; check 6; check 8; check 5; ); let _ = Printexc.print main () Ocaml output: File "lexer.ml", line 34, characters 0-0: Error: Syntax error Can someone help me work out what the error is caused by and help me on my way to fixing it?

    Read the article

  • How do I do automatic data serialization of data objects in Haskell

    - by Adam Gent
    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) or 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 concentrate on the domain I am working in. It seems in very strict FP like Haskell and Ocaml this is not possible. Particularly Haskell. The only thing I have seen is doing some sort of preprocessing or meta-programming (ocaml). Is it just accepted that you have to do all the transformations from the bottom upwards? In other words you have to do lot of boring work to turn a data type in haskell into JSON/XML/DB Row object and back again into a data object.

    Read the article

< Previous Page | 1 2 3 4  | Next Page >