简体   繁体   English

F#记录和评估

[英]F# record and evaluating of this

I'm struggling a bit with a F#-assignment, which I hope you can answer for mere: I have that 我在F#分配上有些挣扎,我希望您能回答这个问题:我有

We will use the type OrderedList<'a> declared as follows 我们将使用如下声明的OrderedList<'a>类型

type OrderedList<’a when ’a : equality> = 
    { front: ’a list
    ; rear: ’a list}

For instance, the value let ex = {front = ['x']; rear = ['z';'y']} 例如,值let ex = {front = ['x']; rear = ['z';'y']} let ex = {front = ['x']; rear = ['z';'y']} has type OrderedList<char> and represents the ordered list ['x', 'y', 'z'] . OrderedList<char> let ex = {front = ['x']; rear = ['z';'y']}类型为OrderedList<char>并表示有序列表['x', 'y', 'z']

The question that I'm struggling with is: 我正在努力的问题是:

We define the canonical representation of an ordered list to be the representation where the rear list is empty. 我们将有序列表的规范表示定义为后列表为空的表示。 Declare a function canonical:OrderedList<'a>->OrderedList<'a> , where canonical ol returns the canonical representation of ol . 声明函数canonical:OrderedList<'a>->OrderedList<'a> ,其中canonical ol返回的规范表示ol

Just as a startup, I've tried something: 作为一家创业公司,我尝试了一些方法:

let canonicial (list:OrderedList<'a>)= 
    match list with
    | {x::xs}, {y::xss} -> if x = y then "SUCCESS!!!!" else failwith "FEJL!!!"
    | _ -> failwith "Some"

My issue is that I don't know how to get to the element in the type / the syntax for this. 我的问题是我不知道该如何获取类型/语法中的元素。 I know the function has not been solved correctly, but right now I focus mostly on the syntax. 我知道该函数尚未正确解决,但现在我主要关注语法。

Hope to get some help! 希望得到一些帮助!

Well I think I can give you the solution now (you surely have more problems to solve): 好吧,我想我可以为您提供解决方案(您肯定还有更多的问题需要解决):

let canonical = 
   function   
   | { front = _; rear = [] } as ol -> ol
   | { front = fs; rear = rs } -> { front = fs @ List.rev rs; rear = [] }

as you can see the first case is when the rear is already empty - here it's enough to give to original back 如您所见,第一种情况是后部已经空了-在这里足够将原件退回

in the other case we have to get a new OrderedList<'a> with the reversed old rear appended to the old front - that's it - you don't even need the constraint on 'a - and indeed I find it strange to put it there - usually it's better to but the constraints on the functions in FP - but well different styles and stuff. 在另一种情况下,我们必须获得一个新的OrderedList<'a> ,并将反向的旧后方附加到旧的前方-就是这样-您甚至不需要'a的约束-实际上我发现放置它很奇怪那里-通常情况下最好,但要限制FP中的功能-但样式和内容要大不相同。

I hope this helps you out a bit 希望这对您有所帮助

BTW: I used function on purpose - you should try to convert this into your usual match ... with ... style so you can get your syntax right 顺便说一句:我故意使用function -您应尝试将其转换为通常的match ... with ...风格match ... with ...以便正确使用语法

Thanks, thanks, thanks! 谢谢,谢谢,谢谢! Now I better understand this topic! 现在我更好地理解了这个话题! I rewritten your code to: 我将您的代码重写为:

let canonical2 (ol:OrderedList<'a>) : OrderedList<'a> = 
    match ol with
    |{ front = _; rear = []} -> ol
    |{ front = f; rear = r} -> {front = f @ List.rev r; rear = []}

Another way to do it granted that @ already takes care of returning the "other list" if one is empty (so shouldn't be an overhead to always append) : 这样做的另一种方式是,如果一个人为空,@已经负责返回“其他列表”(因此不应该总是附加开销):

let canonical ol = { ol with front = ol.front @ List.rev ol.rear; rear = [] }
// or
let canonical { front = fs; rear = rs } = { front = fs @ List.rev rs; rear = [] }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM