首页 \ 问答 \ 为什么从字符串转换为int会导致奇怪的值(Why converting from string to int results in weird value)

为什么从字符串转换为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
更新时间:2022-06-09 22:06

最满意答案

不确定,我不能尝试,因为我现在没有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.
  • 从文档输入页面 : 它们实现整数表达式之间的纯关系 ,并可用于所有方向 只是一个例子: ?- 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 ...
  • 不确定,我不能尝试,因为我现在没有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 ...
  • 这里有一些提示。 您可以通过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 ...
  • 这对我来说似乎很复杂。 问题是基本上做长形式的乘法,就像你在纸上做的那样。 如果您首先反转列表(使用内置的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

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)