sml

Case Statements and Pattern Matching

心不动则不痛 提交于 2019-12-03 13:28:32
I'm coding in SML for an assignment and I've done a few practice problems and I feel like I'm missing something- I feel like I'm using too many case statements. Here's what I'm doing and the problem statements for what I'm having trouble with.: Write a function all_except_option, which takes a string and a string list. Return NONE if the string is not in the list, else return SOME lst where lst is like the argument list except the string is not in it. fun all_except_option(str : string, lst : string list) = case lst of [] => NONE | x::xs => case same_string(x, str) of true => SOME xs | false =

What are the limits of type inference?

喜夏-厌秋 提交于 2019-12-03 05:48:52
问题 What are the limits of type inference? Which type systems have no general inference algorithm? 回答1: Joe Wells showed that type inference is undecidable for System F, which is the most basic polymorphic lambda calculus, independently discovered by Girard and Reynolds. This is the most important result showing the limits of type inference. Here's an important problem that is still open: what is the best way to integrate Generalized Algebraic Data Types into Hindley-Milner type inference? Every

Curried anonymous function in SML

倾然丶 夕夏残阳落幕 提交于 2019-12-03 04:54:40
I have the function below and it works: (fn x => x * 2) 2; but this one doesn't work: (fn x y => x + y ) 2 3; Can anyone tell me why? Or give me some hint to get it to work? (fn x => fn y => x+y) 2 3; works. fn simply doesn't have the same syntactic sugar to define curried functions that fun has. In Standard ML, a function can have only one argument , so use (fn (x,y) => x + y) (2,3) and the type is fn: int * int -> int in this time (x,y) and (2,3) is a list structure, The answers posted above are correct. SML functions take only one argument. As a result, SML functions can have only one of

Functional Breadth First Search

我只是一个虾纸丫 提交于 2019-12-03 04:16:26
问题 Functional depth first search is lovely in directed acyclic graphs. In graphs with cycles however, how do we avoid infinite recursion? In a procedural language I would mark nodes as I hit them, but let's say I can't do that. A list of visited nodes is possible, but will be slow because using one will result in a linear search of that list before recurring. A better data structure than a list here would obviously help, but that's not the aim of the game, because I'm coding in ML - lists are

Horner's algorithm in SML? [closed]

▼魔方 西西 提交于 2019-12-02 23:52:48
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I am trying to implement Horner's algorithm in SML. fun horner(lst1:real list,x:real) = let val i = ref 1 val result = ref (List.last(lst1)) in if (lst1) = ([]:real list) then 0.0 else while (!i <= length(lst1)-1) do (result:=!result*x+List.nth(lst1,length(lst1)-(!i)-1); i := !i+1); !result end; Takes on a{n},

Using ListPair.foldr to implement zipWith in SML

倖福魔咒の 提交于 2019-12-02 21:54:22
问题 Background: Beginner level at SML My assignment requires me to use ListPair.foldr and only this function to implement the zipWith function. ListPair.foldr : ('a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list -> 'c zipWith : ('a * 'b -> 'c) -> 'a list -> 'b list -> 'c list ListPair.foldr returns a single 'c element, while zipWith returns a 'c list, so naturally my approach would use ListPair.foldr repeatedly to spill out individual 'c elements which I then put in my 'c list. ListPair.foldr takes

Is there a Haskell/ML-like compiler to C?

无人久伴 提交于 2019-12-02 20:37:48
People have written games for the iPhone in Scheme. Because (some) Scheme-compilers compile down to C, it was easy to mix with Objective-C and integrate with XCode. I am aware of patches for Haskell and OCaml compilers to enable ARM/iOS-backends. But those appear unofficial and experimental/unstable. I prefer a static haskell/ML-type type-system over Scheme's dynamic typing. Is there a stable ML/SML/Haskell compiler which generates C-code so that it can be used in a similar way as Scheme/Gambit-C? I can't help with ML, but have you looked at JHC ? JHC is a whole-program optimizing Haskell

What are the limits of type inference?

会有一股神秘感。 提交于 2019-12-02 19:07:40
What are the limits of type inference? Which type systems have no general inference algorithm? Joe Wells showed that type inference is undecidable for System F, which is the most basic polymorphic lambda calculus, independently discovered by Girard and Reynolds. This is the most important result showing the limits of type inference. Here's an important problem that is still open: what is the best way to integrate Generalized Algebraic Data Types into Hindley-Milner type inference? Every year Simon Peyton Jones comes up with a new answers, which is supposedly better than the previous year's

Functional Breadth First Search

假如想象 提交于 2019-12-02 17:34:40
Functional depth first search is lovely in directed acyclic graphs. In graphs with cycles however, how do we avoid infinite recursion? In a procedural language I would mark nodes as I hit them, but let's say I can't do that. A list of visited nodes is possible, but will be slow because using one will result in a linear search of that list before recurring. A better data structure than a list here would obviously help, but that's not the aim of the game, because I'm coding in ML - lists are king, and anything else I will have to write myself. Is there a clever way around this issue? Or will I

How to draw a type-inference parse tree for sml

眉间皱痕 提交于 2019-12-02 14:49:24
问题 So I am working on my practice final, there is problem ask me to draw a parse tree for this sml code: fun ff f x y = if (f x y) then (f 3 y) else (f x "zero") val ff = fn : (int -> string -> bool) -> int -> string -> bool I understand how to get this type but not so sure how to draw a parse tree to represent it. For my homework we did do question like this which is way simpler : image for my other homework 回答1: The simplest way I can think of is to slightly reformat your code and basically