在Haskell中查找字符串中最短和最长的单词 [英] Finding the shortest and longest word of a string in Haskell

查看:178
本文介绍了在Haskell中查找字符串中最短和最长的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到给定字符串中最短和最长的单词。如果输入的字符串是家庭移动电话,那么最长的单词应该是电话,最短的单词将被点击。我已经写了下列功能。我如何找到字符串的索引并从中找到最短和最长的单词?

  ---------将字符串转换为字符串列表并查找每个字的长度---- ---- 

stringConvert :: String - > [Int]
stringConvert x =地图(长度)(单词x)

----------查找最长的单词---------- -

findLongestWord :: String - > Int
findLongestWord x = maximum(stringConvert x)

----------找到最短的单词-----------

findShortestWord :: String - > Int
findShortestWord x = minimum(stringConvert x)


解决方案

<因为你正在寻找某个单词,所以你的函数的签名应该是

  findLongestWord :: String  - >字符串
findShortestWord :: String - >字符串

您已经实现的 stringConvert 正确的想法,但它有点问题,因为结果没有哪个单词与哪个长度相关的信息。将长度计算中的字符串拆分为单词列表可能更好,并且有一个方便的函数称为比较,它实际上不需要 stringConvert 函数。

  import Data.List(maximumBy)
import Data .Ord(比较)

findLongestWord :: String - >字符串
findLongestWord s = maximumBy(比较长度)(单词s)

maximumBy 类似于最大值,但它将第一个参数用作比较两个项目的函数。 比较是一个高阶函数,可用于转换类型为 a - >的函数。 b (其中 b 是可以比较的某种类型,即 Ord b => a - > b ,如果你已经熟悉类型约束)到两个 a 之间的比较函数(即类型为 a - > a - >订购)。



最短的单词可以以类似的方式找到。 b

I am trying to find the shortest and longest word in a given string. If the entered string is "house tap mobile telephone" then the longest word should be telephone and shortest will be tap. I have already written the following functions. How do i find the index of the strings and find the shortest and longest word out of it?

---------Converting string into a list of strings and finding length of each word--------

stringConvert :: String -> [Int]
stringConvert x = map (length) (words x)

----------Find the longest word-----------

findLongestWord :: String -> Int
findLongestWord x = maximum(stringConvert x)

----------Find the shortest word-----------

findShortestWord :: String -> Int
findShortestWord x = minimum(stringConvert x)

解决方案

Since you are looking for a certain word, the signatures of your functions should be

findLongestWord :: String -> String
findShortestWord :: String -> String

The stringConvert function you have implemented has the right idea, but it is a bit problematic because the result doesn't have the information which word is associated with which length. It might be better to separate splitting the string into a word list from the length calculation, and there is a handy function called comparing that actually removes the need for the stringConvert function altogether.

import Data.List (maximumBy)
import Data.Ord (comparing)

findLongestWord :: String -> String
findLongestWord s = maximumBy (comparing length) (words s)

maximumBy is similar to maximum but it takes as the first parameter a function that should be used to compare two items. comparing is a higher-order function that can be used to convert a function of type a -> b (where bis some type that can be compared, i.e. Ord b => a -> b if you are already familiar with type constraints) into a comparison function between two a's (i.e. a function of type a -> a -> Ordering).

The shortest word can be found in a similar manner.

这篇关于在Haskell中查找字符串中最短和最长的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆