sml

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

True QuickSort in Standard ML

断了今生、忘了曾经 提交于 2019-11-30 09:55:24
问题 Since RosettaCode's Standard ML solution is a very slow version of Quicksort according to the question (and discussion) "Why is the minimalist, example Haskell quicksort not a "true" quicksort?", how would a functional Quicksort look like in Standard ML if it behaved according to the complexity of Hoare's algoritm? fun quicksort [] = [] | quicksort (x::xs) = let val (left, right) = List.partition (fn y => y<x) xs in quicksort left @ [x] @ quicksort right end That is, one that employs some

Open file in ML(SMLNJ)

旧时模样 提交于 2019-11-30 09:54:00
问题 I need to read file in ML (SLMNJ) and save it in some structures. I need to read some data that points to graph declaration: [( 1 , 2 , 13 ),( 2 , 3 , 3 ),( 2 , 4 , 8 ),( 2 , 5 , 4 ),( 3 , 1 , 5 ),( 3 , 4 , 1 ),( 4 , 6 , 5 ),( 5 , 5 , 5 ),( 6 , 4 , 6 )] (first number: name of the node , secend number: name of connected node , third number weight for this mane (each () show one mane ) ) for expamle this is test input how to read file and which structure to save it 回答1: for reading from file

What are the options SOME and NONE in SML?

萝らか妹 提交于 2019-11-30 09:14:51
问题 I am new to SML (and programming, actually). fun readlist (infile : string) = let val ins = TextIO.openIn infile fun loop ins = case TextIO.inputLine ins of SOME line => line :: loop ins | NONE => [] in loop ins before TextIO.closeIn ins end ; This is a program I encountered here. How do I use SOME and NONE, and how to use 'before'? 回答1: The option data type is used if there is a possibility of something having no valid value. For instance, fun divide x y = if y == 0 then NONE else SOME (x /

Standard ML functor examples

假如想象 提交于 2019-11-30 06:58:16
问题 Functors in Standard ML are related to the module system and can generate structures based on other structures. An example of a functor generating list combinators for various types of lists is given below, but this example has a problem: The various types of lists all have advantages -- for example, lazy lists can be infinitely long, and concantenation lists have a O(1) concat operator. But when all of these list types conform to the same signature, the functor can only use their general

How to 'fix' the SML/NJ interactive system to use Arrow Keys

天大地大妈咪最大 提交于 2019-11-30 03:45:20
I'm having some trouble using SML/NJ interactive system, namely, that when I try to use my arrow keys (either left or right to make a correction in the expression I've typed, up to repeat the last expression), my Terminal prints codes. (e.g. ^[[A for up ^[[D for left, etc.). While I can still use the system, it makes it very tedious. I've looked around in Control.Compiler, is there something I'm missing? For whatever its worth, I'm using the Mac Terminal. Thanks ^_^ Try this. You can use socat to add readline support to many things: socat READLINE EXEC:sml I just realized you're on OS X. socat

Explaining pattern matching vs switch

僤鯓⒐⒋嵵緔 提交于 2019-11-29 20:25:24
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. Having formerly been one of "those people", I don't know that there's a succinct way to sum up why pattern-matching is such tasty goodness. It's experiential. Back when I had just glanced at pattern-matching and

Open file in ML(SMLNJ)

不羁岁月 提交于 2019-11-29 18:11:44
I need to read file in ML (SLMNJ) and save it in some structures. I need to read some data that points to graph declaration: [( 1 , 2 , 13 ),( 2 , 3 , 3 ),( 2 , 4 , 8 ),( 2 , 5 , 4 ),( 3 , 1 , 5 ),( 3 , 4 , 1 ),( 4 , 6 , 5 ),( 5 , 5 , 5 ),( 6 , 4 , 6 )] (first number: name of the node , secend number: name of connected node , third number weight for this mane (each () show one mane ) ) for expamle this is test input how to read file and which structure to save it for reading from file follow this to list of string per line : val infile = "c:/input.txt" ; fun readlist (infile : string) = let

What is wrong with my code in sml?

老子叫甜甜 提交于 2019-11-29 17:58:18
I don't know why my code doesn't work. fun lookup _ [] = 0 | lookup key ((k,v)::entries) = if k = key then v else (lookup key entries) That's what happened when I tested it in cmd. val lookup = fn : ''a -> (''a * int) list -> int - lookup (1,[(1,2),(2,3)]); val it = fn : ((int * (int * int) list) * int) list -> int There's nothing wrong with your code, you just didn't call lookup with enough arguments. You make a common mistakes among beginner SML programmers coming from other languages. I'll try to clarify that. First, the most important thing to know about functions in Standard ML is this:

What are the options SOME and NONE in SML?

帅比萌擦擦* 提交于 2019-11-29 14:08:04
I am new to SML (and programming, actually). fun readlist (infile : string) = let val ins = TextIO.openIn infile fun loop ins = case TextIO.inputLine ins of SOME line => line :: loop ins | NONE => [] in loop ins before TextIO.closeIn ins end ; This is a program I encountered here. How do I use SOME and NONE, and how to use 'before'? molbdnilo The option data type is used if there is a possibility of something having no valid value. For instance, fun divide x y = if y == 0 then NONE else SOME (x / y) could be used if you need to handle the special case of division by zero without resorting to