sml

I want to make function maptree with standard ML

↘锁芯ラ 提交于 2019-12-23 18:23:02
问题 I want to make function maptree with standard ML. If function f(x) = x + 1; then maptree(f, NODE(NODE(LEAF 1,LEAF 2),LEAF 3)); should make result NODE(NODE(LEAF 2,LEAF 3),LEAF 4)) I write the code like below. datatype 'a tree = LEAF of 'a | NODE of 'a tree * 'a tree; fun f(x) = x + 1; fun maptree(f, NODE(X, Y)) = NODE(maptree(f, X), maptree(f, Y)) | maptree(f, LEAF(X)) = LEAF(f X); but when I execute this code like this maptree(f, (NODE(NODE(LEAF 1,LEAF 2),LEAF 3))); result is not I want to

How do I inspect an integer's base-2 representation in Standard ML? [duplicate]

浪尽此生 提交于 2019-12-23 16:27:45
问题 This question already has an answer here : How to do bitwise AND in SML/NJ? (1 answer) Closed 3 years ago . I am trying to implement a repeated squaring algorithm in SML. I want it to be tail recursive. The goal is to multiply all x^k where k is 2^m and 2^m is a 1 in the binary representation of n. E.g, for x^25, calculate x^1 * x^8 * x^16 because 16 + 8 + 1 = 25 I have no idea how to represent a number by it's binary parts, or to use bitwise operations to check manually (because from what I

Accessing SML tuples by Index Variable

六眼飞鱼酱① 提交于 2019-12-23 08:54:05
问题 Question is simple. How to access a tuple by using Index variable in SML? val index = 5; val tuple1 = (1,2,3,4,5,6,7,8,9,10); val correctValue = #index tuple1 ?? I hope, somebody would be able to help out. Thanks in advance! 回答1: There doesn't exist a function which takes an integer value and a tuple, and extracts that element from the tuple. There are of course the #1 , #2 , ... functions, but these do not take an integer argument. That is, the name of the "function" is #5 , it is not the

Accessing SML tuples by Index Variable

独自空忆成欢 提交于 2019-12-23 08:52:57
问题 Question is simple. How to access a tuple by using Index variable in SML? val index = 5; val tuple1 = (1,2,3,4,5,6,7,8,9,10); val correctValue = #index tuple1 ?? I hope, somebody would be able to help out. Thanks in advance! 回答1: There doesn't exist a function which takes an integer value and a tuple, and extracts that element from the tuple. There are of course the #1 , #2 , ... functions, but these do not take an integer argument. That is, the name of the "function" is #5 , it is not the

Evaluate buffer in ghci or hugs via Emacs

吃可爱长大的小学妹 提交于 2019-12-23 07:21:59
问题 Using sml-mode in Emacs I have been able to send my buffer contents directly to an inferior SML process using C-c C-b . Now I want to do the same thing only with Haskell. Haskell-mode does not seem to support this, so I'm wondering: What is the right way to go about this with Emacs and Haskell? While learning SML I've been using C-c C-b almost non-stop to easily evaluate my program as I go, instantly seeing the results of assigning values etc. But if I use C-c C-l in haskell-mode on a saved

Evaluate buffer in ghci or hugs via Emacs

旧城冷巷雨未停 提交于 2019-12-23 07:21:05
问题 Using sml-mode in Emacs I have been able to send my buffer contents directly to an inferior SML process using C-c C-b . Now I want to do the same thing only with Haskell. Haskell-mode does not seem to support this, so I'm wondering: What is the right way to go about this with Emacs and Haskell? While learning SML I've been using C-c C-b almost non-stop to easily evaluate my program as I go, instantly seeing the results of assigning values etc. But if I use C-c C-l in haskell-mode on a saved

Turning a string into a char list list using SML

萝らか妹 提交于 2019-12-23 03:48:10
问题 I'm trying to turn a string into a char list list and I have the following code: fun linelist file = let val instr = TextIO.openIn file val str = TextIO.inputAll instr in String.tokens (fn x => x = #"\n")str before TextIO.closeIn instr end; fun getsudo file = map explode (linelist file); I need to turn a file (sudo.txt) with the following 53**7****\n6**195***\n*98****6*\n8***6***3\n4**8*3**1\n7***2***6\n*6****28*\n***419**5\n****8**79\n into [[#"5",#"3",#"*",#"*",#"7",#"*",#"*",#"*",#"*",], [

Using Poly/ML to build projects with nested directory structures

纵饮孤独 提交于 2019-12-23 01:38:28
问题 Until now, I have been using Poly/ML for several small projects where all source code files are all in the same directory. To build these projects, all I had to do was run the following command in the REPL: > PolyML.make "Main"; But now I have a project whose scale makes it impractical to put all source code files in the same directory. To build these projects in the REPL, I need to run the following commands: > PolyML.make "foo/Foo"; > PolyML.make "bar/Bar"; > PolyML.make "qux/Qux"; > PolyML

What is the difference between 'a and ''a in SML?

风流意气都作罢 提交于 2019-12-22 10:58:24
问题 For example: fun example (a:'a list) : list = a will have a signatures of: 'a list -> 'a list What if I define it differently but with same content like fun example (a : ''a list) : list = a its signature will be: ''a list -> ''a list What's the difference? 回答1: A plain type variable like 'a can be substituted with an arbitrary type. The form ''a is a so-called equality type variable, which means that it can only be substituted by types that admit the use of the equality operator = (or <> )

SML : why functions always take one-argument make language flexible

岁酱吖の 提交于 2019-12-22 08:28:48
问题 I have learned (from a SML book) that functions in SML always takes just one argument: a tuple. A function that takes multiple arguments is just a function that takes one tuple as argument, implemented with a tuple binding in function binding. I understand this point. But after this, the book says something that I don't understand: this point makes SML language flexible and elegant design, and you can do something useful that you cannot do in Java. Why does this design make the language