为什么从字符串转换为int会导致奇怪的值(Why converting from string to int results in weird value)
我是F#的新手,我无法找到数字字符串的最后一个元素,并将该元素转换为int。 所以如果字符串是“123”,那么我想回到3.然而,这个代码:
let s (a : string) = let x = a.[0] int x
这让我回到49,当我做
s "123"
。 我无法弄清楚为什么会发生这种情况。 另外,当我输入字符串“124”时,它仍然给我49.有人可以解释为什么会发生这种情况,我该如何解决这个问题?I'm new to F# and I was having trouble finding the last element of an number-string and converting that element to an int. So if the string was "123", then I would want to get back 3. However, this code:
let s (a : string) = let x = a.[0] int x
This gives me back 49 when I do
s "123"
. I can't figure out why this is happening. Also, when I input the string "124" it still gives me 49. Can someone explain why this is happening and how I can fix this?
原文:https://stackoverflow.com/questions/37463247
最满意答案
不确定,我不能尝试,因为我现在没有Turbo Prolog,但我依稀记得Variables不需要声明。
我怀疑孩子应该是人(毕竟,孩子是一个人)。 尝试以这种方式简化
DOMAINS Person = SYMBOL PREDICATES parent(Person,Person) female(Person) male(Person) mother(Person,Person) ...
not sure about, and I can't try because I don't have Turbo Prolog right now, but I vaguely remember that Variables doesn't need to be declared.
And I suspect that Child should be Person instead (after all, a Child is a Person). Try to simplify in this way
DOMAINS Person = SYMBOL PREDICATES parent(Person,Person) female(Person) male(Person) mother(Person,Person) ...
相关问答
更多-
是的,您可以在任何阶段 (WAM,小装配,组装等) 之后停止编译器 。 要在产生WAM代码后停止,请在gplc使用标志-W或-w 。 Yes, you can stop the compiler after any stage ( WAM, mini-assembly, assembly, etc). To stop after WAM code is produced, use flag -W or -w with gplc.
-
在Prolog中#=意味着什么?(What does #= mean in Prolog?)[2024-01-10]
从文档输入页面 : 它们实现整数表达式之间的纯关系 ,并可用于所有方向 只是一个例子: ?- X+3 #= X*2. X = 3. 看起来很简单,但实际上在常规算术表达式评估中很难获得这样的结果。 ?- X+3 is X*2. ERROR: is/2: Arguments are not sufficiently instantiated 从is / 2页面,您可以看到签名 -Number is +Expr 其中+Expr表示必须接地。 另外,左边的参数是/ 2实际上应该是原子的: ?- 3+3 is ... -
PROLOG程序出错(Error in PROLOG program)[2022-08-13]
不确定,我不能尝试,因为我现在没有Turbo Prolog,但我依稀记得Variables不需要声明。 我怀疑孩子应该是人(毕竟,孩子是一个人)。 尝试以这种方式简化 DOMAINS Person = SYMBOL PREDICATES parent(Person,Person) female(Person) male(Person) mother(Person,Person) ... not sure about, and I can't try because I don't have Turb ... -
prolog中的“或”程序(“Or” procedure in prolog)[2021-12-08]
这里有一些提示。 您可以通过Suspect1 = Suspect2与Suspect1具有相同的特征,并通过member(Suspect1, Suspects)列表的任何成员是否具有Suspect1的特征。 在Prolog中处理析取的常用方法是引入带有两个子句的谓词。 例如,检查嫌疑人是否有戒指或者mac的谓词 ring_or_mac(suspect(_, _, _, _, ring, _)). ring_or_mac(suspect(_, _, _, _, mac, _)). 如果您以正确的方式将这些提示 ... -
我试过这个简单的方法: foo.pl内容: foo :- write('hello, world'), nl. 然后在python中: >>> import commands >>> commands.getoutput('echo "foo." | swipl -q -f foo.pl') 'hello, world\ntrue.\n\n' >>> x = commands.getoutput('echo "foo." | swipl -q -f foo.pl') >>> x 'hello, world ...
-
带列表的Prolog程序(Prolog program with lists)[2022-09-10]
这对我来说似乎很复杂。 问题是基本上做长形式的乘法,就像你在纸上做的那样。 如果您首先反转列表(使用内置的reverse/2谓词),事情会变得简单得多: %--------------------------------------------------------------% % Mult/3: multiply a integer, represented as a list of digits, % % by an integer value N, producing an int ... -
当控制台由于halt被调用而关闭时,它不是“崩溃”。 如果从命令行而不是控制台运行程序swipl -s proof_tester.pl ,那么如果代码执行halt ,则输出仍将在“终端”窗口中。 您可能更喜欢将消息从SWI-Prolog重定向到文本文件( Windows , Linux ),然后您可以使用文本编辑器或其他方式随意阅读。 When the console closes due to halt being invoked, it is not a "crash". If you run the ...
-
什么time/1 ? 在SWI-Prolog中,尝试: ?- time(your_goal). 和 ?- profile(your_goal). What about time/1? In SWI-Prolog, try: ?- time(your_goal). and ?- profile(your_goal).
-
谓词p/1使用内置谓词(is)/2 。 注意, X is 5是一种语法上更方便的写作方式is(X,5) 。 但是你的元解释器只期望用户定义的谓词和控制结构(',')/2和true/0 。 如果你想处理(is)/2你必须为它引入一个单独的条款。 sol(X is Y) :- !, X is Y. 在ISO Prolog中,目标predicate_property(Goal,built_in)可用于测试Goal是否是内置谓词。 这适用于许多系统,如B,GNU,SICStus,SWI,XSB,YAP。 所以你可 ...
-
要么?- [fact]. 或?- ['fact.pl']. 应该管用 either ?- [fact]. or ?- ['fact.pl']. should work