sml

Javascript vs Python with respect to Python 'map()' function

纵然是瞬间 提交于 2019-12-22 07:04:12
问题 In Python there is a function called map that allows you to go: map(someFunction, [x,y,z]) and go on down that list applying the function. Is there a javascript equivalent to this function? I am just learning Python now, and although I have been told javascript is functional language, I can see that I have been programming in a non-functional javascript style. As a general rule, can javascript be utilized as a functional language as effectively as Python can? Does it have similar tricks like

Recursive anonymous functions in SML

孤街浪徒 提交于 2019-12-22 04:29:11
问题 Is it possible to write recursive anonymous functions in SML? I know I could just use the fun syntax, but I'm curious. I have written, as an example of what I want: val fact = fn n => case n of 0 => 1 | x => x * fact (n - 1) 回答1: The anonymous function aren't really anonymous anymore when you bind it to a variable. And since val rec is just the derived form of fun with no difference other than appearance, you could just as well have written it using the fun syntax. Also you can do pattern

building a lexical analyser using ml-lex

自闭症网瘾萝莉.ら 提交于 2019-12-21 19:53:35
问题 I need to create a new instance of a lexer tied to the standard input stream. However, when I type in val lexer = makeLexer( fn n => inputLine( stdIn ) ); I get an error that I don't understand: stdIn:1.5-11.13 Error: operator and operand don't agree [tycon mismatch] operator domain: int -> string operand: int -> string option in expression: ( makeLexer is a function name present in my source code) 回答1: inputLine returns a string option , and my guess is a string is expected. What you want to

BigInt for Standard ML/NJ

半城伤御伤魂 提交于 2019-12-21 18:02:54
问题 Is there a Java BigInt equivalent for Standard ML? The normal int type throws an exception when it overflows. 回答1: Yes, see the IntInf structure. 回答2: The official SML'97 standard basis library introduces a zoo of structures like Int, IntInf, Int32, Int64, LargeInt etc. To actually use them in practice to make things work as expected, and make them work efficiently, you need to look closely at the SML implementation at hand. One family of implementations imitates the memory layout of C and

Iteration over multiple lists in SML

那年仲夏 提交于 2019-12-21 03:16:13
问题 I am having two lists in SML, lets say list A [(a,b,c),(d,e,f)] and list B [b,e] . I want to count how many occurrence of each item in B that matches the second element of each triple in A. The output should be 2. Because b and e each occurs once in A. This is my code so far but my counter is always set to 0 when I move from one element to another in B. I know in Java this will just be a simple double for loop. fun number_in_months (d : (int * int * int ) list, m : (int) list) = if null m

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

寵の児 提交于 2019-12-20 10:21:04
问题 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

Partial sum in Standard ML?

落爺英雄遲暮 提交于 2019-12-20 05:39:18
问题 Im new to functional programming and I have an assignment to compute partial sum of a list. E.g. - psum [1,1,1,1,1]; val it = [1,2,3,4,5] : int list Here is the my code so far. However in function psum2[L] i dont know how to go through each value and add them up so I just print the list. fun psum2(L) : int list = if L=nil then [] else L; fun pSum(L) : int list = psum2(L); exception Empty_List; psum([2,3,4]); 回答1: Your question is a little broad, but here's one way to sum a list. Perhaps you

Type variable to be unified occurs in type

南楼画角 提交于 2019-12-20 03:12:33
问题 I have a function to reconstruct a tree from 2 lists. I return a list on all branches, but I am getting an error that I don't understand. But I assume it has to do with the return types. The error is this: Can't unify ''a with ''a list (Type variable to be unified occurs in type) Found near recon ( ::( preoH, preoT), ::( inoH, ...)) Exception- Fail "Static errors (pass2)" raised The line the error occurs at is the headline of the function definition fun recon (preoH::preoT, inoH::inoT) = What

SML not detecting OS on OS X Mavericks

佐手、 提交于 2019-12-20 02:58:17
问题 I could not run SMLNJ on Mavericks It shows me the error sml: unable to determine architecture/operating system I also looked in /usr/local/smlnj/config/_arch-n-opsys file and Mavericks is mentioned there. 13*) OPSYS=darwin; HEAP_OPSYS=darwin ;; # MacOS X 10.9 Mavericks Can some one help me on this? 回答1: You can use this .pkg file to reinstall the SML and see if the problem happens again. 来源: https://stackoverflow.com/questions/20009628/sml-not-detecting-os-on-os-x-mavericks

How to combine equal sequence elements (functional programming)?

妖精的绣舞 提交于 2019-12-20 02:44:31
问题 I want to write a function that takes in a sequence <1,1,2,2,3> and returns the sequence with equal elements grouped like <<1,1>, <2,2>, <3>>. I'm using sequences, not lists, but some of the functions are similar. Some of the functions I am thinking of using are map, reduce, tabulate, filter, append etc.. Reduce takes in an associative function and returns the sequence that is "reduced" by that operator. So, reduce op+ 0 <1,2,3> = 6. My first thought was to use map to raise the sequence by